Add an unstable cast<U>() -> NonNull<U> method to NonNull<T>.

This is less verbose than going through raw pointers to cast with `as`.
This commit is contained in:
Simon Sapin 2018-01-21 09:56:33 +01:00
parent 6461c9bdd3
commit b8ffc8a3d8

View file

@ -2553,6 +2553,14 @@ impl<T: ?Sized> NonNull<T> {
pub unsafe fn as_mut(&mut self) -> &mut T { pub unsafe fn as_mut(&mut self) -> &mut T {
&mut *self.as_ptr() &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")] #[stable(feature = "nonnull", since = "1.25.0")]