diff options
| author | Ralf Jung <post@ralfj.de> | 2020-07-03 12:04:36 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2020-07-05 18:41:29 +0200 |
| commit | 3b1d5e6d792fb47c9a95c4ea210ce88174f18b13 (patch) | |
| tree | 099d2e0bac3d87a75bde626224f3bb2df6824584 | |
| parent | c478b5473d6623622d318d058477f5f09e2eeb52 (diff) | |
| download | rust-3b1d5e6d792fb47c9a95c4ea210ce88174f18b13.tar.gz rust-3b1d5e6d792fb47c9a95c4ea210ce88174f18b13.zip | |
call the mut version as_mut_ptr and also add an as_ptr-like method to NonNull slices
| -rw-r--r-- | src/libcore/lib.rs | 1 | ||||
| -rw-r--r-- | src/libcore/ptr/mut_ptr.rs | 4 | ||||
| -rw-r--r-- | src/libcore/ptr/non_null.rs | 20 |
3 files changed, 23 insertions, 2 deletions
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 50c56434fa9..66a1bea0c44 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -93,6 +93,7 @@ #![feature(const_slice_ptr_len)] #![feature(const_type_name)] #![feature(const_likely)] +#![feature(const_slice_ptr_ptr)] #![feature(custom_inner_attributes)] #![feature(decl_macro)] #![feature(doc_cfg)] diff --git a/src/libcore/ptr/mut_ptr.rs b/src/libcore/ptr/mut_ptr.rs index 7fc4805c921..70a81f85da8 100644 --- a/src/libcore/ptr/mut_ptr.rs +++ b/src/libcore/ptr/mut_ptr.rs @@ -1041,12 +1041,12 @@ impl<T> *mut [T] { /// use std::ptr; /// /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3); - /// assert_eq!(slice.as_ptr(), 0 as *mut i8); + /// assert_eq!(slice.as_mut_ptr(), 0 as *mut i8); /// ``` #[inline] #[unstable(feature = "slice_ptr_ptr", issue = "none")] #[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")] - pub const fn as_ptr(self) -> *mut T { + pub const fn as_mut_ptr(self) -> *mut T { self as *mut T } } diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index c2d31bfb6a4..3461d224c46 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -204,6 +204,26 @@ impl<T> NonNull<[T]> { pub const fn len(self) -> usize { self.as_ptr().len() } + + /// Returns a non-null pointer to the slice's buffer. + /// + /// # Examples + /// + /// ```rust + /// #![feature(slice_ptr_ptr, nonnull_slice_from_raw_parts)] + /// + /// use std::ptr::NonNull; + /// + /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3); + /// assert_eq!(slice.as_non_null_ptr(), NonNull::new(1 as *mut i8).unwrap()); + /// ``` + #[inline] + #[unstable(feature = "slice_ptr_ptr", issue = "none")] + #[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")] + pub const fn as_non_null_ptr(self) -> NonNull<T> { + // SAFETY: We know `self` is non-null. + unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) } + } } #[stable(feature = "nonnull", since = "1.25.0")] |
