@@ -22,7 +22,7 @@ use crate::task::Poll;
2222/// async fn two() -> usize { 2 }
2323///
2424/// # let _ = async {
25- /// let x = join!(one(), two());
25+ /// let x = join!(one(), two()).await ;
2626/// assert_eq!(x, (1, 2));
2727/// # };
2828/// ```
@@ -39,7 +39,7 @@ use crate::task::Poll;
3939/// async fn three() -> usize { 3 }
4040///
4141/// # let _ = async {
42- /// let x = join!(one(), two(), three());
42+ /// let x = join!(one(), two(), three()).await ;
4343/// assert_eq!(x, (1, 2, 3));
4444/// # };
4545/// ```
@@ -71,61 +71,63 @@ pub macro join {
7171 } ,
7272 @rest : ( )
7373 ) => { {
74- // The futures and whether they have completed
75- let mut state = ( $( UnsafeCell :: new ( ( $fut, false ) ) , ) * ) ;
74+ async move {
75+ // The futures and whether they have completed
76+ let mut state = ( $( UnsafeCell :: new ( ( $fut, false ) ) , ) * ) ;
7677
77- // Make sure the futures don't panic
78- // if polled after completion, and
79- // store their output separately
80- let mut futures = ( $(
81- ( {
82- let ( $( $pos, ) * state, .. ) = & state;
78+ // Make sure the futures don't panic
79+ // if polled after completion, and
80+ // store their output separately
81+ let mut futures = ( $(
82+ ( {
83+ let ( $( $pos, ) * state, .. ) = & state;
8384
84- poll_fn ( move |cx| {
85- // SAFETY: each future borrows a distinct element
86- // of the tuple
87- let ( fut, done) = unsafe { & mut * state. get ( ) } ;
85+ poll_fn ( move |cx| {
86+ // SAFETY: each future borrows a distinct element
87+ // of the tuple
88+ let ( fut, done) = unsafe { & mut * state. get ( ) } ;
8889
89- if * done {
90- return Poll :: Ready ( None )
91- }
90+ if * done {
91+ return Poll :: Ready ( None )
92+ }
9293
93- // SAFETY: The futures are never moved
94- match unsafe { Pin :: new_unchecked ( fut) . poll ( cx) } {
95- Poll :: Ready ( val) => {
96- * done = true ;
97- Poll :: Ready ( Some ( val) )
94+ // SAFETY: The futures are never moved
95+ match unsafe { Pin :: new_unchecked ( fut) . poll ( cx) } {
96+ Poll :: Ready ( val) => {
97+ * done = true ;
98+ Poll :: Ready ( Some ( val) )
99+ }
100+ Poll :: Pending => Poll :: Pending
98101 }
99- Poll :: Pending => Poll :: Pending
100- }
101- } )
102- } , None ) ,
103- ) * ) ;
102+ } )
103+ } , None ) ,
104+ ) * ) ;
104105
105- poll_fn ( move |cx| {
106- let mut done = true ;
106+ poll_fn ( move |cx| {
107+ let mut done = true ;
107108
108- $(
109- let ( $( $pos, ) * ( fut, out) , .. ) = & mut futures;
109+ $(
110+ let ( $( $pos, ) * ( fut, out) , .. ) = & mut futures;
110111
111- // SAFETY: The futures are never moved
112- match unsafe { Pin :: new_unchecked ( fut) . poll ( cx) } {
113- Poll :: Ready ( Some ( val) ) => * out = Some ( val) ,
114- // the future was already done
115- Poll :: Ready ( None ) => { } ,
116- Poll :: Pending => done = false ,
117- }
118- ) *
112+ // SAFETY: The futures are never moved
113+ match unsafe { Pin :: new_unchecked ( fut) . poll ( cx) } {
114+ Poll :: Ready ( Some ( val) ) => * out = Some ( val) ,
115+ // the future was already done
116+ Poll :: Ready ( None ) => { } ,
117+ Poll :: Pending => done = false ,
118+ }
119+ ) *
119120
120- if done {
121- // Extract all the outputs
122- Poll :: Ready ( ( $( {
123- let ( $( $pos, ) * ( _, val) , .. ) = & mut futures;
124- val. unwrap ( )
125- } ) , * ) )
126- } else {
127- Poll :: Pending
128- }
129- } ) . await
121+ if done {
122+ // Extract all the outputs
123+ Poll :: Ready ( ( $( {
124+ let ( $( $pos, ) * ( _, val) , .. ) = & mut futures;
125+ val. unwrap ( )
126+ } ) , * ) )
127+ } else {
128+ Poll :: Pending
129+ }
130+ } ) . await
131+ }
130132 } }
131133}
0 commit comments