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
This commit is contained in:
Matthias Krüger 2023-11-29 12:34:49 +01:00 committed by GitHub
commit afe2d7392f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -1644,6 +1644,24 @@ pub const fn len(self) -> usize {
metadata(self) 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. /// Returns a raw pointer to the slice's buffer.
/// ///
/// This is equivalent to casting `self` to `*const T`, but more type-safe. /// This is equivalent to casting `self` to `*const T`, but more type-safe.

View File

@ -1920,10 +1920,10 @@ pub const fn len(self) -> usize {
/// ///
/// ``` /// ```
/// #![feature(slice_ptr_len)] /// #![feature(slice_ptr_len)]
/// use std::ptr;
/// ///
/// let mut a = [1, 2, 3]; /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
/// let ptr = &mut a as *mut [_]; /// assert!(!slice.is_empty());
/// assert!(!ptr.is_empty());
/// ``` /// ```
#[inline(always)] #[inline(always)]
#[unstable(feature = "slice_ptr_len", issue = "71146")] #[unstable(feature = "slice_ptr_len", issue = "71146")]