Skip to content

Commit 0aee186

Browse files
committed
make MaybeUninit::as_(mut_)ptr const
1 parent d4c940f commit 0aee186

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

library/core/src/mem/maybe_uninit.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,11 @@ impl<T> MaybeUninit<T> {
405405
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
406406
/// until they are, it is advisable to avoid them.)
407407
#[stable(feature = "maybe_uninit", since = "1.36.0")]
408+
#[rustc_const_unstable(feature="maybe_uninit_as_ptr", issue = "none")]
408409
#[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
411413
}
412414

413415
/// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
@@ -442,9 +444,11 @@ impl<T> MaybeUninit<T> {
442444
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
443445
/// until they are, it is advisable to avoid them.)
444446
#[stable(feature = "maybe_uninit", since = "1.36.0")]
447+
#[rustc_const_unstable(feature="maybe_uninit_as_ptr", issue = "none")]
445448
#[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
448452
}
449453

450454
/// Extracts the value from the `MaybeUninit<T>` container. This is a great way

0 commit comments

Comments
 (0)