about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-05-09 18:34:51 +0200
committerGitHub <noreply@github.com>2019-05-09 18:34:51 +0200
commit8fd5587790624a3a360154b5a0c3ff0c2f45033f (patch)
tree55ca5c45abf2b16fb1c760d935c1f4299ed0404e /src/libcore
parentef01f29964df207f181bd5bcf236e41372a17273 (diff)
parentd5e819015f550bedb27bc287f96f3937bb831cd5 (diff)
downloadrust-8fd5587790624a3a360154b5a0c3ff0c2f45033f.tar.gz
rust-8fd5587790624a3a360154b5a0c3ff0c2f45033f.zip
Rollup merge of #60601 - SimonSapin:cast, r=Kimundi
Add a `cast` method to raw pointers.

This is similar to `NonNull::cast`.

Compared to the `as` operator (which has a wide range of meanings depending on the input and output types), a call to this method:

* Can only go from a raw pointer to a raw pointer
* Cannot change the pointer’s `const`ness

… even when the pointed types are inferred based on context.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/ptr.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index a41be4269d5..6355bcdcab2 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -974,6 +974,13 @@ impl<T: ?Sized> *const T {
         (self as *const u8) == null()
     }
 
+    /// Cast to a pointer to a different type
+    #[unstable(feature = "ptr_cast", issue = "60602")]
+    #[inline]
+    pub const fn cast<U>(self) -> *const U {
+        self as _
+    }
+
     /// Returns `None` if the pointer is null, or else returns a reference to
     /// the value wrapped in `Some`.
     ///
@@ -1593,6 +1600,13 @@ impl<T: ?Sized> *mut T {
         (self as *mut u8) == null_mut()
     }
 
+    /// Cast to a pointer to a different type
+    #[unstable(feature = "ptr_cast", issue = "60602")]
+    #[inline]
+    pub const fn cast<U>(self) -> *mut U {
+        self as _
+    }
+
     /// Returns `None` if the pointer is null, or else returns a reference to
     /// the value wrapped in `Some`.
     ///