Skip to content

Commit bf816e0

Browse files
authored
Rollup merge of #71940 - SimonSapin:nonnull-slice, r=kennytm
Add `len` and `slice_from_raw_parts` to `NonNull<[T]>` This follows the precedent of the recently-added `<*const [T]>::len` (adding to its tracking issue #71146) and `ptr::slice_from_raw_parts`.
2 parents 2679c38 + 861dfaa commit bf816e0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/libcore/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
#![feature(const_generics)]
8888
#![feature(const_ptr_offset_from)]
8989
#![feature(const_result)]
90+
#![feature(const_slice_from_raw_parts)]
91+
#![feature(const_slice_ptr_len)]
9092
#![feature(const_type_name)]
9193
#![feature(custom_inner_attributes)]
9294
#![feature(decl_macro)]

src/libcore/ptr/non_null.rs

+59
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,65 @@ impl<T: ?Sized> NonNull<T> {
142142
}
143143
}
144144

145+
impl<T> NonNull<[T]> {
146+
/// Creates a non-null raw slice from a thin pointer and a length.
147+
///
148+
/// The `len` argument is the number of **elements**, not the number of bytes.
149+
///
150+
/// This function is safe, but dereferencing the return value is unsafe.
151+
/// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
152+
///
153+
/// [`slice::from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
154+
///
155+
/// # Examples
156+
///
157+
/// ```rust
158+
/// #![feature(nonnull_slice_from_raw_parts)]
159+
///
160+
/// use std::ptr::NonNull;
161+
///
162+
/// // create a slice pointer when starting out with a pointer to the first element
163+
/// let mut x = [5, 6, 7];
164+
/// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
165+
/// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
166+
/// assert_eq!(unsafe { slice.as_ref()[2] }, 7);
167+
/// ```
168+
///
169+
/// (Note that this example artifically demonstrates a use of this method,
170+
/// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
171+
#[unstable(feature = "nonnull_slice_from_raw_parts", issue = "71941")]
172+
#[rustc_const_unstable(feature = "const_nonnull_slice_from_raw_parts", issue = "71941")]
173+
#[inline]
174+
pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self {
175+
// SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
176+
unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), len)) }
177+
}
178+
179+
/// Returns the length of a non-null raw slice.
180+
///
181+
/// The returned value is the number of **elements**, not the number of bytes.
182+
///
183+
/// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice
184+
/// because the pointer does not have a valid address.
185+
///
186+
/// # Examples
187+
///
188+
/// ```rust
189+
/// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)]
190+
///
191+
/// use std::ptr::NonNull;
192+
///
193+
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
194+
/// assert_eq!(slice.len(), 3);
195+
/// ```
196+
#[unstable(feature = "slice_ptr_len", issue = "71146")]
197+
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
198+
#[inline]
199+
pub const fn len(self) -> usize {
200+
self.as_ptr().len()
201+
}
202+
}
203+
145204
#[stable(feature = "nonnull", since = "1.25.0")]
146205
impl<T: ?Sized> Clone for NonNull<T> {
147206
#[inline]

0 commit comments

Comments
 (0)