@@ -3,8 +3,6 @@ use crate::fmt;
3
3
use crate :: intrinsics;
4
4
use crate :: mem:: ManuallyDrop ;
5
5
6
- // ignore-tidy-undocumented-unsafe
7
-
8
6
/// A wrapper type to construct uninitialized instances of `T`.
9
7
///
10
8
/// # Initialization invariant
@@ -281,7 +279,7 @@ impl<T> MaybeUninit<T> {
281
279
/// # Examples
282
280
///
283
281
/// ```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 )]
285
283
///
286
284
/// use std::mem::MaybeUninit;
287
285
///
@@ -293,7 +291,7 @@ impl<T> MaybeUninit<T> {
293
291
/// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
294
292
/// unsafe {
295
293
/// 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])
297
295
/// }
298
296
/// }
299
297
///
@@ -303,6 +301,7 @@ impl<T> MaybeUninit<T> {
303
301
#[ unstable( feature = "maybe_uninit_uninit_array" , issue = "none" ) ]
304
302
#[ inline( always) ]
305
303
pub fn uninit_array < const LEN : usize > ( ) -> [ Self ; LEN ] {
304
+ // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
306
305
unsafe { MaybeUninit :: < [ MaybeUninit < T > ; LEN ] > :: uninit ( ) . assume_init ( ) }
307
306
}
308
307
@@ -354,6 +353,7 @@ impl<T> MaybeUninit<T> {
354
353
#[ rustc_diagnostic_item = "maybe_uninit_zeroed" ]
355
354
pub fn zeroed ( ) -> MaybeUninit < T > {
356
355
let mut u = MaybeUninit :: < T > :: uninit ( ) ;
356
+ // SAFETY: `u.as_mut_ptr()` points to allocated memory.
357
357
unsafe {
358
358
u. as_mut_ptr ( ) . write_bytes ( 0u8 , 1 ) ;
359
359
}
@@ -367,10 +367,9 @@ impl<T> MaybeUninit<T> {
367
367
#[ unstable( feature = "maybe_uninit_extra" , issue = "63567" ) ]
368
368
#[ inline( always) ]
369
369
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 ( ) }
374
373
}
375
374
376
375
/// Gets a pointer to the contained value. Reading from this pointer or turning it
@@ -769,9 +768,13 @@ impl<T> MaybeUninit<T> {
769
768
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
770
769
/// really are in an initialized state.
771
770
/// 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" ) ]
773
776
#[ inline( always) ]
774
- pub unsafe fn slice_get_ref ( slice : & [ Self ] ) -> & [ T ] {
777
+ pub unsafe fn slice_assume_init_ref ( slice : & [ Self ] ) -> & [ T ] {
775
778
// SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
776
779
// `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
777
780
// The pointer obtained is valid since it refers to memory owned by `slice` which is a
@@ -786,9 +789,13 @@ impl<T> MaybeUninit<T> {
786
789
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
787
790
/// really are in an initialized state.
788
791
/// 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" ) ]
790
797
#[ 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 ] {
792
799
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a
793
800
// mutable reference which is also guaranteed to be valid for writes.
794
801
unsafe { & mut * ( slice as * mut [ Self ] as * mut [ T ] ) }
@@ -797,14 +804,14 @@ impl<T> MaybeUninit<T> {
797
804
/// Gets a pointer to the first element of the array.
798
805
#[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
799
806
#[ inline( always) ]
800
- pub fn first_ptr ( this : & [ MaybeUninit < T > ] ) -> * const T {
807
+ pub fn slice_as_ptr ( this : & [ MaybeUninit < T > ] ) -> * const T {
801
808
this as * const [ MaybeUninit < T > ] as * const T
802
809
}
803
810
804
811
/// Gets a mutable pointer to the first element of the array.
805
812
#[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
806
813
#[ 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 {
808
815
this as * mut [ MaybeUninit < T > ] as * mut T
809
816
}
810
817
}
0 commit comments