about summary refs log tree commit diff
path: root/src/libcore/ptr.rs
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-01-21 09:56:33 +0100
committerSimon Sapin <simon.sapin@exyr.org>2018-01-22 09:22:21 +0100
commitb8ffc8a3d8c181e958d2ddf4f108f0cd3a108013 (patch)
tree18776e03f199e7f63e8c38f2e6c3d6fe1aef7fe6 /src/libcore/ptr.rs
parent6461c9bdd35d6273b88f22fd5e8708eaf8949283 (diff)
downloadrust-b8ffc8a3d8c181e958d2ddf4f108f0cd3a108013.tar.gz
rust-b8ffc8a3d8c181e958d2ddf4f108f0cd3a108013.zip
Add an unstable `cast<U>() -> NonNull<U>` method to `NonNull<T>`.
This is less verbose than going through raw pointers to cast with `as`.
Diffstat (limited to 'src/libcore/ptr.rs')
-rw-r--r--src/libcore/ptr.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index a0d716fb574..3d84e910fe6 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -2553,6 +2553,14 @@ impl<T: ?Sized> NonNull<T> {
     pub unsafe fn as_mut(&mut self) -> &mut T {
         &mut *self.as_ptr()
     }
+
+    /// Cast to a pointer of another type
+    #[unstable(feature = "nonnull_cast", issue = "47653")]
+    pub fn cast<U>(self) -> NonNull<U> {
+        unsafe {
+            NonNull::new_unchecked(self.as_ptr() as *mut U)
+        }
+    }
 }
 
 #[stable(feature = "nonnull", since = "1.25.0")]