1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use core::pin::Pin;
use core::{
    future::Future,
    task::{Context, Poll},
};

extern crate alloc;
use alloc::vec::Vec;

use crate::BoxFuture;

/// A future cosisting of multiple other futures. Ready once all of the inner futures are ready.
pub struct JoinedFuture<'a, T> {
    futures: Vec<(Option<T>, BoxFuture<'a, T>)>,
}

impl<'a, T> JoinedFuture<'a, T> {
    #[inline]
    fn new(futures: Vec<BoxFuture<'a, T>>) -> Self {
        Self {
            futures: futures.into_iter().map(|x| (None, x)).collect(),
        }
    }
}

impl<'a, T> Future for JoinedFuture<'a, T> {
    type Output = Vec<T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut done = true;
        let me = unsafe {
            // SAFETY: This is the only way to access the futures array, and is safe because the
            // pin necessarily lives as long as poll() and is owned, so won't be modified
            self.get_unchecked_mut()
        };
        for future in me.futures.iter_mut() {
            if future.0.is_some() {
                continue;
            }
            done = false;
            if let Poll::Ready(content) = future.1.as_mut().poll(cx) {
                future.0 = Some(content);
            }
        }
        if done {
            Poll::Ready(me.futures.iter_mut().map(|x| x.0.take().unwrap()).collect())
        } else {
            Poll::Pending
        }
    }
}

/// Joins multiple futures into one, which will be ready once all of the inner ones are. This is
/// effectively a small one-time-use runtime without the ability to add any tasks.
#[inline]
pub fn join<T>(futures: Vec<BoxFuture<T>>) -> JoinedFuture<T> {
    JoinedFuture::new(futures)
}

#[macro_export]
macro_rules! join {
    ($($a:expr),* $(,)?) => {
        join(vec![$(
            $crate::prep($a),
        )*])
    };
}

#[macro_export]
macro_rules! join_boxed {
    ($($a:expr),* $(,)?) => {
        join(vec![$(
            $a,
        )*])
    };
}