from_maybeuninit

Function from_maybeuninit 

Source
pub unsafe fn from_maybeuninit<T>(
    slot: &mut MaybeUninit<T>,
) -> OwningSlice<'_, T>
Expand description

Create a OwningSlice<'a, T> with a length and capacity of 1 from a &'a mut MaybeUninit<T>>.

§Safety

The T must be initialized, and dropping or removing the element from the OwningSlice leaves the MaybeUninit semantically without a value, see MaybeUninit::assume_init_mut and MaybeUninit::assume_init_drop.

§Examples:

use noop_allocator::owning_slice;
let mut buf: MaybeUninit<String> = MaybeUninit::new("Hello, world!".to_string());
let mut vec = unsafe { owning_slice::from_maybeuninit(&mut buf) };
assert_eq!(vec, ["Hello, world!"]);
assert_eq!(vec.capacity(), 1);
assert_eq!(vec.pop().as_deref(), Some("Hello, world!"));
vec.push("Hi!".to_string());
assert_eq!(vec, ["Hi!"]);