Skip to content

Commit

Permalink
Add an unstable cast<U>() -> NonNull<U> method to NonNull<T>.
Browse files Browse the repository at this point in the history
This is less verbose than going through raw pointers to cast with `as`.
  • Loading branch information
SimonSapin committed Jan 22, 2018
1 parent 6461c9b commit b8ffc8a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit b8ffc8a

Please sign in to comment.