Add and use Unique::as_non_null_ptr

This commit is contained in:
Ben Kimock 2024-02-08 11:11:13 -05:00
parent 611c3cb561
commit dbf817bae1
2 changed files with 9 additions and 4 deletions

View File

@ -1591,8 +1591,7 @@ impl<T> NonNull<[T]> {
#[unstable(feature = "slice_ptr_get", issue = "74265")]
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
pub const fn as_non_null_ptr(self) -> NonNull<T> {
// SAFETY: We know `self` is non-null.
unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
self.cast()
}
/// Returns a raw pointer to the slice's buffer.
@ -1828,8 +1827,7 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
#[inline]
fn from(unique: Unique<T>) -> Self {
// SAFETY: A Unique pointer cannot be null.
unsafe { NonNull { pointer: unique.as_ptr() } }
unique.as_non_null_ptr()
}
}

View File

@ -106,6 +106,13 @@ impl<T: ?Sized> Unique<T> {
self.pointer.as_ptr()
}
/// Acquires the underlying `*mut` pointer.
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub const fn as_non_null_ptr(self) -> NonNull<T> {
self.pointer
}
/// Dereferences the content.
///
/// The resulting lifetime is bound to self so this behaves "as if"