@@ -405,9 +405,11 @@ impl<T> MaybeUninit<T> {
405
405
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
406
406
/// until they are, it is advisable to avoid them.)
407
407
#[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
408
+ #[ rustc_const_unstable( feature="maybe_uninit_as_ptr" , issue = "none" ) ]
408
409
#[ inline( always) ]
409
- pub fn as_ptr ( & self ) -> * const T {
410
- unsafe { & * self . value as * const T }
410
+ pub const fn as_ptr ( & self ) -> * const T {
411
+ // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
412
+ self as * const _ as * const T
411
413
}
412
414
413
415
/// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
@@ -442,9 +444,11 @@ impl<T> MaybeUninit<T> {
442
444
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
443
445
/// until they are, it is advisable to avoid them.)
444
446
#[ stable( feature = "maybe_uninit" , since = "1.36.0" ) ]
447
+ #[ rustc_const_unstable( feature="maybe_uninit_as_ptr" , issue = "none" ) ]
445
448
#[ inline( always) ]
446
- pub fn as_mut_ptr ( & mut self ) -> * mut T {
447
- unsafe { & mut * self . value as * mut T }
449
+ pub const fn as_mut_ptr ( & mut self ) -> * mut T {
450
+ // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
451
+ self as * mut _ as * mut T
448
452
}
449
453
450
454
/// Extracts the value from the `MaybeUninit<T>` container. This is a great way
0 commit comments