@@ -494,8 +494,12 @@ impl<T> MaybeUninit<T> {
494494 #[ inline( always) ]
495495 #[ rustc_diagnostic_item = "assume_init" ]
496496 pub unsafe fn assume_init ( self ) -> T {
497- intrinsics:: assert_inhabited :: < T > ( ) ;
498- ManuallyDrop :: into_inner ( self . value )
497+ // SAFETY: the caller must guarantee that `self` is initialized.
498+ // This also means that `self` must be a `value` variant.
499+ unsafe {
500+ intrinsics:: assert_inhabited :: < T > ( ) ;
501+ ManuallyDrop :: into_inner ( self . value )
502+ }
499503 }
500504
501505 /// Reads the value from the `MaybeUninit<T>` container. The resulting `T` is subject
@@ -558,8 +562,12 @@ impl<T> MaybeUninit<T> {
558562 #[ unstable( feature = "maybe_uninit_extra" , issue = "63567" ) ]
559563 #[ inline( always) ]
560564 pub unsafe fn read ( & self ) -> T {
561- intrinsics:: assert_inhabited :: < T > ( ) ;
562- self . as_ptr ( ) . read ( )
565+ // SAFETY: the caller must guarantee that `self` is initialized.
566+ // Reading from `self.as_ptr()` is safe since `self` should be initialized.
567+ unsafe {
568+ intrinsics:: assert_inhabited :: < T > ( ) ;
569+ self . as_ptr ( ) . read ( )
570+ }
563571 }
564572
565573 /// Gets a shared reference to the contained value.
@@ -620,8 +628,12 @@ impl<T> MaybeUninit<T> {
620628 #[ unstable( feature = "maybe_uninit_ref" , issue = "63568" ) ]
621629 #[ inline( always) ]
622630 pub unsafe fn get_ref ( & self ) -> & T {
623- intrinsics:: assert_inhabited :: < T > ( ) ;
624- & * self . value
631+ // SAFETY: the caller must guarantee that `self` is initialized.
632+ // This also means that `self` must be a `value` variant.
633+ unsafe {
634+ intrinsics:: assert_inhabited :: < T > ( ) ;
635+ & * self . value
636+ }
625637 }
626638
627639 /// Gets a mutable (unique) reference to the contained value.
@@ -738,8 +750,12 @@ impl<T> MaybeUninit<T> {
738750 #[ unstable( feature = "maybe_uninit_ref" , issue = "63568" ) ]
739751 #[ inline( always) ]
740752 pub unsafe fn get_mut ( & mut self ) -> & mut T {
741- intrinsics:: assert_inhabited :: < T > ( ) ;
742- & mut * self . value
753+ // SAFETY: the caller must guarantee that `self` is initialized.
754+ // This also means that `self` must be a `value` variant.
755+ unsafe {
756+ intrinsics:: assert_inhabited :: < T > ( ) ;
757+ & mut * self . value
758+ }
743759 }
744760
745761 /// Assuming all the elements are initialized, get a slice to them.
@@ -752,7 +768,11 @@ impl<T> MaybeUninit<T> {
752768 #[ unstable( feature = "maybe_uninit_slice_assume_init" , issue = "none" ) ]
753769 #[ inline( always) ]
754770 pub unsafe fn slice_get_ref ( slice : & [ Self ] ) -> & [ T ] {
755- & * ( slice as * const [ Self ] as * const [ T ] )
771+ // SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
772+ // `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
773+ // The pointer obtained is valid since it refers to memory owned by `slice` which is a
774+ // reference and thus guaranteed to be valid for reads.
775+ unsafe { & * ( slice as * const [ Self ] as * const [ T ] ) }
756776 }
757777
758778 /// Assuming all the elements are initialized, get a mutable slice to them.
@@ -765,7 +785,9 @@ impl<T> MaybeUninit<T> {
765785 #[ unstable( feature = "maybe_uninit_slice_assume_init" , issue = "none" ) ]
766786 #[ inline( always) ]
767787 pub unsafe fn slice_get_mut ( slice : & mut [ Self ] ) -> & mut [ T ] {
768- & mut * ( slice as * mut [ Self ] as * mut [ T ] )
788+ // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
789+ // mutable reference which is also guaranteed to be valid for writes.
790+ unsafe { & mut * ( slice as * mut [ Self ] as * mut [ T ] ) }
769791 }
770792
771793 /// Gets a pointer to the first element of the array.
0 commit comments