Question about the `Unpin` trait

Do Vec<T> require T: Unpin once the Unpin trait being stabilized?

I ask because Vec<T> may move things inside when it need to expand for capacity.

No, because that would be a breaking change. (Adding trait bounds to an existing type is a breaking change). Due to Rust’s stability guarantees there will be no breaking change until Rust 2.0 (if that ever comes).

Ok.

I am doing experiments on a Bin type (worked like a secondary stack so you can put anything in it, and only get them back in reverse order).

Playground

As you can see I use a Vec<u8> to store the data, and it will move its content when necessary (expecially, when I call reserve). Now, to make the method push safe do I need to require T: Unpin so I can move them freely?

Pin is designed to be backwards compatible, and thus there are no types that are always illegal to move. Rather, an unsafe API that provides a Pin<&mut T> ‘reference’ must ensure that that the contained value is never moved again – which includes ensuring that safe code can’t directly access the wrapped &mut T reference, because that would allow moving using mem::replace etc. However, that doesn’t apply to Unpin types, for which you can convert Pin<&mut T> to &mut T using Pin::get_mut.

Since your code never vends Pin references, it doesn’t have to worry about this; no need for T: Unpin.

2 Likes

Ok thanks.

Here is the final update.

Playground