]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a utility to poll multiple futures simultaneously
authorMatt Corallo <git@bluematt.me>
Fri, 10 May 2024 20:49:30 +0000 (20:49 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 10 May 2024 21:29:53 +0000 (21:29 +0000)
If we have a handful of futures we want to make progress on
simultaneously we need some way to poll all of them in series,
which we add here in the form of `MultiFuturePoller`. Its probably
not as effecient as some of the options in the `futures` crate, but
it is very trivial and not that bad.

lightning/src/util/async_poll.rs [new file with mode: 0644]
lightning/src/util/mod.rs

diff --git a/lightning/src/util/async_poll.rs b/lightning/src/util/async_poll.rs
new file mode 100644 (file)
index 0000000..979d906
--- /dev/null
@@ -0,0 +1,34 @@
+//! Somse utilities to make working with std Futures easier
+
+use crate::prelude::*;
+use core::future::Future;
+use core::marker::Unpin;
+use core::pin::Pin;
+use core::task::{Context, Poll};
+
+pub(crate) struct MultiFuturePoller<F: Future<Output = ()> + Unpin>(pub Vec<Option<F>>);
+
+impl<F: Future<Output = ()> + Unpin> Future for MultiFuturePoller<F> {
+       type Output = ();
+       fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
+               let mut have_pending_futures = false;
+               for fut_option in self.get_mut().0.iter_mut() {
+                       let mut fut = match fut_option.take() {
+                               None => continue,
+                               Some(fut) => fut,
+                       };
+                       match Pin::new(&mut fut).poll(cx) {
+                               Poll::Ready(()) => {},
+                               Poll::Pending => {
+                                       have_pending_futures = true;
+                                       *fut_option = Some(fut);
+                               },
+                       }
+               }
+               if have_pending_futures {
+                       Poll::Pending
+               } else {
+                       Poll::Ready(())
+               }
+       }
+}
index c1ab8c75c2ee70efb51e7bb214ee76a8d06bef03..a81a36c5583b713bc491f6556e57999ce681af5e 100644 (file)
@@ -30,6 +30,7 @@ pub mod base32;
 pub(crate) mod base32;
 
 pub(crate) mod atomic_counter;
+pub(crate) mod async_poll;
 pub(crate) mod byte_utils;
 pub(crate) mod transaction_utils;
 pub(crate) mod time;