@@ -781,7 +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`].
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`.
785
787
///
786
788
/// [`offset`]: pointer#method.offset-1
787
789
///
@@ -820,6 +822,12 @@ impl<T: ?Sized> *mut T {
820
822
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
821
823
/// such large allocations either.)
822
824
///
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
+ ///
823
831
/// [`add`]: #method.add
824
832
/// [allocated object]: crate::ptr#allocated-object
825
833
///
@@ -875,7 +883,7 @@ impl<T: ?Sized> *mut T {
875
883
/// units of **bytes**.
876
884
///
877
885
/// 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
879
887
/// documentation and safety requirements.
880
888
///
881
889
/// For non-`Sized` pointees this operation considers only the data pointers,
0 commit comments