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?
Because
Pin<T>implementsUnpinif the inner valueTimplementsUnpin, which is the case for&mut F(frompin_mut!) andBox<F>(fromBox::pin) and all other pointer types in the standard library. Pointers implementUnpin, because they can be moved safely, since moving the pointer does not affect the pointee. See thestd::pinmodule's documentation.A great read on how pinning in Rust works and why we need to pin
Futuretypes can be found in the async book.