我对Pin<P>
的要求感到困惑:
一旦定住,其值必须永远保持不变。
我在Reddit的讨论帖子中看到了这个问题,但是阅读完后我仍然感到困惑。
对我来说,如果我们要求指针在Pin<P>
对象存在时被定住,但在Pin<P>
对象被释放后可以重新获得移动它的能力,这似乎更自然。
以一个具体的例子为例:
use std::mem;
use std::pin::Pin;
fn move_pinned_ref<T>(mut a: T, mut b: T) {
unsafe {
let p: Pin<&mut T> = Pin::new_unchecked(&mut a);
// *I prefer* it to mean that the pointee `a` cannot move when `p` is in the scope.
// But *actually* it means the pointee `a` can never move again.
}
mem::swap(&mut a, &mut b);
// *I prefer* it to be valid because `p` has been dropped.
// But we *actually* have violated the pinning API contract that a value,
// once pinned, must remain pinned forever.
}
能否有人指出,我喜欢的设计有什么问题吗?