Skip to content

Commit

Permalink
Rollup merge of #122800 - zachs18:nonnull-slice-is_empty, r=Amanieu
Browse files Browse the repository at this point in the history
Add `NonNull::<[T]>::is_empty`.

As per #71146 (comment)

I figured this should be fine to be insta-stable (with an FCP), but I can edit if that is not desired.

r? ```@Amanieu```
  • Loading branch information
matthiaskrgr committed Mar 22, 2024
2 parents e13c40c + 1b95760 commit ef4a64b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions library/core/src/ptr/non_null.rs
Expand Up @@ -1575,6 +1575,25 @@ impl<T> NonNull<[T]> {
self.as_ptr().len()
}

/// Returns `true` if the non-null raw slice has a length of 0.
///
/// # Examples
///
/// ```rust
/// #![feature(slice_ptr_is_empty_nonnull)]
/// use std::ptr::NonNull;
///
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
/// assert!(!slice.is_empty());
/// ```
#[unstable(feature = "slice_ptr_is_empty_nonnull", issue = "71146")]
#[rustc_const_unstable(feature = "const_slice_ptr_is_empty_nonnull", issue = "71146")]
#[must_use]
#[inline]
pub const fn is_empty(self) -> bool {
self.len() == 0
}

/// Returns a non-null pointer to the slice's buffer.
///
/// # Examples
Expand Down

0 comments on commit ef4a64b

Please sign in to comment.