The implementation is very easy:
pub fn pin(x: T) -> Pin<Box<T>> {
(box x).into()
}
But I wonder what actually happens if T is !Unpin (it has self reference etc) ?
How does box x fix up those self references?
The implementation is very easy:
pub fn pin(x: T) -> Pin<Box<T>> {
(box x).into()
}
But I wonder what actually happens if T is !Unpin (it has self reference etc) ?
How does box x fix up those self references?
It doesn't.
An important realization about Unpin is that even for !Unpin types, as long as you haven't pinned them, you can move them freely.
In this case, we know the value has not been pinned, because it was moved to us as a parameter (and this is forbidden for pinned !Unpin types). So we can move it freely, until we create the Pin.
This rule make sense if we think about e.g. futures: before the future started executing (and thus was not yet pinned), it cannot have self-references because the code that is responsible to create them is inside the future, so it is yet to be run.