Skip to main content

sofie_db/
join.rs

1use std::future::poll_fn;
2use std::task::Poll;
3
4use crate::BoxFuture;
5
6/// Polls in order first, so requests sent on a first poll leave as one pipeline.
7pub async fn join_all<T>(mut futures: Vec<BoxFuture<'_, T>>) -> Vec<T> {
8    let mut outputs: Vec<Option<T>> = futures.iter().map(|_| None).collect();
9    poll_fn(|context| {
10        let mut pending = false;
11        for (future, output) in futures.iter_mut().zip(outputs.iter_mut()) {
12            if output.is_some() {
13                continue;
14            }
15            match future.as_mut().poll(context) {
16                Poll::Ready(value) => *output = Some(value),
17                Poll::Pending => pending = true,
18            }
19        }
20        if pending {
21            Poll::Pending
22        } else {
23            Poll::Ready(())
24        }
25    })
26    .await;
27    outputs.into_iter().flatten().collect()
28}