about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2024-02-08 11:11:13 -0500
committerBen Kimock <kimockb@gmail.com>2024-02-08 19:56:30 -0500
commitdbf817bae1f25f0fa2fbb1e40ab74afc892d9a60 (patch)
tree6edbdf53ae3ffcdd2e53bf66b0b5e769561ca6ed
parent611c3cb56109593824f3570720d8404b591724cc (diff)
downloadrust-dbf817bae1f25f0fa2fbb1e40ab74afc892d9a60.tar.gz
rust-dbf817bae1f25f0fa2fbb1e40ab74afc892d9a60.zip
Add and use Unique::as_non_null_ptr
-rw-r--r--library/core/src/ptr/non_null.rs6
-rw-r--r--library/core/src/ptr/unique.rs7
2 files changed, 9 insertions, 4 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs
index 575af96fc98..320cd5eb3b2 100644
--- a/library/core/src/ptr/non_null.rs
+++ b/library/core/src/ptr/non_null.rs
@@ -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()
     }
 }
 
diff --git a/library/core/src/ptr/unique.rs b/library/core/src/ptr/unique.rs
index 2d878836b16..b74d691e454 100644
--- a/library/core/src/ptr/unique.rs
+++ b/library/core/src/ptr/unique.rs
@@ -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"