@@ -798,8 +798,20 @@ impl<T: ?Sized> *mut T {
798
798
/// but it provides slightly more information to the optimizer, which can
799
799
/// sometimes allow it to optimize slightly better with some backends.
800
800
///
801
- /// This method is the inverse of [`add`](#method.add) (and, with the parameters
802
- /// in the other order, of [`sub`](#method.sub)).
801
+ /// This method can be though of as recovering the `count` that was passed
802
+ /// to [`add`](#method.add) (or, with the parameters in the other order,
803
+ /// to [`sub`](#method.sub)). The following are all equivalent, assuming
804
+ /// that their safety preconditions are met:
805
+ /// ```rust
806
+ /// # #![feature(ptr_unsigned_offset_from)]
807
+ /// # unsafe fn blah(ptr: *mut i32, origin: *mut i32, count: usize) -> bool {
808
+ /// ptr.sub_ptr(origin) == count
809
+ /// # &&
810
+ /// origin.add(count) == ptr
811
+ /// # &&
812
+ /// ptr.sub(count) == origin
813
+ /// # }
814
+ /// ```
803
815
///
804
816
/// # Safety
805
817
///
@@ -828,23 +840,23 @@ impl<T: ?Sized> *mut T {
828
840
/// let ptr1: *mut i32 = p.add(1);
829
841
/// let ptr2: *mut i32 = p.add(3);
830
842
///
831
- /// assert_eq!(ptr2.unsigned_offset_from (ptr1), 2);
843
+ /// assert_eq!(ptr2.sub_ptr (ptr1), 2);
832
844
/// assert_eq!(ptr1.add(2), ptr2);
833
845
/// assert_eq!(ptr2.sub(2), ptr1);
834
- /// assert_eq!(ptr2.unsigned_offset_from (ptr2), 0);
846
+ /// assert_eq!(ptr2.sub_ptr (ptr2), 0);
835
847
/// }
836
848
///
837
849
/// // This would be incorrect, as the pointers are not correctly ordered:
838
850
/// // ptr1.offset_from(ptr2)
839
851
#[ unstable( feature = "ptr_unsigned_offset_from" , issue = "88888888" ) ]
840
852
#[ rustc_const_unstable( feature = "const_ptr_offset_from" , issue = "92980" ) ]
841
853
#[ inline]
842
- pub const unsafe fn unsigned_offset_from ( self , origin : * const T ) -> usize
854
+ pub const unsafe fn sub_ptr ( self , origin : * const T ) -> usize
843
855
where
844
856
T : Sized ,
845
857
{
846
- // SAFETY: the caller must uphold the safety contract for `unsigned_offset_from `.
847
- unsafe { ( self as * const T ) . unsigned_offset_from ( origin) }
858
+ // SAFETY: the caller must uphold the safety contract for `sub_ptr `.
859
+ unsafe { ( self as * const T ) . sub_ptr ( origin) }
848
860
}
849
861
850
862
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
0 commit comments