This commit is contained in:
Philipp Oppermann
2020-03-31 16:41:34 +02:00
parent 8aea96a4e3
commit a98de2a76c

View File

@@ -703,7 +703,7 @@ As we already saw in this post, the [`Future::poll`] method uses pinning in form
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>
```
The reason that this method takes `self: Pin<&mut Self>` instead of the normal `&mut self` is that future instances created from async/await are often self-referential, as we saw [above][self-ref-async-await]. By wrapping `Self` into `Pin` and letting the compiler opt-out of `Unpin` for self-referentual futures generated from async/await, it is guaranteed that the futures are not moved in memory between `poll` calls. This ensures that all internal references are still valid.
The reason that this method takes `self: Pin<&mut Self>` instead of the normal `&mut self` is that future instances created from async/await are often self-referential, as we saw [above][self-ref-async-await]. By wrapping `Self` into `Pin` and letting the compiler opt-out of `Unpin` for self-referential futures generated from async/await, it is guaranteed that the futures are not moved in memory between `poll` calls. This ensures that all internal references are still valid.
[self-ref-async-await]: @/second-edition/posts/12-async-await/index.md#self-referential-structs