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§

source

type T: ?Sized

The inner type that may be initialized.

  • For MaybeUninit<T: Sized>, this is T.
  • For [MaybeUninit<T>], this is [T].

Required Methods§

source

fn as_ptr(&self) -> *const Self::T

Gets a raw pointer to the inner type.

source

fn as_mut_ptr(&mut self) -> *mut Self::T

Gets a raw mutable pointer to the inner type.

source

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 that self points to must be initialized.
source

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 that self points to must be initialized.
source

fn from_ref(init_ref: &Self::T) -> &Self

👎Deprecated: Use AsUninit::as_ref_uninit instead

Downgrades a & _ to a &MaybeUninit<_>. Rarely useful.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T> MaybeUninitExt for [MaybeUninit<T>]

§

type T = [T]

source§

fn as_ptr(&self) -> *const Self::T

source§

fn as_mut_ptr(&mut self) -> *mut Self::T

source§

unsafe fn assume_init_by_ref(&self) -> &Self::T

source§

unsafe fn assume_init_by_mut(&mut self) -> &mut Self::T

source§

fn from_ref(slice: &Self::T) -> &Self

👎Deprecated: Use AsUninit::as_ref_uninit instead

Implementors§

source§

impl<T> MaybeUninitExt for MaybeUninit<T>

§

type T = T