From d5e819015f550bedb27bc287f96f3937bb831cd5 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 7 May 2019 13:32:07 +0200 Subject: [PATCH] Add a `cast` method to raw pointers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/libcore/ptr.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index a41be4269d504..6355bcdcab2fa 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -974,6 +974,13 @@ impl *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(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 *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(self) -> *mut U { + self as _ + } + /// Returns `None` if the pointer is null, or else returns a reference to /// the value wrapped in `Some`. ///