Why does a pinned future implement Unpin?

345 views Asked by At
fn test<T: Unpin>(t: T) {

}

fn main() {
    let a = 1;
    let b = async {
        1
    };
    test(b) // the trait `Unpin` is not implemented for `[async block]`
}

Future is not Unpin, so why does pinning it (pin_mut, Box::pin, etc., which wrap it with Pin<..>) make it implement Unpin?

1

There are 1 answers

3
Jonas Fassbender On

Because Pin<T> implements Unpin if the inner value T implements Unpin, which is the case for &mut F (from pin_mut!) and Box<F> (from Box::pin) and all other pointer types in the standard library. Pointers implement Unpin, because they can be moved safely, since moving the pointer does not affect the pointee. See the std::pin module's documentation.

A great read on how pinning in Rust works and why we need to pin Future types can be found in the async book.