Struct UnsafePinned
#[repr(transparent)]
pub struct UnsafePinned<T: ?Sized> { pub(in ::pin::unsafe_pinned) value: UnsafeCell<T> }
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.
Fields
value: UnsafeCell<T>
Implementations
impl<T> UnsafePinned<T>
const fn new(value: T) -> SelfConstructs a new instance of
UnsafePinnedwhich will wrap the specified value.All access to the inner value through
&UnsafePinned<T>or&mut UnsafePinned<T>orPin<&mut UnsafePinned<T>>requiresunsafecode.const fn into_inner(self) -> TUnwraps the value, consuming this
UnsafePinned.
impl<T: ?Sized> UnsafePinned<T>
const fn get_mut_pinned(self: Pin<&mut Self>) -> *mut TGet read-write access to the contents of a pinned
UnsafePinned.const fn get_mut_unchecked(&mut self) -> *mut TGet read-write access to the contents of an
UnsafePinned.You should usually be using
get_mut_pinnedinstead to explicitly track the fact that this memory is "pinned" due to there being aliases.const fn get(&self) -> *mut TGet 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
UnsafeCellfor more discussion and caveats.use UnsafePinned; unsafeconst fn raw_get(this: *const Self) -> *mut TGets an immutable pointer to the wrapped value.
The difference from
getis that this function accepts a raw pointer, which is useful to avoid the creation of temporary references.const fn raw_get_mut(this: *mut Self) -> *mut TGets a mutable pointer to the wrapped value.
The difference from
get_mut_pinnedandget_mut_uncheckedis that this function accepts a raw pointer, which is useful to avoid the creation of temporary references.
Trait Implementations
impl<T> From<T> for UnsafePinned<T>
fn from(value: T) -> SelfCreates a new
UnsafePinned<T>containing the given value.
impl<T: ?Sized + Sync> Sync for UnsafePinned<T>
impl<T: ?Sized> !Unpin for UnsafePinned<T>
impl<T: ?Sized> Debug for UnsafePinned<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafePinned<U>> for UnsafePinned<T>
impl<T: Default> Default for UnsafePinned<T>
fn default() -> SelfCreates an
UnsafePinned, with theDefaultvalue for T.
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafePinned<U>> for UnsafePinned<T>
impl<T: PointeeSized> !UnsafeUnpin for UnsafePinned<T>
Auto Trait Implementations
impl<T> !Freeze for UnsafePinned<T>
impl<T> !RefUnwindSafe for UnsafePinned<T>
impl<T> Send for UnsafePinned<T>
where
UnsafeCell<T>: Send,
T: ?Sized,
impl<T> UnwindSafe for UnsafePinned<T>
where
UnsafeCell<T>: UnwindSafe,
T: ?Sized,
Blanket Implementations
impl<T> Any for UnsafePinned<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for UnsafePinned<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for UnsafePinned<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for UnsafePinned<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> From<never> for UnsafePinned<T>
fn from(t: never) -> T
impl<T> SizeHint for UnsafePinned<T>
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for UnsafePinned<T>
impl<T, U> Into<U> for UnsafePinned<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for UnsafePinned<T>
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for UnsafePinned<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>