Skip to content

Commit bd3e8eb

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 bd3e8eb

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lightning/src/util/async_poll.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Some utilities to make working with the standard library's [`Future`]s easier
11+
12+
use crate::prelude::*;
13+
use core::future::Future;
14+
use core::marker::Unpin;
15+
use core::pin::Pin;
16+
use core::task::{Context, Poll};
17+
18+
pub(crate) struct MultiFuturePoller<F: Future<Output = ()> + Unpin>(pub Vec<Option<F>>);
19+
20+
impl<F: Future<Output = ()> + Unpin> Future for MultiFuturePoller<F> {
21+
type Output = ();
22+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
23+
let mut have_pending_futures = false;
24+
for fut_option in self.get_mut().0.iter_mut() {
25+
let mut fut = match fut_option.take() {
26+
None => continue,
27+
Some(fut) => fut,
28+
};
29+
match Pin::new(&mut fut).poll(cx) {
30+
Poll::Ready(()) => {},
31+
Poll::Pending => {
32+
have_pending_futures = true;
33+
*fut_option = Some(fut);
34+
},
35+
}
36+
}
37+
if have_pending_futures {
38+
Poll::Pending
39+
} else {
40+
Poll::Ready(())
41+
}
42+
}
43+
}

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)