Skip to content

Commit

Permalink
document remaining unsafety in maybe_uninit.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 4, 2020
1 parent 5d41aff commit 41a047d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use crate::fmt;
use crate::intrinsics;
use crate::mem::ManuallyDrop;

// ignore-tidy-undocumented-unsafe

/// A wrapper type to construct uninitialized instances of `T`.
///
/// # Initialization invariant
Expand Down Expand Up @@ -355,6 +353,7 @@ impl<T> MaybeUninit<T> {
#[rustc_diagnostic_item = "maybe_uninit_zeroed"]
pub fn zeroed() -> MaybeUninit<T> {
let mut u = MaybeUninit::<T>::uninit();
// SAFETY: `u.as_mut_ptr()` points to allocated memory.
unsafe {
u.as_mut_ptr().write_bytes(0u8, 1);
}
Expand All @@ -368,10 +367,9 @@ impl<T> MaybeUninit<T> {
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
#[inline(always)]
pub fn write(&mut self, val: T) -> &mut T {
unsafe {
self.value = ManuallyDrop::new(val);
self.assume_init_mut()
}
*self = MaybeUninit::new(val);
// SAFETY: We just initialized this value.
unsafe { self.assume_init_mut() }
}

/// Gets a pointer to the contained value. Reading from this pointer or turning it
Expand Down

0 comments on commit 41a047d

Please sign in to comment.