From b8ffc8a3d8c181e958d2ddf4f108f0cd3a108013 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sun, 21 Jan 2018 09:56:33 +0100 Subject: [PATCH] Add an unstable `cast() -> NonNull` method to `NonNull`. This is less verbose than going through raw pointers to cast with `as`. --- src/libcore/ptr.rs | 8 ++++++++ 1 file changed, 8 insertions(+) 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 NonNull { 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(self) -> NonNull { + unsafe { + NonNull::new_unchecked(self.as_ptr() as *mut U) + } + } } #[stable(feature = "nonnull", since = "1.25.0")]