]> git.lizzy.rs Git - rust.git/commitdiff
Add an unstable `cast<U>() -> NonNull<U>` method to `NonNull<T>`.
authorSimon Sapin <simon.sapin@exyr.org>
Sun, 21 Jan 2018 08:56:33 +0000 (09:56 +0100)
committerSimon Sapin <simon.sapin@exyr.org>
Mon, 22 Jan 2018 08:22:21 +0000 (09:22 +0100)
This is less verbose than going through raw pointers to cast with `as`.

src/libcore/ptr.rs

index a0d716fb574050a5ba6fee44737d9b862735cbb7..3d84e910fe662d0de5873d267b8fbda362584038 100644 (file)
@@ -2553,6 +2553,14 @@ pub unsafe fn as_ref(&self) -> &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")]