Trait AsMaybeUninit

Source
pub unsafe trait AsMaybeUninit {
    type Uninit: ?Sized + MaybeUninitExt<T = Self>;
    type SizedPart: Sized;

    // Required methods
    fn as_ref_uninit(&self) -> &Self::Uninit;
    unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit;
    unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit;
    unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit;
}
Expand description

Converts a reference into its maybe-initialized form.

This trait allows you to use a unified API for MaybeUninit on sized and unsized types.

You probably don’t need to implement this trait yourself: it is automatically implemented for all T: Sized and [T].

§Safety

  • Uninit must have the same layout as Self but with no requirement on its contents. See the MaybeUninit layout.
  • All methods return pointers that point to the same memory as their input.

Required Associated Types§

Source

type Uninit: ?Sized + MaybeUninitExt<T = Self>

This type in its maybe-uninitialized form.

Source

type SizedPart: Sized

The largest Sized element in Self, used to check for the absence of drop glue via a Copy bound.

For Self: Sized, this equals Self, but for [T] it is T.

Required Methods§

Source

fn as_ref_uninit(&self) -> &Self::Uninit

Converts a &self to its maybe-initialized equivalent.

Source

unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit

Converts a &mut T to its maybe-initialized equivalent.

This converts a read-write-valid-values-only reference to a write-anything reference.

§Safety

The same requirements as Out::as_mut_uninit. Care should be taken with usage of the output - uninitialized garbage must not be written.

Source

unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit

Converts a raw pointer to a reference to maybe-uninit.

§Safety

This has the same requirements as &*(raw as *const Self::Uninit).

  • raw must point to a readable allocated memory for 'a for the size of T
  • raw must be aligned for T
Source

unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit

Converts a raw mutable pointer to a mutable reference to maybe-uninit.

§Safety

This has the same requirements as &mut *(raw as *mut Self::Uninit).

  • raw must point to a readable and writeable allocated object for 'a for the size of T
  • The pointer must not alias any other mutable references for 'a
  • raw must be aligned for T

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> AsMaybeUninit for [T]

Source§

type Uninit = [MaybeUninit<T>]

Source§

type SizedPart = T

Source§

fn as_ref_uninit(&self) -> &Self::Uninit

Source§

unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit

Source§

unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit

Source§

unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit

Implementors§