Skip to content

feat(byte_sub_ptr): unstably add ptr::byte_sub_ptr #132459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<T: ?Sized> *const T {
intrinsics::ptr_mask(self.cast::<()>(), mask).with_metadata_of(self)
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
///
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
Expand Down Expand Up @@ -677,7 +677,7 @@ impl<T: ?Sized> *const T {
unsafe { intrinsics::ptr_offset_from(self, origin) }
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of **bytes**.
///
/// This is purely a convenience for casting to a `u8` pointer and
Expand All @@ -695,7 +695,7 @@ impl<T: ?Sized> *const T {
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
}

/// Calculates the distance between two pointers, *where it's known that
/// Calculates the distance between two pointers within the same allocation, *where it's known that
/// `self` is equal to or greater than `origin`*. The returned value is in
/// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
///
Expand Down Expand Up @@ -790,6 +790,25 @@ impl<T: ?Sized> *const T {
unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }
}

/// Calculates the distance between two pointers within the same allocation, *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)`.
Expand Down
25 changes: 22 additions & 3 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ impl<T: ?Sized> *mut T {
(self as *const T).guaranteed_ne(other as _)
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
///
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
Expand Down Expand Up @@ -839,7 +839,7 @@ impl<T: ?Sized> *mut T {
unsafe { (self as *const T).offset_from(origin) }
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of **bytes**.
///
/// This is purely a convenience for casting to a `u8` pointer and
Expand All @@ -857,7 +857,7 @@ impl<T: ?Sized> *mut T {
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
}

/// Calculates the distance between two pointers, *where it's known that
/// Calculates the distance between two pointers within the same allocation, *where it's known that
/// `self` is equal to or greater than `origin`*. The returned value is in
/// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
///
Expand Down Expand Up @@ -930,6 +930,25 @@ impl<T: ?Sized> *mut T {
unsafe { (self as *const T).sub_ptr(origin) }
}

/// Calculates the distance between two pointers within the same allocation, *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
Expand Down
25 changes: 22 additions & 3 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ impl<T: ?Sized> NonNull<T> {
unsafe { NonNull { pointer: self.pointer.byte_sub(count) } }
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
///
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
Expand Down Expand Up @@ -773,7 +773,7 @@ impl<T: ?Sized> NonNull<T> {
unsafe { self.pointer.offset_from(origin.pointer) }
}

/// Calculates the distance between two pointers. The returned value is in
/// Calculates the distance between two pointers within the same allocation. The returned value is in
/// units of **bytes**.
///
/// This is purely a convenience for casting to a `u8` pointer and
Expand All @@ -793,7 +793,7 @@ impl<T: ?Sized> NonNull<T> {

// N.B. `wrapping_offset``, `wrapping_add`, etc are not implemented because they can wrap to null

/// Calculates the distance between two pointers, *where it's known that
/// Calculates the distance between two pointers within the same allocation, *where it's known that
/// `self` is equal to or greater than `origin`*. The returned value is in
/// units of T: the distance in bytes is divided by `mem::size_of::<T>()`.
///
Expand Down Expand Up @@ -866,6 +866,25 @@ impl<T: ?Sized> NonNull<T> {
unsafe { self.pointer.sub_ptr(subtracted.pointer) }
}

/// Calculates the distance between two pointers within the same allocation, *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.
///
Expand Down
Loading