Trait uninit::extension_traits::MaybeUninitExt
source · pub trait MaybeUninitExt {
type T: ?Sized;
// Required methods
fn as_ptr(&self) -> *const Self::T;
fn as_mut_ptr(&mut self) -> *mut Self::T;
unsafe fn assume_init_by_ref(&self) -> &Self::T;
unsafe fn assume_init_by_mut(&mut self) -> &mut Self::T;
fn from_ref(init_ref: &Self::T) -> &Self;
}
Expand description
Extension trait providing tranformations between init and uninit.
from_mut
?
This is provided by AsMaybeUninit::as_mut_uninit
.
The conversion from &mut T
to &mut MaybeUninit<T>
is unsafe
, since there
are restrictions on what safe code can do with the output. These are the same
requirements as Out::as_mut_uninit
.
Specifically, nothing must overwrite an initialized pointee
with MaybeUninit::uninit()
data, i.e., an
uninitialised (and thus garbage) value, even though the API of &mut T
allows this safely. This is similar to Pin::get_mut_unchecked
.
The correct way to convert &mut T
into a maybe-initialized reference is through
the &out
reference abstraction.
Required Associated Types§
Required Methods§
sourcefn as_mut_ptr(&mut self) -> *mut Self::T
fn as_mut_ptr(&mut self) -> *mut Self::T
Gets a raw mutable pointer to the inner type.
sourceunsafe fn assume_init_by_ref(&self) -> &Self::T
unsafe fn assume_init_by_ref(&self) -> &Self::T
Converts a &MaybeUninit<_>
to a & _
.
Safety
Don’t be lured by the reference: this has the same safety requirements
that .assume_init
does. Mainly:
- The
Self::T
thatself
points to must be initialized.
sourceunsafe fn assume_init_by_mut(&mut self) -> &mut Self::T
unsafe fn assume_init_by_mut(&mut self) -> &mut Self::T
Converts a &mut MaybeUninit<_>
to a &mut _
.
Safety
Don’t be lured by the mut
reference: this has the same safety
requirements that .assume_init
does.
Mainly:
- The
Self::T
thatself
points to must be initialized.