about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-08-07 12:24:28 +0200
committerRalf Jung <post@ralfj.de>2020-08-07 12:24:28 +0200
commit0aee186723a3bb3290fd4348b92b3c1aad862fc9 (patch)
treee8d13b62ed6956dfa6434a4f12833a416b4aed90
parentd4c940f0821754a98491b2d23fbb5323c14a2bf5 (diff)
downloadrust-0aee186723a3bb3290fd4348b92b3c1aad862fc9.tar.gz
rust-0aee186723a3bb3290fd4348b92b3c1aad862fc9.zip
make MaybeUninit::as_(mut_)ptr const
-rw-r--r--library/core/src/mem/maybe_uninit.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs
index cf721b01ce3..132b9db19ce 100644
--- a/library/core/src/mem/maybe_uninit.rs
+++ b/library/core/src/mem/maybe_uninit.rs
@@ -405,9 +405,11 @@ impl<T> MaybeUninit<T> {
     /// (Notice that the rules around references to uninitialized data are not finalized yet, but
     /// until they are, it is advisable to avoid them.)
     #[stable(feature = "maybe_uninit", since = "1.36.0")]
+    #[rustc_const_unstable(feature="maybe_uninit_as_ptr", issue = "none")]
     #[inline(always)]
-    pub fn as_ptr(&self) -> *const T {
-        unsafe { &*self.value as *const T }
+    pub const fn as_ptr(&self) -> *const T {
+        // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
+        self as *const _ as *const T
     }
 
     /// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
@@ -442,9 +444,11 @@ impl<T> MaybeUninit<T> {
     /// (Notice that the rules around references to uninitialized data are not finalized yet, but
     /// until they are, it is advisable to avoid them.)
     #[stable(feature = "maybe_uninit", since = "1.36.0")]
+    #[rustc_const_unstable(feature="maybe_uninit_as_ptr", issue = "none")]
     #[inline(always)]
-    pub fn as_mut_ptr(&mut self) -> *mut T {
-        unsafe { &mut *self.value as *mut T }
+    pub const fn as_mut_ptr(&mut self) -> *mut T {
+        // `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
+        self as *mut _ as *mut T
     }
 
     /// Extracts the value from the `MaybeUninit<T>` container. This is a great way