Simplify implementation of various pointer methods

This commit is contained in:
Maybe Waffle 2022-10-28 23:06:29 +04:00
parent 6c54745784
commit d3b51926f8
3 changed files with 15 additions and 25 deletions

View File

@ -154,6 +154,7 @@
#![feature(maybe_uninit_uninit_array)] #![feature(maybe_uninit_uninit_array)]
#![feature(ptr_alignment_type)] #![feature(ptr_alignment_type)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(set_ptr_value)]
#![feature(slice_ptr_get)] #![feature(slice_ptr_get)]
#![feature(slice_split_at_unchecked)] #![feature(slice_split_at_unchecked)]
#![feature(str_internals)] #![feature(str_internals)]

View File

@ -473,8 +473,7 @@ impl<T: ?Sized> *const T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset(self, count: isize) -> Self { pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`. // SAFETY: the caller must uphold the safety contract for `offset`.
let this = unsafe { self.cast::<u8>().offset(count).cast::<()>() }; unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
from_raw_parts::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -554,7 +553,7 @@ impl<T: ?Sized> *const T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_offset(self, count: isize) -> Self { pub const fn wrapping_byte_offset(self, count: isize) -> Self {
from_raw_parts::<T>(self.cast::<u8>().wrapping_offset(count).cast::<()>(), metadata(self)) self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
} }
/// Masks out bits of the pointer according to a mask. /// Masks out bits of the pointer according to a mask.
@ -567,8 +566,7 @@ impl<T: ?Sized> *const T {
#[must_use = "returns a new pointer rather than modifying its argument"] #[must_use = "returns a new pointer rather than modifying its argument"]
#[inline(always)] #[inline(always)]
pub fn mask(self, mask: usize) -> *const T { pub fn mask(self, mask: usize) -> *const T {
let this = intrinsics::ptr_mask(self.cast::<()>(), mask); intrinsics::ptr_mask(self.cast::<()>(), mask).with_metadata_of(self)
from_raw_parts::<T>(this, metadata(self))
} }
/// Calculates the distance between two pointers. The returned value is in /// Calculates the distance between two pointers. The returned value is in
@ -906,8 +904,7 @@ impl<T: ?Sized> *const T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_add(self, count: usize) -> Self { pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`. // SAFETY: the caller must uphold the safety contract for `add`.
let this = unsafe { self.cast::<u8>().add(count).cast::<()>() }; unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
from_raw_parts::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer (convenience for /// Calculates the offset from a pointer (convenience for
@ -993,8 +990,7 @@ impl<T: ?Sized> *const T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub(self, count: usize) -> Self { pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`. // SAFETY: the caller must uphold the safety contract for `sub`.
let this = unsafe { self.cast::<u8>().sub(count).cast::<()>() }; unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
from_raw_parts::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -1074,7 +1070,7 @@ impl<T: ?Sized> *const T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_add(self, count: usize) -> Self { pub const fn wrapping_byte_add(self, count: usize) -> Self {
from_raw_parts::<T>(self.cast::<u8>().wrapping_add(count).cast::<()>(), metadata(self)) self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -1154,7 +1150,7 @@ impl<T: ?Sized> *const T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_sub(self, count: usize) -> Self { pub const fn wrapping_byte_sub(self, count: usize) -> Self {
from_raw_parts::<T>(self.cast::<u8>().wrapping_sub(count).cast::<()>(), metadata(self)) self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
} }
/// Reads the value from `self` without moving it. This leaves the /// Reads the value from `self` without moving it. This leaves the

View File

@ -487,8 +487,7 @@ impl<T: ?Sized> *mut T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_offset(self, count: isize) -> Self { pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`. // SAFETY: the caller must uphold the safety contract for `offset`.
let this = unsafe { self.cast::<u8>().offset(count).cast::<()>() }; unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
from_raw_parts_mut::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -567,10 +566,7 @@ impl<T: ?Sized> *mut T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_offset(self, count: isize) -> Self { pub const fn wrapping_byte_offset(self, count: isize) -> Self {
from_raw_parts_mut::<T>( self.cast::<u8>().wrapping_offset(count).with_metadata_of(self)
self.cast::<u8>().wrapping_offset(count).cast::<()>(),
metadata(self),
)
} }
/// Masks out bits of the pointer according to a mask. /// Masks out bits of the pointer according to a mask.
@ -583,8 +579,7 @@ impl<T: ?Sized> *mut T {
#[must_use = "returns a new pointer rather than modifying its argument"] #[must_use = "returns a new pointer rather than modifying its argument"]
#[inline(always)] #[inline(always)]
pub fn mask(self, mask: usize) -> *mut T { pub fn mask(self, mask: usize) -> *mut T {
let this = intrinsics::ptr_mask(self.cast::<()>(), mask) as *mut (); intrinsics::ptr_mask(self.cast::<()>(), mask).cast_mut().with_metadata_of(self)
from_raw_parts_mut::<T>(this, metadata(self))
} }
/// Returns `None` if the pointer is null, or else returns a unique reference to /// Returns `None` if the pointer is null, or else returns a unique reference to
@ -1011,8 +1006,7 @@ impl<T: ?Sized> *mut T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_add(self, count: usize) -> Self { pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`. // SAFETY: the caller must uphold the safety contract for `add`.
let this = unsafe { self.cast::<u8>().add(count).cast::<()>() }; unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
from_raw_parts_mut::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer (convenience for /// Calculates the offset from a pointer (convenience for
@ -1098,8 +1092,7 @@ impl<T: ?Sized> *mut T {
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn byte_sub(self, count: usize) -> Self { pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`. // SAFETY: the caller must uphold the safety contract for `sub`.
let this = unsafe { self.cast::<u8>().sub(count).cast::<()>() }; unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
from_raw_parts_mut::<T>(this, metadata(self))
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -1179,7 +1172,7 @@ impl<T: ?Sized> *mut T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_add(self, count: usize) -> Self { pub const fn wrapping_byte_add(self, count: usize) -> Self {
from_raw_parts_mut::<T>(self.cast::<u8>().wrapping_add(count).cast::<()>(), metadata(self)) self.cast::<u8>().wrapping_add(count).with_metadata_of(self)
} }
/// Calculates the offset from a pointer using wrapping arithmetic. /// Calculates the offset from a pointer using wrapping arithmetic.
@ -1259,7 +1252,7 @@ impl<T: ?Sized> *mut T {
#[unstable(feature = "pointer_byte_offsets", issue = "96283")] #[unstable(feature = "pointer_byte_offsets", issue = "96283")]
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")] #[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
pub const fn wrapping_byte_sub(self, count: usize) -> Self { pub const fn wrapping_byte_sub(self, count: usize) -> Self {
from_raw_parts_mut::<T>(self.cast::<u8>().wrapping_sub(count).cast::<()>(), metadata(self)) self.cast::<u8>().wrapping_sub(count).with_metadata_of(self)
} }
/// Reads the value from `self` without moving it. This leaves the /// Reads the value from `self` without moving it. This leaves the