Skip to content

Commit a92be0f

Browse files
authored
Merge pull request #1233 from yoshuawuyts/into-future
Add `IntoFuture::into_future` desugaring
2 parents 8531348 + be45f8f commit a92be0f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Diff for: src/expressions/await-expr.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
> _AwaitExpression_ :\
55
>    [_Expression_] `.` `await`
66
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.
911
Await expressions are legal only within an [async context], like an [`async fn`] or an [`async` block].
1012

1113
More specifically, an await expression has the following effect.
1214

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.
1821

1922
> **Edition differences**: Await expressions are only available beginning with Rust 2018.
2023
@@ -29,7 +32,7 @@ Effectively, an await expression is roughly equivalent to the following non-norm
2932

3033
<!-- ignore: example expansion -->
3134
```rust,ignore
32-
match future_operand {
35+
match operand.into_future() {
3336
mut pinned => loop {
3437
let mut pin = unsafe { Pin::new_unchecked(&mut pinned) };
3538
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
5356
[`poll::Ready`]: ../../std/task/enum.Poll.html#variant.Ready
5457
[async context]: ../expressions/block-expr.md#async-context
5558
[future]: ../../std/future/trait.Future.html
59+
[`IntoFuture`]: ../../std/future/trait.IntoFuture.html
60+
[`IntoFuture::into_future`]: ../../std/future/trait.IntoFuture.html#tymethod.into_future

0 commit comments

Comments
 (0)