Skip to content

Commit 98022e6

Browse files
committed
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.
1 parent 20f2dab commit 98022e6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lightning/src/util/async_poll.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Somse utilities to make working with std Futures easier
2+
3+
use crate::prelude::*;
4+
use core::future::Future;
5+
use core::marker::Unpin;
6+
use core::pin::Pin;
7+
use core::task::{Context, Poll};
8+
9+
pub(crate) struct MultiFuturePoller<F: Future<Output = ()> + Unpin>(pub Vec<Option<F>>);
10+
11+
impl<F: Future<Output = ()> + Unpin> Future for MultiFuturePoller<F> {
12+
type Output = ();
13+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
14+
let mut have_pending_futures = false;
15+
for fut_option in self.get_mut().0.iter_mut() {
16+
let mut fut = match fut_option.take() {
17+
None => continue,
18+
Some(fut) => fut,
19+
};
20+
match Pin::new(&mut fut).poll(cx) {
21+
Poll::Ready(()) => {},
22+
Poll::Pending => {
23+
have_pending_futures = true;
24+
*fut_option = Some(fut);
25+
},
26+
}
27+
}
28+
if have_pending_futures {
29+
Poll::Pending
30+
} else {
31+
Poll::Ready(())
32+
}
33+
}
34+
}

lightning/src/util/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod base32;
3030
pub(crate) mod base32;
3131

3232
pub(crate) mod atomic_counter;
33+
pub(crate) mod async_poll;
3334
pub(crate) mod byte_utils;
3435
pub(crate) mod transaction_utils;
3536
pub(crate) mod time;

0 commit comments

Comments
 (0)