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.
This commit is contained in:
Matt Corallo 2024-05-10 20:49:30 +00:00
parent 20f2dab5c0
commit 98022e6d6d
2 changed files with 35 additions and 0 deletions

View file

@ -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(())
}
}
}

View 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;