@@ -606,7 +606,9 @@ impl<T: ?Sized> *const T {
606606 /// Calculates the distance between two pointers. The returned value is in
607607 /// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
608608 ///
609- /// This function is the inverse of [`offset`].
609+ /// This function is the inverse of [`offset`]: it is valid to call if and only if
610+ /// `self` could have been computed as `origin.offset(n)` for some `n`, and it will
611+ /// then return that `n`.
610612 ///
611613 /// [`offset`]: #method.offset
612614 ///
@@ -645,6 +647,12 @@ impl<T: ?Sized> *const T {
645647 /// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
646648 /// such large allocations either.)
647649 ///
650+ /// The requirement for pointers to be derived from the same allocated object is primarily
651+ /// needed for `const`-compatibility: at compile-time, pointers into *different* allocated
652+ /// object do not have a known distance to each other. However, the requirement also exists at
653+ /// runtime, and may be exploited by optimizations. You can use `(self as usize).sub(origin as
654+ /// usize) / mem::size_of::<T>()` to avoid this requirement.
655+ ///
648656 /// [`add`]: #method.add
649657 /// [allocated object]: crate::ptr#allocated-object
650658 ///
@@ -702,7 +710,7 @@ impl<T: ?Sized> *const T {
702710 /// units of **bytes**.
703711 ///
704712 /// This is purely a convenience for casting to a `u8` pointer and
705- /// using [offset_from][pointer::offset_from] on it. See that method for
713+ /// using [` offset_from` ][pointer::offset_from] on it. See that method for
706714 /// documentation and safety requirements.
707715 ///
708716 /// For non-`Sized` pointees this operation considers only the data pointers,
0 commit comments