Skip to content

Commit

Permalink
also add is_empty to const raw slices
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 24, 2023
1 parent eab8c7d commit c2b58d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
17 changes: 17 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,23 @@ impl<T> *const [T] {
metadata(self)
}

/// Returns `true` if the raw slice has a length of 0.
///
/// # Examples
///
/// ```
/// #![feature(slice_ptr_len)]
///
/// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3);

Check failure on line 1654 in library/core/src/ptr/const_ptr.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

failed to resolve: use of undeclared crate or module `ptr`

Check failure on line 1654 in library/core/src/ptr/const_ptr.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

failed to resolve: use of undeclared crate or module `ptr`
/// 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.
Expand Down
5 changes: 2 additions & 3 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,9 +1921,8 @@ impl<T> *mut [T] {
/// ```
/// #![feature(slice_ptr_len)]
///
/// 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);

Check failure on line 1924 in library/core/src/ptr/mut_ptr.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

failed to resolve: use of undeclared crate or module `ptr`

Check failure on line 1924 in library/core/src/ptr/mut_ptr.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

failed to resolve: use of undeclared crate or module `ptr`
/// assert!(!slice.is_empty());
/// ```
#[inline(always)]
#[unstable(feature = "slice_ptr_len", issue = "71146")]
Expand Down

0 comments on commit c2b58d7

Please sign in to comment.