feat(byte_sub_ptr): add ptr::byte_sub_ptr · qinheping/verify-rust-std@861009d (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -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, *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::<u8>().sub_ptr(origin.cast::<u8>()) }
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)`.
Original file line number Diff line number Diff line change
@@ -930,6 +930,25 @@ impl<T: ?Sized> *mut T {
930 930 unsafe { (self as *const T).sub_ptr(origin) }
931 931 }
932 932
933 +/// Calculates the distance between two pointers, *where it's known that
934 + /// `self` is equal to or greater than `origin`*. The returned value is in
935 + /// units of **bytes**.
936 + ///
937 + /// This is purely a convenience for casting to a `u8` pointer and
938 + /// using [`sub_ptr`][pointer::sub_ptr] on it. See that method for
939 + /// documentation and safety requirements.
940 + ///
941 + /// For non-`Sized` pointees this operation considers only the data pointers,
942 + /// ignoring the metadata.
943 + #[unstable(feature = "ptr_sub_ptr", issue = "95892")]
944 +#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
945 +#[inline]
946 +#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
947 +pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: *mut U) -> usize {
948 +// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
949 +unsafe { (self as *const T).byte_sub_ptr(origin) }
950 +}
951 +
933 952 /// Adds an unsigned offset to a pointer.
934 953 ///
935 954 /// This can only move the pointer forward (or not move it). If you need to move forward or
Original file line number Diff line number Diff line change
@@ -866,6 +866,25 @@ impl<T: ?Sized> NonNull {
866 866 unsafe { self.pointer.sub_ptr(subtracted.pointer) }
867 867 }
868 868
869 +/// Calculates the distance between two pointers, *where it's known that
870 + /// `self` is equal to or greater than `origin`*. The returned value is in
871 + /// units of **bytes**.
872 + ///
873 + /// This is purely a convenience for casting to a `u8` pointer and
874 + /// using [`sub_ptr`][NonNull::sub_ptr] on it. See that method for
875 + /// documentation and safety requirements.
876 + ///
877 + /// For non-`Sized` pointees this operation considers only the data pointers,
878 + /// ignoring the metadata.
879 + #[inline(always)]
880 +#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
881 +#[unstable(feature = "ptr_sub_ptr", issue = "95892")]
882 +#[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
883 +pub const unsafe fn byte_sub_ptr<U: ?Sized>(self, origin: NonNull<U>) -> usize {
884 +// SAFETY: the caller must uphold the safety contract for `byte_sub_ptr`.
885 +unsafe { self.pointer.byte_sub_ptr(origin.pointer) }
886 +}
887 +
869 888 /// Reads the value from `self` without moving it. This leaves the
870 889 /// memory in `self` unchanged.
871 890 ///