Skip to content

Commit c85075d

Browse files
authored
Rollup merge of #75248 - TimDiekmann:NonNull-as_mut_ptr, r=RalfJung
Add `as_mut_ptr` to `NonNull<[T]>` Adds `as_mut_ptr` to shortcut converting a `NonNull<[T]>` to `*mut T` as proposed in #74265 (comment). r? @RalfJung
2 parents ccffe18 + a784729 commit c85075d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

library/core/src/ptr/non_null.rs

+18
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ impl<T> NonNull<[T]> {
224224
unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
225225
}
226226

227+
/// Returns a raw pointer to the slice's buffer.
228+
///
229+
/// # Examples
230+
///
231+
/// ```rust
232+
/// #![feature(slice_ptr_get, nonnull_slice_from_raw_parts)]
233+
/// use std::ptr::NonNull;
234+
///
235+
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
236+
/// assert_eq!(slice.as_mut_ptr(), 1 as *mut i8);
237+
/// ```
238+
#[inline]
239+
#[unstable(feature = "slice_ptr_get", issue = "74265")]
240+
#[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
241+
pub const fn as_mut_ptr(self) -> *mut T {
242+
self.as_non_null_ptr().as_ptr()
243+
}
244+
227245
/// Returns a raw pointer to an element or subslice, without doing bounds
228246
/// checking.
229247
///

0 commit comments

Comments
 (0)