add (unchecked) indexing methods to raw pointers and NonNull
This commit is contained in:
parent
90580c7b0e
commit
5f5c98bd8a
@ -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)]
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user