Rollup merge of #132459 - RalfJung:byte_sub_ptr, r=scottmcm · qinheping/verify-rust-std@ecd55b1 (original) (raw)

`@@ -582,7 +582,7 @@ impl<T: ?Sized> *const T {

`

582

582

` intrinsics::ptr_mask(self.cast::<()>(), mask).with_metadata_of(self)

`

583

583

`}

`

584

584

``

585

``

`-

/// Calculates the distance between two pointers. The returned value is in

`

``

585

`+

/// Calculates the distance between two pointers within the same allocation. The returned value is in

`

586

586

`` /// units of T: the distance in bytes divided by mem::size_of::<T>().

``

587

587

`///

`

588

588

`` /// This is equivalent to (self as isize - origin as isize) / (mem::size_of::<T>() as isize),

``

`@@ -677,7 +677,7 @@ impl<T: ?Sized> *const T {

`

677

677

`unsafe { intrinsics::ptr_offset_from(self, origin) }

`

678

678

`}

`

679

679

``

680

``

`-

/// Calculates the distance between two pointers. The returned value is in

`

``

680

`+

/// Calculates the distance between two pointers within the same allocation. The returned value is in

`

681

681

`/// units of bytes.

`

682

682

`///

`

683

683

`` /// This is purely a convenience for casting to a u8 pointer and

``

`@@ -695,7 +695,7 @@ impl<T: ?Sized> *const T {

`

695

695

`unsafe { self.cast::().offset_from(origin.cast::()) }

`

696

696

`}

`

697

697

``

698

``

`-

/// Calculates the distance between two pointers, *where it's known that

`

``

698

`+

/// Calculates the distance between two pointers within the same allocation, *where it's known that

`

699

699

`` /// self is equal to or greater than origin*. The returned value is in

``

700

700

`` /// units of T: the distance in bytes is divided by mem::size_of::<T>().

``

701

701

`///

`

`@@ -790,6 +790,25 @@ impl<T: ?Sized> *const T {

`

790

790

`unsafe { intrinsics::ptr_offset_from_unsigned(self, origin) }

`

791

791

`}

`

792

792

``

``

793

`+

/// Calculates the distance between two pointers within the same allocation, *where it's known that

`

``

794

`` +

/// self is equal to or greater than origin*. The returned value is in

``

``

795

`+

/// units of bytes.

`

``

796

`+

///

`

``

797

`` +

/// This is purely a convenience for casting to a u8 pointer and

``

``

798

`` +

/// using [sub_ptr][pointer::sub_ptr] on it. See that method for

``

``

799

`+

/// documentation and safety requirements.

`

``

800

`+

///

`

``

801

`` +

/// For non-Sized pointees this operation considers only the data pointers,

``

``

802

`+

/// ignoring the metadata.

`

``

803

`+

#[unstable(feature = "ptr_sub_ptr", issue = "95892")]

`

``

804

`+

#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]

`

``

805

`+

#[inline]

`

``

806

`+

#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces

`

``

807

`+

pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *const U) -> usize {

`

``

808

`` +

// SAFETY: the caller must uphold the safety contract for sub_ptr.

``

``

809

`+

unsafe { self.cast::().sub_ptr(origin.cast::()) }

`

``

810

`+

}

`

``

811

+

793

812

`/// Returns whether two pointers are guaranteed to be equal.

`

794

813

`///

`

795

814

`` /// At runtime this function behaves like Some(self == other).

``