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
77
78
79
80
81
use super::Merge as MergeTrait;
use crate::utils::MaybeDone;
use core::fmt::{self, Debug};
use core::future::{Future, IntoFuture};
use core::pin::Pin;
use core::task::{Context, Poll};
use pin_project::pin_project;
macro_rules! impl_merge_tuple {
($($F:ident)+) => (const _: () = {
#[pin_project]
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[allow(non_snake_case)]
pub(super) struct Join<$($F: Future),*> {
done: bool,
$(#[pin] $F: MaybeDone<$F>,)*
}
impl<$($F),*> Debug for Join<$($F),*>
where $(
$F: Future + Debug,
$F::Output: Debug,
)* {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Join")
$(.field(&self.$F))*
.finish()
}
}
#[async_trait::async_trait(?Send)]
impl<$($F),*> MergeTrait for ($($F),*)
where $(
$F: IntoFuture,
)* {
type Output = ($($F::Output),*);
async fn merge(self) -> Self::Output {
let ($($F),*): ($($F),*) = self;
Join {
done: false,
$($F: MaybeDone::new($F.into_future())),*
}.await
}
}
impl<$($F: Future),*> Future for Join<$($F),*> {
type Output = ($($F::Output),*);
fn poll(
self: Pin<&mut Self>, cx: &mut Context<'_>
) -> Poll<Self::Output> {
let mut all_done = true;
let mut this = self.project();
assert!(!*this.done, "Futures must not be polled after completing");
$(all_done &= this.$F.as_mut().poll(cx).is_ready();)*
if all_done {
*this.done = true;
Poll::Ready(($(this.$F.take().unwrap()),*))
} else {
Poll::Pending
}
}
}
}; )
}
impl_merge_tuple! { A B }
impl_merge_tuple! { A B C }
impl_merge_tuple! { A B C D }
impl_merge_tuple! { A B C D E }
impl_merge_tuple! { A B C D E F }
impl_merge_tuple! { A B C D E F G }
impl_merge_tuple! { A B C D E F G H }
impl_merge_tuple! { A B C D E F G H I }
impl_merge_tuple! { A B C D E F G H I J }
impl_merge_tuple! { A B C D E F G H I J K }
impl_merge_tuple! { A B C D E F G H I J K L }