Skip to content

Commit dbf817b

Browse files
committed
Add and use Unique::as_non_null_ptr
1 parent 611c3cb commit dbf817b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

library/core/src/ptr/non_null.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,7 @@ impl<T> NonNull<[T]> {
15911591
#[unstable(feature = "slice_ptr_get", issue = "74265")]
15921592
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
15931593
pub const fn as_non_null_ptr(self) -> NonNull<T> {
1594-
// SAFETY: We know `self` is non-null.
1595-
unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
1594+
self.cast()
15961595
}
15971596

15981597
/// Returns a raw pointer to the slice's buffer.
@@ -1828,8 +1827,7 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
18281827
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
18291828
#[inline]
18301829
fn from(unique: Unique<T>) -> Self {
1831-
// SAFETY: A Unique pointer cannot be null.
1832-
unsafe { NonNull { pointer: unique.as_ptr() } }
1830+
unique.as_non_null_ptr()
18331831
}
18341832
}
18351833

library/core/src/ptr/unique.rs

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ impl<T: ?Sized> Unique<T> {
106106
self.pointer.as_ptr()
107107
}
108108

109+
/// Acquires the underlying `*mut` pointer.
110+
#[must_use = "`self` will be dropped if the result is not used"]
111+
#[inline]
112+
pub const fn as_non_null_ptr(self) -> NonNull<T> {
113+
self.pointer
114+
}
115+
109116
/// Dereferences the content.
110117
///
111118
/// The resulting lifetime is bound to self so this behaves "as if"

0 commit comments

Comments
 (0)