Skip to content

Commit b8ffc8a

Browse files
committed
Add an unstable cast<U>() -> NonNull<U> method to NonNull<T>.
This is less verbose than going through raw pointers to cast with `as`.
1 parent 6461c9b commit b8ffc8a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/libcore/ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,14 @@ impl<T: ?Sized> NonNull<T> {
25532553
pub unsafe fn as_mut(&mut self) -> &mut T {
25542554
&mut *self.as_ptr()
25552555
}
2556+
2557+
/// Cast to a pointer of another type
2558+
#[unstable(feature = "nonnull_cast", issue = "47653")]
2559+
pub fn cast<U>(self) -> NonNull<U> {
2560+
unsafe {
2561+
NonNull::new_unchecked(self.as_ptr() as *mut U)
2562+
}
2563+
}
25562564
}
25572565

25582566
#[stable(feature = "nonnull", since = "1.25.0")]

0 commit comments

Comments
 (0)