@@ -3,8 +3,6 @@ use crate::fmt;
33use crate :: intrinsics;
44use crate :: mem:: ManuallyDrop ;
55
6- // ignore-tidy-undocumented-unsafe
7-
86/// A wrapper type to construct uninitialized instances of `T`.
97///
108/// # Initialization invariant
@@ -281,7 +279,7 @@ impl<T> MaybeUninit<T> {
281279 /// # Examples
282280 ///
283281 /// ```no_run
284- /// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice_assume_init )]
282+ /// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice )]
285283 ///
286284 /// use std::mem::MaybeUninit;
287285 ///
@@ -293,7 +291,7 @@ impl<T> MaybeUninit<T> {
293291 /// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
294292 /// unsafe {
295293 /// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
296- /// MaybeUninit::slice_get_ref (&buf[..len])
294+ /// MaybeUninit::slice_assume_init_ref (&buf[..len])
297295 /// }
298296 /// }
299297 ///
@@ -303,6 +301,7 @@ impl<T> MaybeUninit<T> {
303301 #[ unstable( feature = "maybe_uninit_uninit_array" , issue = "none" ) ]
304302 #[ inline( always) ]
305303 pub fn uninit_array < const LEN : usize > ( ) -> [ Self ; LEN ] {
304+ // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
306305 unsafe { MaybeUninit :: < [ MaybeUninit < T > ; LEN ] > :: uninit ( ) . assume_init ( ) }
307306 }
308307
@@ -354,6 +353,7 @@ impl<T> MaybeUninit<T> {
354353 #[ rustc_diagnostic_item = "maybe_uninit_zeroed" ]
355354 pub fn zeroed ( ) -> MaybeUninit < T > {
356355 let mut u = MaybeUninit :: < T > :: uninit ( ) ;
356+ // SAFETY: `u.as_mut_ptr()` points to allocated memory.
357357 unsafe {
358358 u. as_mut_ptr ( ) . write_bytes ( 0u8 , 1 ) ;
359359 }
@@ -367,10 +367,9 @@ impl<T> MaybeUninit<T> {
367367 #[ unstable( feature = "maybe_uninit_extra" , issue = "63567" ) ]
368368 #[ inline( always) ]
369369 pub fn write ( & mut self , val : T ) -> & mut T {
370- unsafe {
371- self . value = ManuallyDrop :: new ( val) ;
372- self . assume_init_mut ( )
373- }
370+ * self = MaybeUninit :: new ( val) ;
371+ // SAFETY: We just initialized this value.
372+ unsafe { self . assume_init_mut ( ) }
374373 }
375374
376375 /// Gets a pointer to the contained value. Reading from this pointer or turning it
@@ -769,9 +768,13 @@ impl<T> MaybeUninit<T> {
769768 /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
770769 /// really are in an initialized state.
771770 /// Calling this when the content is not yet fully initialized causes undefined behavior.
772- #[ unstable( feature = "maybe_uninit_slice_assume_init" , issue = "none" ) ]
771+ ///
772+ /// See [`assume_init_ref`] for more details and examples.
773+ ///
774+ /// [`assume_init_ref`]: MaybeUninit::assume_init_ref
775+ #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
773776 #[ inline( always) ]
774- pub unsafe fn slice_get_ref ( slice : & [ Self ] ) -> & [ T ] {
777+ pub unsafe fn slice_assume_init_ref ( slice : & [ Self ] ) -> & [ T ] {
775778 // SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
776779 // `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
777780 // The pointer obtained is valid since it refers to memory owned by `slice` which is a
@@ -786,9 +789,13 @@ impl<T> MaybeUninit<T> {
786789 /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
787790 /// really are in an initialized state.
788791 /// Calling this when the content is not yet fully initialized causes undefined behavior.
789- #[ unstable( feature = "maybe_uninit_slice_assume_init" , issue = "none" ) ]
792+ ///
793+ /// See [`assume_init_mut`] for more details and examples.
794+ ///
795+ /// [`assume_init_mut`]: MaybeUninit::assume_init_mut
796+ #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
790797 #[ inline( always) ]
791- pub unsafe fn slice_get_mut ( slice : & mut [ Self ] ) -> & mut [ T ] {
798+ pub unsafe fn slice_assume_init_mut ( slice : & mut [ Self ] ) -> & mut [ T ] {
792799 // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
793800 // mutable reference which is also guaranteed to be valid for writes.
794801 unsafe { & mut * ( slice as * mut [ Self ] as * mut [ T ] ) }
@@ -797,14 +804,14 @@ impl<T> MaybeUninit<T> {
797804 /// Gets a pointer to the first element of the array.
798805 #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
799806 #[ inline( always) ]
800- pub fn first_ptr ( this : & [ MaybeUninit < T > ] ) -> * const T {
807+ pub fn slice_as_ptr ( this : & [ MaybeUninit < T > ] ) -> * const T {
801808 this as * const [ MaybeUninit < T > ] as * const T
802809 }
803810
804811 /// Gets a mutable pointer to the first element of the array.
805812 #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
806813 #[ inline( always) ]
807- pub fn first_ptr_mut ( this : & mut [ MaybeUninit < T > ] ) -> * mut T {
814+ pub fn slice_as_mut_ptr ( this : & mut [ MaybeUninit < T > ] ) -> * mut T {
808815 this as * mut [ MaybeUninit < T > ] as * mut T
809816 }
810817}
0 commit comments