Auto merge of #113797 - RalfJung:offset_from_docs, r=workingjubilee
offset_from: docs improvements This is the part of https://github.com/rust-lang/rust/pull/112837 that doesn't add a new function, just tweaks the existing docs.
This commit is contained in:
commit
a6dfd89fa7
@ -607,7 +607,16 @@ impl<T: ?Sized> *const T {
|
||||
/// Calculates the distance between two pointers. The returned value is in
|
||||
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
|
||||
///
|
||||
/// This function is the inverse of [`offset`].
|
||||
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
|
||||
/// except that it has a lot more opportunities for UB, in exchange for the compiler
|
||||
/// better understanding what you are doing.
|
||||
///
|
||||
/// The primary motivation of this method is for computing the `len` of an array/slice
|
||||
/// of `T` that you are currently representing as a "start" and "end" pointer
|
||||
/// (and "end" is "one past the end" of the array).
|
||||
/// In that case, `end.offset_from(start)` gets you the length of the array.
|
||||
///
|
||||
/// All of the following safety requirements are trivially satisfied for this usecase.
|
||||
///
|
||||
/// [`offset`]: #method.offset
|
||||
///
|
||||
@ -616,7 +625,7 @@ impl<T: ?Sized> *const T {
|
||||
/// If any of the following conditions are violated, the result is Undefined
|
||||
/// Behavior:
|
||||
///
|
||||
/// * Both the starting and other pointer must be either in bounds or one
|
||||
/// * Both `self` and `origin` must be either in bounds or one
|
||||
/// byte past the end of the same [allocated object].
|
||||
///
|
||||
/// * Both pointers must be *derived from* a pointer to the same object.
|
||||
@ -646,6 +655,14 @@ impl<T: ?Sized> *const T {
|
||||
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
|
||||
/// such large allocations either.)
|
||||
///
|
||||
/// The requirement for pointers to be derived from the same allocated object is primarily
|
||||
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
|
||||
/// objects is not known at compile-time. However, the requirement also exists at
|
||||
/// runtime and may be exploited by optimizations. If you wish to compute the difference between
|
||||
/// pointers that are not guaranteed to be from the same allocation, use `(self as isize -
|
||||
/// origin as isize) / mem::size_of::<T>()`.
|
||||
// FIXME: recommend `addr()` instead of `as usize` once that is stable.
|
||||
///
|
||||
/// [`add`]: #method.add
|
||||
/// [allocated object]: crate::ptr#allocated-object
|
||||
///
|
||||
@ -703,7 +720,7 @@ impl<T: ?Sized> *const T {
|
||||
/// units of **bytes**.
|
||||
///
|
||||
/// This is purely a convenience for casting to a `u8` pointer and
|
||||
/// using [offset_from][pointer::offset_from] on it. See that method for
|
||||
/// using [`offset_from`][pointer::offset_from] on it. See that method for
|
||||
/// documentation and safety requirements.
|
||||
///
|
||||
/// For non-`Sized` pointees this operation considers only the data pointers,
|
||||
|
@ -781,7 +781,16 @@ impl<T: ?Sized> *mut T {
|
||||
/// Calculates the distance between two pointers. The returned value is in
|
||||
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
|
||||
///
|
||||
/// This function is the inverse of [`offset`].
|
||||
/// This is equivalent to `(self as isize - origin as isize) / (mem::size_of::<T>() as isize)`,
|
||||
/// except that it has a lot more opportunities for UB, in exchange for the compiler
|
||||
/// better understanding what you are doing.
|
||||
///
|
||||
/// The primary motivation of this method is for computing the `len` of an array/slice
|
||||
/// of `T` that you are currently representing as a "start" and "end" pointer
|
||||
/// (and "end" is "one past the end" of the array).
|
||||
/// In that case, `end.offset_from(start)` gets you the length of the array.
|
||||
///
|
||||
/// All of the following safety requirements are trivially satisfied for this usecase.
|
||||
///
|
||||
/// [`offset`]: pointer#method.offset-1
|
||||
///
|
||||
@ -790,7 +799,7 @@ impl<T: ?Sized> *mut T {
|
||||
/// If any of the following conditions are violated, the result is Undefined
|
||||
/// Behavior:
|
||||
///
|
||||
/// * Both the starting and other pointer must be either in bounds or one
|
||||
/// * Both `self` and `origin` must be either in bounds or one
|
||||
/// byte past the end of the same [allocated object].
|
||||
///
|
||||
/// * Both pointers must be *derived from* a pointer to the same object.
|
||||
@ -820,6 +829,14 @@ impl<T: ?Sized> *mut T {
|
||||
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
|
||||
/// such large allocations either.)
|
||||
///
|
||||
/// The requirement for pointers to be derived from the same allocated object is primarily
|
||||
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
|
||||
/// objects is not known at compile-time. However, the requirement also exists at
|
||||
/// runtime and may be exploited by optimizations. If you wish to compute the difference between
|
||||
/// pointers that are not guaranteed to be from the same allocation, use `(self as isize -
|
||||
/// origin as isize) / mem::size_of::<T>()`.
|
||||
// FIXME: recommend `addr()` instead of `as usize` once that is stable.
|
||||
///
|
||||
/// [`add`]: #method.add
|
||||
/// [allocated object]: crate::ptr#allocated-object
|
||||
///
|
||||
@ -875,7 +892,7 @@ impl<T: ?Sized> *mut T {
|
||||
/// units of **bytes**.
|
||||
///
|
||||
/// This is purely a convenience for casting to a `u8` pointer and
|
||||
/// using [offset_from][pointer::offset_from] on it. See that method for
|
||||
/// using [`offset_from`][pointer::offset_from] on it. See that method for
|
||||
/// documentation and safety requirements.
|
||||
///
|
||||
/// For non-`Sized` pointees this operation considers only the data pointers,
|
||||
|
Loading…
x
Reference in New Issue
Block a user