Struct UnsafePinned
pub struct UnsafePinned<T: ?Sized> { /* private fields */ }🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
Expand description
This type provides a way to entirely opt-out of typical aliasing rules;
specifically, &mut UnsafePinned<T> is not guaranteed to be a unique pointer.
This also subsumes the effects of UnsafeCell, i.e., &UnsafePinned<T> may point to data
that is being mutated.
However, even if you define your type like pub struct Wrapper(UnsafePinned<...>), it is still
very risky to have an &mut Wrapper that aliases anything else. Many functions that work
generically on &mut T assume that the memory that stores T is uniquely owned (such as
mem::swap). In other words, while having aliasing with &mut Wrapper is not immediate
Undefined Behavior, it is still unsound to expose such a mutable reference to code you do not
control! Techniques such as pinning via Pin are needed to ensure soundness.
Similar to UnsafeCell, UnsafePinned will not usually show up in
the public API of a library. It is an internal implementation detail of libraries that need to
support aliasing mutable references.
This type blocks niches the same way UnsafeCell does.
Source§
Source§
Source 🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
unsafe_pinned #125735)Get read-write access to the contents of a pinned UnsafePinned.
Source 🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
unsafe_pinned #125735)Get read-write access to the contents of an UnsafePinned.
You should usually be using get_mut_pinned instead to explicitly track the fact that this
memory is “pinned” due to there being aliases.
Source 🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
unsafe_pinned #125735)Get mutable access to the contents of a shared UnsafePinned.
This can be cast to a pointer of any kind. When creating references, you must uphold the
aliasing rules; see UnsafeCell for more discussion and caveats.
#![feature(unsafe_pinned)]
use std::pin::UnsafePinned;
unsafe {
let mut x = UnsafePinned::new(0);
let ptr = x.get();
x.get_mut_unchecked().write(1);
assert_eq!(ptr.read(), 1);
}Source 🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
unsafe_pinned #125735)Gets an immutable pointer to the wrapped value.
The difference from get is that this function accepts a raw pointer, which is useful to
avoid the creation of temporary references.
Source 🔬This is a nightly-only experimental API. (unsafe_pinned #125735)
unsafe_pinned #125735)Gets a mutable pointer to the wrapped value.
The difference from get_mut_pinned and get_mut_unchecked is that this function
accepts a raw pointer, which is useful to avoid the creation of temporary references.