File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -790,6 +790,25 @@ impl<T: ?Sized> *const T {
790790 unsafe { intrinsics:: ptr_offset_from_unsigned ( self , origin) }
791791 }
792792
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+
793812 /// Returns whether two pointers are guaranteed to be equal.
794813 ///
795814 /// 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 {
930930 unsafe { ( self as * const T ) . sub_ptr ( origin) }
931931 }
932932
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+
933952 /// Adds an unsigned offset to a pointer.
934953 ///
935954 /// 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<T> {
866866 unsafe { self . pointer . sub_ptr ( subtracted. pointer ) }
867867 }
868868
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+
869888 /// Reads the value from `self` without moving it. This leaves the
870889 /// memory in `self` unchanged.
871890 ///
You can’t perform that action at this time.
0 commit comments