about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-11-29 12:34:49 +0100
committerGitHub <noreply@github.com>2023-11-29 12:34:49 +0100
commitafe2d7392f7d013e19e540afd203af23c60e6919 (patch)
tree84f4d985f59850e7b231d39bf10a7a979eb74c3f
parentaab61d0b9a143d21a4290fd00521f08f7abea0c5 (diff)
parenta8162673e353a7e80925c5bb93687656656f108a (diff)
downloadrust-afe2d7392f7d013e19e540afd203af23c60e6919.tar.gz
rust-afe2d7392f7d013e19e540afd203af23c60e6919.zip
Rollup merge of #118231 - RalfJung:const-raw-slice-empty, r=cuviper
also add is_empty to const raw slices

We have this on mutable raw slices but not const raw slices, which makes little sense.

Cc https://github.com/rust-lang/rust/issues/71146
-rw-r--r--library/core/src/ptr/const_ptr.rs18
-rw-r--r--library/core/src/ptr/mut_ptr.rs6
2 files changed, 21 insertions, 3 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index 36685f756d0..c8a0eb4ffc2 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -1644,6 +1644,24 @@ impl<T> *const [T] {
         metadata(self)
     }
 
+    /// Returns `true` if the raw slice has a length of 0.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(slice_ptr_len)]
+    /// use std::ptr;
+    ///
+    /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);
+    /// assert!(!slice.is_empty());
+    /// ```
+    #[inline(always)]
+    #[unstable(feature = "slice_ptr_len", issue = "71146")]
+    #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
+    pub const fn is_empty(self) -> bool {
+        self.len() == 0
+    }
+
     /// Returns a raw pointer to the slice's buffer.
     ///
     /// This is equivalent to casting `self` to `*const T`, but more type-safe.
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index bc362fb627f..ce0e6d6f297 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -1920,10 +1920,10 @@ impl<T> *mut [T] {
     ///
     /// ```
     /// #![feature(slice_ptr_len)]
+    /// use std::ptr;
     ///
-    /// let mut a = [1, 2, 3];
-    /// let ptr = &mut a as *mut [_];
-    /// assert!(!ptr.is_empty());
+    /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
+    /// assert!(!slice.is_empty());
     /// ```
     #[inline(always)]
     #[unstable(feature = "slice_ptr_len", issue = "71146")]