Sub topic 9
Pin: promising a value will not move
This chapter assumes you know ownership and borrowing, how moves work, raw pointers at a basic level, and what a Future is. The previous chapter showed why a self-referential value breaks if it moves. Pin is the type that prevents that move.
What Pin is
Pin<P> is a wrapper around a pointer type P, such as &mut T, Box<T>, or Rc<T>. It does not pin the pointer. It pins the value the pointer points to. While a value is pinned, safe code cannot move it or get a &mut T to it, so its address stays the same until it is dropped.
Moving a value means a bitwise copy to a new address. std::mem::swap and std::mem::replace do this through a &mut T. If safe code could never obtain a &mut T to a pinned value, it could never move that value.
Why poll takes Pin<&mut Self>
The signature of Future::poll is:
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>The compiler-generated state machine for an async block may hold references to its own fields across an .await. The first call to poll promises that the future will not move again. That promise is the Pin<&mut Self> receiver.
The API in short
| Operation | Safe? | Meaning |
|---|---|---|
| `Pin::new(&mut x)` | Yes, only if `T: Unpin` | Pin a value that does not care about moving |
| `Box::pin(x)` | Yes | Move `x` to the heap and pin it there |
| `Pin::as_mut(&mut self)` | Yes | Reborrow a `Pin<&mut T>` without consuming it |
| `Pin::get_mut` | Yes, only if `T: Unpin` | Get `&mut T` back |
| `Pin::get_unchecked_mut` | `unsafe` | Get `&mut T`. You promise not to move the value. |
Using it
as_mut matters because poll consumes its Pin<&mut Self>. To poll in a loop, reborrow each time:
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
fn poll_twice<F: Future>(mut fut: Pin<&mut F>, cx: &mut Context<'_>) -> (Poll<F::Output>, Poll<F::Output>) {
let first = fut.as_mut().poll(cx);
let second = fut.as_mut().poll(cx);
(first, second)
}Without as_mut, the first poll would move fut, and the second call would not compile. Polling a future again after it returned Ready may panic, so real code should not do what this example does.
Common errors
- "the trait
Unpinis not implemented". You calledPin::newon a future from anasyncblock. UseBox::pinorstd::pin::pin!instead. - Moving after pinning. A
Pin<Box<T>>can be moved freely, because only the heap value is pinned. APin<&mut T>cannot give you theTback by value. - Assuming Pin forbids all mutation. You can still mutate a pinned value through
Pin<&mut T>methods. What is forbidden is moving it.
Exercise
Why does Future::poll take Pin<&mut Self> instead of &mut Self?
Show answer
With &mut Self, safe code could call mem::swap and move the future after it started running. A future from an async block may hold references into itself, and a move would leave them dangling. Pin<&mut Self> means safe code cannot move the value after it is pinned.
Exercise
Why do you call fut.as_mut().poll(cx) instead of fut.poll(cx) inside a loop?
Show answer
poll takes self: Pin<&mut Self> by value, so calling it on fut would consume fut. as_mut creates a shorter-lived Pin<&mut F> reborrow, so fut stays usable on the next iteration.