You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/await-expr.md
+13-8
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,20 @@
4
4
> _AwaitExpression_ :\
5
5
> [_Expression_]`.``await`
6
6
7
-
*Await expressions* suspend the current computation until the given future is ready to produce a value.
8
-
The syntax for an await expression is an expression with a type that implements the [Future] trait, called the *future operand*, then the token `.`, and then the `await` keyword.
7
+
An `await` expression is a syntactic construct for suspending a computation
8
+
provided by an implementation of `std::future::IntoFuture` until the given
9
+
future is ready to produce a value.
10
+
The syntax for an await expression is an expression with a type that implements the [`IntoFuture`] trait, called the *future operand*, then the token `.`, and then the `await` keyword.
9
11
Await expressions are legal only within an [async context], like an [`async fn`] or an [`async` block].
10
12
11
13
More specifically, an await expression has the following effect.
12
14
13
-
1. Evaluate the future operand to a [future]`tmp`;
14
-
2. Pin `tmp` using [`Pin::new_unchecked`];
15
-
3. This pinned future is then polled by calling the [`Future::poll`] method and passing it the current [task context](#task-context);
16
-
3. If the call to `poll` returns [`Poll::Pending`], then the future returns `Poll::Pending`, suspending its state so that, when the surrounding async context is re-polled,execution returns to step 2;
17
-
4. Otherwise the call to `poll` must have returned [`Poll::Ready`], in which case the value contained in the [`Poll::Ready`] variant is used as the result of the `await` expression itself.
15
+
1. Create a future by calling [`IntoFuture::into_future`] on the future operand.
16
+
2. Evaluate the future to a [future]`tmp`;
17
+
3. Pin `tmp` using [`Pin::new_unchecked`];
18
+
4. This pinned future is then polled by calling the [`Future::poll`] method and passing it the current [task context](#task-context);
19
+
5. If the call to `poll` returns [`Poll::Pending`], then the future returns `Poll::Pending`, suspending its state so that, when the surrounding async context is re-polled,execution returns to step 2;
20
+
6. Otherwise the call to `poll` must have returned [`Poll::Ready`], in which case the value contained in the [`Poll::Ready`] variant is used as the result of the `await` expression itself.
18
21
19
22
> **Edition differences**: Await expressions are only available beginning with Rust 2018.
20
23
@@ -29,7 +32,7 @@ Effectively, an await expression is roughly equivalent to the following non-norm
29
32
30
33
<!-- ignore: example expansion -->
31
34
```rust,ignore
32
-
match future_operand {
35
+
match operand.into_future() {
33
36
mut pinned => loop {
34
37
let mut pin = unsafe { Pin::new_unchecked(&mut pinned) };
35
38
match Pin::future::poll(Pin::borrow(&mut pin), &mut current_context) {
@@ -53,3 +56,5 @@ The variable `current_context` refers to the context taken from the async enviro
0 commit comments