Skip to content

Commit 6c73f25

Browse files
committed
avoid talking about inverses
1 parent 14625f5 commit 6c73f25

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

library/core/src/ptr/const_ptr.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -607,8 +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`]: it is valid to call and will return
611-
/// `n` if and only if `origin.offset(n)` is valid to call and will return `self`.
610+
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
611+
/// except that it has a lot more opportunities for UB, in exchange for the compiler
612+
/// better understanding what you are doing.
612613
///
613614
/// [`offset`]: #method.offset
614615
///
@@ -617,7 +618,7 @@ impl<T: ?Sized> *const T {
617618
/// If any of the following conditions are violated, the result is Undefined
618619
/// Behavior:
619620
///
620-
/// * Both the starting and other pointer must be either in bounds or one
621+
/// * Both `self` and `origin` must be either in bounds or one
621622
/// byte past the end of the same [allocated object].
622623
///
623624
/// * Both pointers must be *derived from* a pointer to the same object.
@@ -651,8 +652,9 @@ impl<T: ?Sized> *const T {
651652
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
652653
/// objects is not known at compile-time. However, the requirement also exists at
653654
/// runtime and may be exploited by optimizations. If you wish to compute the difference between
654-
/// pointers that are not guaranteed to be from the same allocation, use `(self as
655-
/// usize).sub(origin as usize) / mem::size_of::<T>()`.
655+
/// pointers that are not guaranteed to be from the same allocation, use `(self as isize -
656+
/// origin as isize) / mem::size_of::<T>()`.
657+
// FIXME: recommend `addr()` instead of `as usize` once that is stable.
656658
///
657659
/// [`add`]: #method.add
658660
/// [allocated object]: crate::ptr#allocated-object

library/core/src/ptr/mut_ptr.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -781,8 +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`]: 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.
786787
///
787788
/// [`offset`]: pointer#method.offset-1
788789
///
@@ -791,7 +792,7 @@ impl<T: ?Sized> *mut T {
791792
/// If any of the following conditions are violated, the result is Undefined
792793
/// Behavior:
793794
///
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
795796
/// byte past the end of the same [allocated object].
796797
///
797798
/// * Both pointers must be *derived from* a pointer to the same object.
@@ -825,8 +826,9 @@ impl<T: ?Sized> *mut T {
825826
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
826827
/// objects is not known at compile-time. However, the requirement also exists at
827828
/// 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.
830832
///
831833
/// [`add`]: #method.add
832834
/// [allocated object]: crate::ptr#allocated-object

0 commit comments

Comments
 (0)