Skip to content

Commit a8c9314

Browse files
committed
remove implicit .await from core::future::join
1 parent d07cef2 commit a8c9314

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

library/core/src/future/join.rs

+51-49
Original file line numberDiff line numberDiff line change
@@ -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
}

library/core/tests/future.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ fn poll_n(val: usize, num: usize) -> PollN {
3232
#[test]
3333
fn test_join() {
3434
block_on(async move {
35-
let x = join!(async { 0 });
35+
let x = join!(async { 0 }).await;
3636
assert_eq!(x, 0);
3737

38-
let x = join!(async { 0 }, async { 1 });
38+
let x = join!(async { 0 }, async { 1 }).await;
3939
assert_eq!(x, (0, 1));
4040

41-
let x = join!(async { 0 }, async { 1 }, async { 2 });
41+
let x = join!(async { 0 }, async { 1 }, async { 2 }).await;
4242
assert_eq!(x, (0, 1, 2));
4343

4444
let x = join!(
@@ -50,8 +50,17 @@ fn test_join() {
5050
poll_n(5, 3),
5151
poll_n(6, 4),
5252
poll_n(7, 1)
53-
);
53+
)
54+
.await;
5455
assert_eq!(x, (0, 1, 2, 3, 4, 5, 6, 7));
56+
57+
let y = String::new();
58+
let x = join!(async {
59+
println!("{}", &y);
60+
1
61+
})
62+
.await;
63+
assert_eq!(x, 1);
5564
});
5665
}
5766

0 commit comments

Comments
 (0)