@@ -781,7 +781,9 @@ impl<T: ?Sized> *mut T {
781781 /// Calculates the distance between two pointers. The returned value is in
782782 /// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
783783 ///
784- /// This function is the inverse of [`offset`].
784+ /// This function is the inverse of [`offset`]: it is valid to call if and only if
785+ /// `self` could have been computed as `origin.offset(n)` for some `n`, and it will
786+ /// then return that `n`.
785787 ///
786788 /// [`offset`]: pointer#method.offset-1
787789 ///
@@ -820,6 +822,12 @@ impl<T: ?Sized> *mut T {
820822 /// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
821823 /// such large allocations either.)
822824 ///
825+ /// The requirement for pointers to be derived from the same allocated object is primarily
826+ /// needed for `const`-compatibility: at compile-time, pointers into *different* allocated
827+ /// object do not have a known distance to each other. However, the requirement also exists at
828+ /// runtime, and may be exploited by optimizations. You can use `(self as usize).sub(origin as
829+ /// usize) / mem::size_of::<T>()` to avoid this requirement.
830+ ///
823831 /// [`add`]: #method.add
824832 /// [allocated object]: crate::ptr#allocated-object
825833 ///
@@ -875,7 +883,7 @@ impl<T: ?Sized> *mut T {
875883 /// units of **bytes**.
876884 ///
877885 /// This is purely a convenience for casting to a `u8` pointer and
878- /// using [offset_from][pointer::offset_from] on it. See that method for
886+ /// using [` offset_from` ][pointer::offset_from] on it. See that method for
879887 /// documentation and safety requirements.
880888 ///
881889 /// For non-`Sized` pointees this operation considers only the data pointers,
0 commit comments