about summary refs log tree commit diff
path: root/src/libcore/ptr/const_ptr.rs
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-07-03 11:58:05 +0200
committerRalf Jung <post@ralfj.de>2020-07-05 18:41:29 +0200
commitc478b5473d6623622d318d058477f5f09e2eeb52 (patch)
tree4fadaf54b746c3987af7b221ade3174b958a2495 /src/libcore/ptr/const_ptr.rs
parent0cd7ff7ddfb75a38dca81ad3e76b1e984129e939 (diff)
downloadrust-c478b5473d6623622d318d058477f5f09e2eeb52.tar.gz
rust-c478b5473d6623622d318d058477f5f09e2eeb52.zip
add as_ptr method to raw slices
Diffstat (limited to 'src/libcore/ptr/const_ptr.rs')
-rw-r--r--src/libcore/ptr/const_ptr.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcore/ptr/const_ptr.rs b/src/libcore/ptr/const_ptr.rs
index d1d7a715238..f926befb4b3 100644
--- a/src/libcore/ptr/const_ptr.rs
+++ b/src/libcore/ptr/const_ptr.rs
@@ -826,6 +826,27 @@ impl<T> *const [T] {
         // Only `std` can make this guarantee.
         unsafe { Repr { rust: self }.raw }.len
     }
+
+    /// Returns a raw pointer to the slice's buffer.
+    ///
+    /// This is equivalent to casting `self` to `*const T`, but more type-safe.
+    ///
+    /// # Examples
+    ///
+    /// ```rust
+    /// #![feature(slice_ptr_ptr)]
+    ///
+    /// use std::ptr;
+    ///
+    /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
+    /// assert_eq!(slice.as_ptr(), 0 as *const i8);
+    /// ```
+    #[inline]
+    #[unstable(feature = "slice_ptr_ptr", issue = "none")]
+    #[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
+    pub const fn as_ptr(self) -> *const T {
+        self as *const T
+    }
 }
 
 // Equality for pointers