I get confused when learning implementation of Pin in rust.
I understand when a type is not safe to move, it can impl !Unpin. Pin is to prevent others get &mut T from these types, so that they can not be std::mem::swap.
If Pin is designed for !Unpin types, why we can't just call Pin::new on those types?
It gives error like the following. I know I should use Pin::new_unchecked, but why?
struct TestNUnpin {
b: String,
}
impl !Unpin for TestNUnpin {}
// error: the trait `Unpin` is not implemented for `TestNUnpin`
std::pin::Pin::new(&TestNUnpin{b: "b".to_owned()});
My reasoning is:
- Pin is to help !Unpin types
- We can pass !Unpin types to Pin::new to make them unmovable.
- For Unpin types, they can not be pinned, so we can't created by Pin::new