Skip to content

Commit 0e0a47d

Browse files
committed
document remaining unsafety in maybe_uninit.rs
1 parent dddc5ff commit 0e0a47d

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

library/core/src/mem/maybe_uninit.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fmt;
33
use crate::intrinsics;
44
use crate::mem::ManuallyDrop;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
/// A wrapper type to construct uninitialized instances of `T`.
97
///
108
/// # Initialization invariant
@@ -355,6 +353,7 @@ impl<T> MaybeUninit<T> {
355353
#[rustc_diagnostic_item = "maybe_uninit_zeroed"]
356354
pub fn zeroed() -> MaybeUninit<T> {
357355
let mut u = MaybeUninit::<T>::uninit();
356+
// SAFETY: `u.as_mut_ptr()` points to allocated memory.
358357
unsafe {
359358
u.as_mut_ptr().write_bytes(0u8, 1);
360359
}
@@ -368,10 +367,9 @@ impl<T> MaybeUninit<T> {
368367
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
369368
#[inline(always)]
370369
pub fn write(&mut self, val: T) -> &mut T {
371-
unsafe {
372-
self.value = ManuallyDrop::new(val);
373-
self.assume_init_mut()
374-
}
370+
*self = MaybeUninit::new(val);
371+
// SAFETY: We just initialized this value.
372+
unsafe { self.assume_init_mut() }
375373
}
376374

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

0 commit comments

Comments
 (0)