|
1 | | -#![allow(non_snake_case)] |
2 | | - |
3 | 1 | use crate::future::{assert_future, try_maybe_done, TryMaybeDone}; |
4 | 2 | use core::fmt; |
5 | 3 | use core::pin::Pin; |
6 | 4 | use futures_core::future::{Future, TryFuture}; |
7 | 5 | use futures_core::task::{Context, Poll}; |
8 | 6 | use pin_project_lite::pin_project; |
9 | 7 |
|
10 | | -macro_rules! generate { |
11 | | - ($( |
12 | | - $(#[$doc:meta])* |
13 | | - ($Join:ident, <Fut1, $($Fut:ident),*>), |
14 | | - )*) => ($( |
15 | | - pin_project! { |
16 | | - $(#[$doc])* |
17 | | - #[must_use = "futures do nothing unless you `.await` or poll them"] |
18 | | - pub struct $Join<Fut1: TryFuture, $($Fut: TryFuture),*> { |
19 | | - #[pin] Fut1: TryMaybeDone<Fut1>, |
20 | | - $(#[pin] $Fut: TryMaybeDone<$Fut>,)* |
21 | | - } |
22 | | - } |
| 8 | +pin_project! { |
| 9 | + /// Future for the [`try_join`](super::TryFutureExt::try_join) method. |
| 10 | + #[must_use = "futures do nothing unless you `.await` or poll them"] |
| 11 | + pub struct TryJoin<Fut1: TryFuture, Fut2: TryFuture> { |
| 12 | + #[pin] fut1: TryMaybeDone<Fut1>, |
| 13 | + #[pin] fut2: TryMaybeDone<Fut2> |
| 14 | + } |
| 15 | +} |
23 | 16 |
|
24 | | - impl<Fut1, $($Fut),*> fmt::Debug for $Join<Fut1, $($Fut),*> |
25 | | - where |
26 | | - Fut1: TryFuture + fmt::Debug, |
27 | | - Fut1::Ok: fmt::Debug, |
28 | | - Fut1::Error: fmt::Debug, |
29 | | - $( |
30 | | - $Fut: TryFuture + fmt::Debug, |
31 | | - $Fut::Ok: fmt::Debug, |
32 | | - $Fut::Error: fmt::Debug, |
33 | | - )* |
34 | | - { |
35 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
36 | | - f.debug_struct(stringify!($Join)) |
37 | | - .field("Fut1", &self.Fut1) |
38 | | - $(.field(stringify!($Fut), &self.$Fut))* |
39 | | - .finish() |
40 | | - } |
41 | | - } |
| 17 | +impl<Fut1: TryFuture, Fut2: TryFuture> TryJoin<Fut1, Fut2> { |
| 18 | + pub(crate) fn new(fut1: Fut1, fut2: Fut2) -> Self { |
| 19 | + Self { fut1: try_maybe_done(fut1), fut2: try_maybe_done(fut2) } |
| 20 | + } |
| 21 | +} |
42 | 22 |
|
43 | | - impl<Fut1, $($Fut),*> $Join<Fut1, $($Fut),*> |
44 | | - where |
45 | | - Fut1: TryFuture, |
46 | | - $( |
47 | | - $Fut: TryFuture<Error=Fut1::Error> |
48 | | - ),* |
49 | | - { |
50 | | - fn new(Fut1: Fut1, $($Fut: $Fut),*) -> Self { |
51 | | - Self { |
52 | | - Fut1: try_maybe_done(Fut1), |
53 | | - $($Fut: try_maybe_done($Fut)),* |
54 | | - } |
55 | | - } |
56 | | - } |
| 23 | +impl<Fut1, Fut2> fmt::Debug for TryJoin<Fut1, Fut2> |
| 24 | +where |
| 25 | + Fut1: TryFuture + fmt::Debug, |
| 26 | + Fut1::Ok: fmt::Debug, |
| 27 | + Fut1::Error: fmt::Debug, |
| 28 | + Fut2: TryFuture + fmt::Debug, |
| 29 | + Fut2::Ok: fmt::Debug, |
| 30 | + Fut2::Error: fmt::Debug, |
| 31 | +{ |
| 32 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 33 | + f.debug_struct("TryJoin").field("fut1", &self.fut1).field("fut2", &self.fut2).finish() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<Fut1, Fut2> Future for TryJoin<Fut1, Fut2> |
| 38 | +where |
| 39 | + Fut1: TryFuture, |
| 40 | + Fut2: TryFuture<Error = Fut1::Error>, |
| 41 | +{ |
| 42 | + type Output = Result<(Fut1::Ok, Fut2::Ok), Fut1::Error>; |
57 | 43 |
|
58 | | - impl<Fut1, $($Fut),*> Future for $Join<Fut1, $($Fut),*> |
59 | | - where |
60 | | - Fut1: TryFuture, |
61 | | - $( |
62 | | - $Fut: TryFuture<Error=Fut1::Error> |
63 | | - ),* |
64 | | - { |
65 | | - type Output = Result<(Fut1::Ok, $($Fut::Ok),*), Fut1::Error>; |
| 44 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 45 | + let mut all_done = true; |
| 46 | + let mut futures = self.project(); |
66 | 47 |
|
67 | | - fn poll( |
68 | | - self: Pin<&mut Self>, cx: &mut Context<'_> |
69 | | - ) -> Poll<Self::Output> { |
70 | | - let mut all_done = true; |
71 | | - let mut futures = self.project(); |
72 | | - all_done &= futures.Fut1.as_mut().poll(cx)?.is_ready(); |
73 | | - $( |
74 | | - all_done &= futures.$Fut.as_mut().poll(cx)?.is_ready(); |
75 | | - )* |
| 48 | + all_done &= futures.fut1.as_mut().poll(cx)?.is_ready(); |
| 49 | + all_done &= futures.fut2.as_mut().poll(cx)?.is_ready(); |
76 | 50 |
|
77 | | - if all_done { |
78 | | - Poll::Ready(Ok(( |
79 | | - futures.Fut1.take_output().unwrap(), |
80 | | - $( |
81 | | - futures.$Fut.take_output().unwrap() |
82 | | - ),* |
83 | | - ))) |
84 | | - } else { |
85 | | - Poll::Pending |
86 | | - } |
87 | | - } |
| 51 | + if all_done { |
| 52 | + Poll::Ready(Ok(( |
| 53 | + futures.fut1.take_output().unwrap(), |
| 54 | + futures.fut2.take_output().unwrap(), |
| 55 | + ))) |
| 56 | + } else { |
| 57 | + Poll::Pending |
88 | 58 | } |
89 | | - )*) |
90 | | -} |
91 | | - |
92 | | -generate! { |
93 | | - /// Future for the [`try_join`](try_join()) function. |
94 | | - (TryJoin, <Fut1, Fut2>), |
| 59 | + } |
95 | 60 | } |
96 | 61 |
|
97 | 62 | /// Joins the result of two futures, waiting for them both to complete or |
|
0 commit comments