@@ -781,8 +781,9 @@ impl<T: ?Sized> *mut T {
781
781
/// Calculates the distance between two pointers. The returned value is in
782
782
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
783
783
///
784
- /// This function is the inverse of [`offset`]: it is valid to call and will return
785
- /// `n` if and only if `origin.offset(n)` is valid to call and will return `self`.
784
+ /// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
785
+ /// except that it has a lot more opportunities for UB, in exchange for the compiler
786
+ /// better understanding what you are doing.
786
787
///
787
788
/// [`offset`]: pointer#method.offset-1
788
789
///
@@ -791,7 +792,7 @@ impl<T: ?Sized> *mut T {
791
792
/// If any of the following conditions are violated, the result is Undefined
792
793
/// Behavior:
793
794
///
794
- /// * Both the starting and other pointer must be either in bounds or one
795
+ /// * Both `self` and `origin` must be either in bounds or one
795
796
/// byte past the end of the same [allocated object].
796
797
///
797
798
/// * Both pointers must be *derived from* a pointer to the same object.
@@ -825,8 +826,9 @@ impl<T: ?Sized> *mut T {
825
826
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
826
827
/// objects is not known at compile-time. However, the requirement also exists at
827
828
/// runtime and may be exploited by optimizations. If you wish to compute the difference between
828
- /// pointers that are not guaranteed to be from the same allocation, use `(self as
829
- /// usize).sub(origin as usize) / mem::size_of::<T>()`.
829
+ /// pointers that are not guaranteed to be from the same allocation, use `(self as isize -
830
+ /// origin as isize) / mem::size_of::<T>()`.
831
+ // FIXME: recommend `addr()` instead of `as usize` once that is stable.
830
832
///
831
833
/// [`add`]: #method.add
832
834
/// [allocated object]: crate::ptr#allocated-object
0 commit comments