Skip to content

Commit

Permalink
Rollup merge of rust-lang#122800 - zachs18:nonnull-slice-is_empty, r=…
Browse files Browse the repository at this point in the history
…Amanieu

Add `NonNull::<[T]>::is_empty`.

As per rust-lang#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 authored Mar 22, 2024
2 parents dd1bde9 + 1b95760 commit a582c1e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
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 a582c1e

Please sign in to comment.