Skip to content

Commit

Permalink
show how to set with ptr::write
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 24, 2019
1 parent a5e2d0c commit be8d728
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/libcore/mem.rs
Expand Up @@ -1191,7 +1191,8 @@ impl<T> MaybeUninit<T> {
}

/// Sets the value of the `MaybeUninit<T>`. This overwrites any previous value
/// without dropping it. For your convenience, this also returns a mutable
/// without dropping it, so be careful not to use this twice unless you want to
/// skip running the destructor. For your convenience, this also returns a mutable
/// reference to the (now safely initialized) contents of `self`.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
Expand All @@ -1214,7 +1215,7 @@ impl<T> MaybeUninit<T> {
/// use std::mem::MaybeUninit;
///
/// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
/// x.set(vec![0,1,2]);
/// unsafe { x.as_mut_ptr().write(vec![0,1,2]); }
/// // Create a reference into the `MaybeUninit<T>`. This is okay because we initialized it.
/// let x_vec = unsafe { &*x.as_ptr() };
/// assert_eq!(x_vec.len(), 3);
Expand Down Expand Up @@ -1250,7 +1251,7 @@ impl<T> MaybeUninit<T> {
/// use std::mem::MaybeUninit;
///
/// let mut x = MaybeUninit::<Vec<u32>>::uninitialized();
/// x.set(vec![0,1,2]);
/// unsafe { x.as_mut_ptr().write(vec![0,1,2]); }
/// // Create a reference into the `MaybeUninit<Vec<u32>>`.
/// // This is okay because we initialized it.
/// let x_vec = unsafe { &mut *x.as_mut_ptr() };
Expand Down Expand Up @@ -1295,7 +1296,7 @@ impl<T> MaybeUninit<T> {
/// use std::mem::MaybeUninit;
///
/// let mut x = MaybeUninit::<bool>::uninitialized();
/// x.set(true);
/// unsafe { x.as_mut_ptr().write(true); }
/// let x_init = unsafe { x.into_initialized() };
/// assert_eq!(x_init, true);
/// ```
Expand Down

0 comments on commit be8d728

Please sign in to comment.