Skip to content

Commit 3b1d5e6

Browse files
committed
call the mut version as_mut_ptr and also add an as_ptr-like method to NonNull slices
1 parent c478b54 commit 3b1d5e6

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#![feature(const_slice_ptr_len)]
9494
#![feature(const_type_name)]
9595
#![feature(const_likely)]
96+
#![feature(const_slice_ptr_ptr)]
9697
#![feature(custom_inner_attributes)]
9798
#![feature(decl_macro)]
9899
#![feature(doc_cfg)]

src/libcore/ptr/mut_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,12 +1041,12 @@ impl<T> *mut [T] {
10411041
/// use std::ptr;
10421042
///
10431043
/// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3);
1044-
/// assert_eq!(slice.as_ptr(), 0 as *mut i8);
1044+
/// assert_eq!(slice.as_mut_ptr(), 0 as *mut i8);
10451045
/// ```
10461046
#[inline]
10471047
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
10481048
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
1049-
pub const fn as_ptr(self) -> *mut T {
1049+
pub const fn as_mut_ptr(self) -> *mut T {
10501050
self as *mut T
10511051
}
10521052
}

src/libcore/ptr/non_null.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ impl<T> NonNull<[T]> {
204204
pub const fn len(self) -> usize {
205205
self.as_ptr().len()
206206
}
207+
208+
/// Returns a non-null pointer to the slice's buffer.
209+
///
210+
/// # Examples
211+
///
212+
/// ```rust
213+
/// #![feature(slice_ptr_ptr, nonnull_slice_from_raw_parts)]
214+
///
215+
/// use std::ptr::NonNull;
216+
///
217+
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
218+
/// assert_eq!(slice.as_non_null_ptr(), NonNull::new(1 as *mut i8).unwrap());
219+
/// ```
220+
#[inline]
221+
#[unstable(feature = "slice_ptr_ptr", issue = "none")]
222+
#[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")]
223+
pub const fn as_non_null_ptr(self) -> NonNull<T> {
224+
// SAFETY: We know `self` is non-null.
225+
unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
226+
}
207227
}
208228

209229
#[stable(feature = "nonnull", since = "1.25.0")]

0 commit comments

Comments
 (0)