diff options
| -rw-r--r-- | src/libcore/lib.rs | 2 | ||||
| -rw-r--r-- | src/libcore/ptr/const_ptr.rs | 37 | ||||
| -rw-r--r-- | src/libcore/ptr/mut_ptr.rs | 38 | ||||
| -rw-r--r-- | src/libcore/ptr/non_null.rs | 41 |
4 files changed, 103 insertions, 15 deletions
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 66a1bea0c44..1f906650dfa 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -93,7 +93,6 @@ #![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)] @@ -149,6 +148,7 @@ #![feature(associated_type_bounds)] #![feature(const_type_id)] #![feature(const_caller_location)] +#![feature(slice_ptr_get)] #![feature(no_niche)] // rust-lang/rust#68303 #![feature(unsafe_block_in_unsafe_fn)] #![deny(unsafe_op_in_unsafe_fn)] diff --git a/src/libcore/ptr/const_ptr.rs b/src/libcore/ptr/const_ptr.rs index f926befb4b3..22c741d4651 100644 --- a/src/libcore/ptr/const_ptr.rs +++ b/src/libcore/ptr/const_ptr.rs @@ -2,6 +2,7 @@ use super::*; use crate::cmp::Ordering::{self, Equal, Greater, Less}; use crate::intrinsics; use crate::mem; +use crate::slice::SliceIndex; #[lang = "const_ptr"] impl<T: ?Sized> *const T { @@ -834,19 +835,47 @@ impl<T> *const [T] { /// # Examples /// /// ```rust - /// #![feature(slice_ptr_ptr)] - /// + /// #![feature(slice_ptr_get)] /// use std::ptr; /// /// let slice: *const [i8] = ptr::slice_from_raw_parts(ptr::null(), 3); /// assert_eq!(slice.as_ptr(), 0 as *const i8); /// ``` #[inline] - #[unstable(feature = "slice_ptr_ptr", issue = "none")] - #[rustc_const_unstable(feature = "const_slice_ptr_ptr", issue = "none")] + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[rustc_const_unstable(feature = "slice_ptr_get", issue = "none")] pub const fn as_ptr(self) -> *const T { self as *const T } + + /// Returns a raw pointer to an element or subslice, without doing bounds + /// checking. + /// + /// Calling this method with an out-of-bounds index or when `self` is not dereferencable + /// is *[undefined behavior]* even if the resulting pointer is not used. + /// + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html + /// + /// # Examples + /// + /// ``` + /// #![feature(slice_ptr_get)] + /// + /// let x = &[1, 2, 4] as *const [i32]; + /// + /// unsafe { + /// assert_eq!(x.get_unchecked(1), x.as_ptr().add(1)); + /// } + /// ``` + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[inline] + pub unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output + where + I: SliceIndex<[T]>, + { + // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds. + unsafe { index.get_unchecked(self) } + } } // Equality for pointers diff --git a/src/libcore/ptr/mut_ptr.rs b/src/libcore/ptr/mut_ptr.rs index 70a81f85da8..56bade70694 100644 --- a/src/libcore/ptr/mut_ptr.rs +++ b/src/libcore/ptr/mut_ptr.rs @@ -1,6 +1,7 @@ use super::*; use crate::cmp::Ordering::{self, Equal, Greater, Less}; use crate::intrinsics; +use crate::slice::SliceIndex; #[lang = "mut_ptr"] impl<T: ?Sized> *mut T { @@ -1014,7 +1015,6 @@ impl<T> *mut [T] { /// /// ```rust /// #![feature(slice_ptr_len)] - /// /// use std::ptr; /// /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3); @@ -1036,19 +1036,47 @@ impl<T> *mut [T] { /// # Examples /// /// ```rust - /// #![feature(slice_ptr_ptr)] - /// + /// #![feature(slice_ptr_get)] /// use std::ptr; /// /// let slice: *mut [i8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 3); /// 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")] + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[rustc_const_unstable(feature = "slice_ptr_get", issue = "none")] pub const fn as_mut_ptr(self) -> *mut T { self as *mut T } + + /// Returns a raw pointer to an element or subslice, without doing bounds + /// checking. + /// + /// Calling this method with an out-of-bounds index or when `self` is not dereferencable + /// is *[undefined behavior]* even if the resulting pointer is not used. + /// + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html + /// + /// # Examples + /// + /// ``` + /// #![feature(slice_ptr_get)] + /// + /// let x = &mut [1, 2, 4] as *mut [i32]; + /// + /// unsafe { + /// assert_eq!(x.get_unchecked_mut(1), x.as_mut_ptr().add(1)); + /// } + /// ``` + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[inline] + pub unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output + where + I: SliceIndex<[T]>, + { + // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds. + unsafe { index.get_unchecked_mut(self) } + } } // Equality for pointers diff --git a/src/libcore/ptr/non_null.rs b/src/libcore/ptr/non_null.rs index 3461d224c46..f59da631b46 100644 --- a/src/libcore/ptr/non_null.rs +++ b/src/libcore/ptr/non_null.rs @@ -6,6 +6,7 @@ use crate::marker::Unsize; use crate::mem; use crate::ops::{CoerceUnsized, DispatchFromDyn}; use crate::ptr::Unique; +use crate::slice::SliceIndex; /// `*mut T` but non-zero and covariant. /// @@ -192,7 +193,6 @@ impl<T> NonNull<[T]> { /// /// ```rust /// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)] - /// /// use std::ptr::NonNull; /// /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3); @@ -210,20 +210,51 @@ impl<T> NonNull<[T]> { /// # Examples /// /// ```rust - /// #![feature(slice_ptr_ptr, nonnull_slice_from_raw_parts)] - /// + /// #![feature(slice_ptr_get, 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")] + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[rustc_const_unstable(feature = "slice_ptr_get", 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()) } } + + /// Returns a raw pointer to an element or subslice, without doing bounds + /// checking. + /// + /// Calling this method with an out-of-bounds index or when `self` is not dereferencable + /// is *[undefined behavior]* even if the resulting pointer is not used. + /// + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html + /// + /// # Examples + /// + /// ``` + /// #![feature(slice_ptr_get, nonnull_slice_from_raw_parts)] + /// use std::ptr::NonNull; + /// + /// let x = &mut [1, 2, 4]; + /// let x = NonNull::slice_from_raw_parts(NonNull::new(x.as_mut_ptr()).unwrap(), x.len()); + /// + /// unsafe { + /// assert_eq!(x.get_unchecked_mut(1).as_ptr(), x.as_non_null_ptr().as_ptr().add(1)); + /// } + /// ``` + #[unstable(feature = "slice_ptr_get", issue = "none")] + #[inline] + pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output> + where + I: SliceIndex<[T]>, + { + // SAFETY: the caller ensures that `self` is dereferencable and `index` in-bounds. + // As a consequence, the resulting pointer cannot be NULL. + unsafe { NonNull::new_unchecked(self.as_ptr().get_unchecked_mut(index)) } + } } #[stable(feature = "nonnull", since = "1.25.0")] |
