Struct Unique
#[repr(transparent)]
pub struct Unique<T: PointeeSized> { pub(in ::ptr::unique) pointer: NonNull<T>, pub(in ::ptr::unique) _marker: PhantomData<T> }
A wrapper around a raw non-null *mut T that indicates that the possessor
of this wrapper owns the referent. Useful for building abstractions like
Box<T>, Vec<T>, String, and HashMap<K, V>.
Unlike *mut T, Unique<T> behaves "as if" it were an instance of T.
It implements Send/Sync if T is Send/Sync. It also implies
the kind of strong aliasing guarantees an instance of T can expect:
the referent of the pointer should not be modified without a unique path to
its owning Unique.
If you're uncertain of whether it's correct to use Unique for your purposes,
consider using NonNull, which has weaker semantics.
Unlike *mut T, the pointer must always be non-null, even if the pointer
is never dereferenced. This is so that enums may use this forbidden value
as a discriminant -- Option<Unique<T>> has the same size as Unique<T>.
However the pointer may still dangle if it isn't dereferenced.
Unlike *mut T, Unique<T> is covariant over T. This should always be correct
for any type which upholds Unique's aliasing requirements.
Fields
pointer: NonNull<T>_marker: PhantomData<T>
Implementations
impl<T: PointeeSized> Unique<T>
const unsafe fn new_unchecked(ptr: *mut T) -> SelfCreates a new
Unique.Safety
ptrmust be non-null.const fn new(ptr: *mut T) -> Option<Self>Creates a new
Uniqueifptris non-null.const fn from_non_null(pointer: NonNull<T>) -> SelfCreate a new
Uniquefrom aNonNullin const context.const fn as_ptr(self) -> *mut TAcquires the underlying
*mutpointer.const fn as_non_null_ptr(self) -> NonNull<T>Acquires the underlying
*mutpointer.const unsafe fn as_ref(&self) -> &TDereferences the content.
The resulting lifetime is bound to self so this behaves "as if" it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use
&*my_ptr.as_ptr().const unsafe fn as_mut(&mut self) -> &mut TMutably dereferences the content.
The resulting lifetime is bound to self so this behaves "as if" it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use
&mut *my_ptr.as_ptr().const fn cast<U>(self) -> Unique<U>Casts to a pointer of another type.
impl<T: Sized> Unique<T>
const fn dangling() -> SelfCreates a new
Uniquethat is dangling, but well-aligned.This is useful for initializing types which lazily allocate, like
Vec::newdoes.Note that the address of the returned pointer may potentially be that of a valid pointer, which means this must not be used as a "not yet initialized" sentinel value. Types that lazily allocate must track initialization by some other means.
Trait Implementations
impl<T, U: PointeeSized> CoerceUnsized<Unique<U>> for Unique<T>
where
T: Unsize<U> + PointeeSized,
impl<T, U: PointeeSized> DispatchFromDyn<Unique<U>> for Unique<T>
where
T: Unsize<U> + PointeeSized,
impl<T: PointeeSized> Clone for Unique<T>
fn clone(&self) -> Self
impl<T: PointeeSized> Copy for Unique<T>
impl<T: PointeeSized> Debug for Unique<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<T: PointeeSized> From<&mut T> for Unique<T>
fn from(reference: &mut T) -> SelfConverts a
&mut Tto aUnique<T>.This conversion is infallible since references cannot be null.
impl<T: PointeeSized> From<NonNull<T>> for Unique<T>
fn from(pointer: NonNull<T>) -> SelfConverts a
NonNull<T>to aUnique<T>.This conversion is infallible since
NonNullcannot be null.
impl<T: PointeeSized> Pointer for Unique<T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result