From 98022e6d6d2f8756abd82d1ebcf29f2e292e3983 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 10 May 2024 20:49:30 +0000 Subject: [PATCH] Add a utility to poll multiple futures simultaneously 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 | 34 ++++++++++++++++++++++++++++++++ lightning/src/util/mod.rs | 1 + 2 files changed, 35 insertions(+) create mode 100644 lightning/src/util/async_poll.rs diff --git a/lightning/src/util/async_poll.rs b/lightning/src/util/async_poll.rs new file mode 100644 index 000000000..979d90639 --- /dev/null +++ b/lightning/src/util/async_poll.rs @@ -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 + Unpin>(pub Vec>); + +impl + Unpin> Future for MultiFuturePoller { + 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(()) + } + } +} diff --git a/lightning/src/util/mod.rs b/lightning/src/util/mod.rs index c1ab8c75c..a81a36c55 100644 --- a/lightning/src/util/mod.rs +++ b/lightning/src/util/mod.rs @@ -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; -- 2.39.5