Struct possibly_uninit::Out[][src]

pub struct Out<'a, T>(_);

Mutable reference wrapper that only allows writing valid values to the memory location.

This is used for values that might be borowed from either T or MaybeUninit<T>.

One would normally expect this to be &mut MaybeUninit<T>, however, that wouldn’t be sound. Consider this code:

use core::mem::MaybeUninit;
 
fn cast_mut<T>(val: &mut T) -> &mut MaybeUninit<T> {
    unsafe {
        core::mem::transmute(val)
    }
}
 
// No unsafe code here
fn main() {
    let mut message = "Hello world!".to_string();
    core::mem::replace(cast_mut(&mut message), MaybeUninit::uninit());
    println!("This is now garbage: {}", message);
}

The code above triggers UB. Thus the users of the reference must be prevented from writing invalid values into the memory location. That’s only possible by creating a newtype like this one.

While the newtype itself doesn’t track initializedness, so its use may be limited in safe code, it’s a base building block allowing sound implementations of wrappers tracking initializedness.

Implementations

impl<'a, T> Out<'a, T>[src]

pub fn write(self, value: T) -> &'a mut T[src]

Writes a valid value to given memory location, initializing it.

pub fn write_mut(&mut self, value: T) -> &mut T[src]

Writes a valid value to given memory location, initializing it.

This method is same as write, it just reborows the reference instead of consuming it.

pub unsafe fn into_assume_init(self) -> &'a mut T[src]

Turns the wrapper into reference assuming the value was initialized.

Safety

Calling this function if no value was written is UB.

pub fn as_non_null(&mut self) -> NonNull<T>[src]

Returns mutable non-null pointer to the value.

pub fn as_mut_ptr(&mut self) -> *mut T[src]

Returns mutable raw pointer to the value.

Note that this is in fact non-null, it’s just sometimes more useful than as_non_null.

pub fn write_zeroes(&mut self) -> &mut T where
    T: ZeroValid
[src]

Overwrites the value with all zeroes.

pub fn into_zeroed(self) -> &'a mut T where
    T: ZeroValid
[src]

Overwrites the value with all zeroes.

Consumes the referenceto to preserve the lifetime

Trait Implementations

impl<'a, T> BorrowOut<T> for Out<'a, T>[src]

impl<'a, T> BorrowUninit<T> for Out<'a, T>[src]

impl<'a, T> From<&'a mut MaybeUninit<T>> for Out<'a, T>[src]

impl<'a, T> From<&'a mut T> for Out<'a, T>[src]

Auto Trait Implementations

impl<'a, T> Send for Out<'a, T> where
    T: Send

impl<'a, T> Sync for Out<'a, T> where
    T: Sync

impl<'a, T> Unpin for Out<'a, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AnyValid for T[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> PtrCount for T[src]

type Item = T

Either Self or type of item in the slice.

impl<T> TakeItem<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.