diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-03-22 11:37:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-22 11:37:00 +0100 |
| commit | ef4a64b5d209513f7e60b2d0267329adf441d390 (patch) | |
| tree | fe3de4476da0a6eaaea7a8477ffa0c68ec72d6c8 | |
| parent | e13c40c7bd50c6a607d2403458e1bac396f143fa (diff) | |
| parent | 1b95760e41bed110ea20b44f85193fe900b13242 (diff) | |
Rollup merge of #122800 - zachs18:nonnull-slice-is_empty, r=Amanieu
Add `NonNull::<[T]>::is_empty`. As per https://github.com/rust-lang/rust/issues/71146#issuecomment-2008373983 I figured this should be fine to be insta-stable (with an FCP), but I can edit if that is not desired. r? ```@Amanieu```
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 9c0236c172a..2ac42e20d43 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -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 |
