Skip to content

Commit 62bdb1a

Browse files
committed
offset_from: docs improvements
1 parent 4f22692 commit 62bdb1a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

library/core/src/ptr/const_ptr.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ impl<T: ?Sized> *const T {
607607
/// Calculates the distance between two pointers. The returned value is in
608608
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
609609
///
610-
/// This function is the inverse of [`offset`].
610+
/// This function is the inverse of [`offset`]: it is valid to call if and only if
611+
/// `self` could have been computed as `origin.offset(n)` for some `n`, and it will
612+
/// then return that `n`.
611613
///
612614
/// [`offset`]: #method.offset
613615
///
@@ -646,6 +648,12 @@ impl<T: ?Sized> *const T {
646648
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
647649
/// such large allocations either.)
648650
///
651+
/// The requirement for pointers to be derived from the same allocated object is primarily
652+
/// needed for `const`-compatibility: at compile-time, pointers into *different* allocated
653+
/// object do not have a known distance to each other. However, the requirement also exists at
654+
/// runtime, and may be exploited by optimizations. You can use `(self as usize).sub(origin as
655+
/// usize) / mem::size_of::<T>()` to avoid this requirement.
656+
///
649657
/// [`add`]: #method.add
650658
/// [allocated object]: crate::ptr#allocated-object
651659
///
@@ -703,7 +711,7 @@ impl<T: ?Sized> *const T {
703711
/// units of **bytes**.
704712
///
705713
/// This is purely a convenience for casting to a `u8` pointer and
706-
/// using [offset_from][pointer::offset_from] on it. See that method for
714+
/// using [`offset_from`][pointer::offset_from] on it. See that method for
707715
/// documentation and safety requirements.
708716
///
709717
/// For non-`Sized` pointees this operation considers only the data pointers,

library/core/src/ptr/mut_ptr.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)