979d906392d7ad2442a5b369e521c1e83511fcec
[rust-lightning] / lightning / src / util / async_poll.rs
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 }