mirror of https://github.com/rust-lang/rust.git
feat(byte_sub_ptr): add ptr::byte_sub_ptr
This is an API that naturally should exist as a combination of byte_offset_from and sub_ptr both existing (they showed up at similar times so this union was never made). Adding these is a logical (and perhaps final) precondition of stabilizing ptr_sub_ptr (#95892).
This commit is contained in:
parent
a8e1186e3c
commit
aba2088735
|
@ -790,6 +790,25 @@ impl<T: ?Sized> *const T {
|
|||
unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
|
||||
}
|
||||
|
||||
/// Calculates the distance between two pointers, *where it's known that
|
||||
/// `self` is equal to or greater than `origin`*. The returned value is in
|
||||
/// units of **bytes**.
|
||||
///
|
||||
/// This is purely a convenience for casting to a `u8` pointer and
|
||||
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for
|
||||
/// documentation and safety requirements.
|
||||
///
|
||||
/// For non-`Sized` pointees this operation considers only the data pointers,
|
||||
/// ignoring the metadata.
|
||||
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
|
||||
#[inline]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize {
|
||||
// SAFETY: the caller must uphold the safety contract for `sub_ptr`.
|
||||
unsafe { self.cast::<u8>().sub_ptr(origin.cast::<u8>()) }
|
||||
}
|
||||
|
||||
/// Returns whether two pointers are guaranteed to be equal.
|
||||
///
|
||||
/// At runtime this function behaves like `Some(self == other)`.
|
||||
|
|
|
@ -930,6 +930,25 @@ impl<T: ?Sized> *mut T {
|
|||
unsafe { (self as *const T).sub_ptr(origin) }
|
||||
}
|
||||
|
||||
/// Calculates the distance between two pointers, *where it's known that
|
||||
/// `self` is equal to or greater than `origin`*. The returned value is in
|
||||
/// units of **bytes**.
|
||||
///
|
||||
/// This is purely a convenience for casting to a `u8` pointer and
|
||||
/// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for
|
||||
/// documentation and safety requirements.
|
||||
///
|
||||
/// For non-`Sized` pointees this operation considers only the data pointers,
|
||||
/// ignoring the metadata.
|
||||
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
|
||||
#[inline]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *mut U) -> usize {
|
||||
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
|
||||
unsafe { (self as *const T).byte_sub_ptr(origin) }
|
||||
}
|
||||
|
||||
/// Adds an unsigned offset to a pointer.
|
||||
///
|
||||
/// This can only move the pointer forward (or not move it). If you need to move forward or
|
||||
|
|
|
@ -866,6 +866,25 @@ impl<T: ?Sized> NonNull<T> {
|
|||
unsafe { self.pointer.sub_ptr(subtracted.pointer) }
|
||||
}
|
||||
|
||||
/// Calculates the distance between two pointers, *where it's known that
|
||||
/// `self` is equal to or greater than `origin`*. The returned value is in
|
||||
/// units of **bytes**.
|
||||
///
|
||||
/// This is purely a convenience for casting to a `u8` pointer and
|
||||
/// using [`sub_ptr`][NonNull::sub_ptr] on it. See that method for
|
||||
/// documentation and safety requirements.
|
||||
///
|
||||
/// For non-`Sized` pointees this operation considers only the data pointers,
|
||||
/// ignoring the metadata.
|
||||
#[inline(always)]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
|
||||
#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
|
||||
pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: NonNull<U>) -> usize {
|
||||
// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
|
||||
unsafe { self.pointer.byte_sub_ptr(origin.pointer) }
|
||||
}
|
||||
|
||||
/// Reads the value from `self` without moving it. This leaves the
|
||||
/// memory in `self` unchanged.
|
||||
///
|
||||
|
|
Loading…
Reference in New Issue