about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-09 13:38:33 +0100
committerGitHub <noreply@github.com>2022-01-09 13:38:33 +0100
commit295ef3a3367dfbb1eef6ea4d259e546053796f6f (patch)
tree962249f14c25b658e2aa86ed2b77734e07f8fed6
parent51001b35bd4079ea40536a264e054eec9b4f11bd (diff)
parent1a966235138ad68967aa0892aa0621315081e4e8 (diff)
downloadrust-295ef3a3367dfbb1eef6ea4d259e546053796f6f.tar.gz
rust-295ef3a3367dfbb1eef6ea4d259e546053796f6f.zip
Rollup merge of #92657 - Kixunil:ptr_as_const_mut, r=m-ou-se
Implemented const casts of raw pointers

This adds `as_mut()` method for `*const T` and `as_const()` for `*mut T`
which are intended to make casting of consts safer. This was discussed
in the [internals discussion][discussion].

Given that this is a simple change and multiple people agreed to it including `@RalfJung` I decided to go ahead and open the PR.

[discussion]: https://internals.rust-lang.org/t/casting-constness-can-be-risky-heres-a-simple-fix/15933
-rw-r--r--library/core/src/ptr/const_ptr.rs10
-rw-r--r--library/core/src/ptr/mut_ptr.rs14
2 files changed, 24 insertions, 0 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index a93327a0132..7b826f921ca 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -48,6 +48,16 @@ impl<T: ?Sized> *const T {
         self as _
     }
 
+    /// Changes constness without changing the type.
+    ///
+    /// This is a bit safer than `as` because it wouldn't silently change the type if the code is
+    /// refactored.
+    #[unstable(feature = "ptr_const_cast", issue = "92675")]
+    #[rustc_const_unstable(feature = "ptr_const_cast", issue = "92675")]
+    pub const fn as_mut(self) -> *mut T {
+        self as _
+    }
+
     /// Casts a pointer to its raw bits.
     ///
     /// This is equivalent to `as usize`, but is more specific to enhance readability.
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index 5fd3b2ebc60..6c50d405297 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -47,6 +47,20 @@ impl<T: ?Sized> *mut T {
         self as _
     }
 
+    /// Changes constness without changing the type.
+    ///
+    /// This is a bit safer than `as` because it wouldn't silently change the type if the code is
+    /// refactored.
+    ///
+    /// While not strictly required (`*mut T` coerces to `*const T`), this is provided for symmetry
+    /// with `as_mut()` on `*const T` and may have documentation value if used instead of implicit
+    /// coercion.
+    #[unstable(feature = "ptr_const_cast", issue = "92675")]
+    #[rustc_const_unstable(feature = "ptr_const_cast", issue = "92675")]
+    pub const fn as_const(self) -> *const T {
+        self as _
+    }
+
     /// Casts a pointer to its raw bits.
     ///
     /// This is equivalent to `as usize`, but is more specific to enhance readability.