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;
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 {
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
}
}
}
#[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,
)*])
};
}