Skip to content

Commit d5505a7

Browse files
committed
also remove redundant requirements from offset()
1 parent 8e31c9a commit d5505a7

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<T: ?Sized> *const T {
623623
/// * The distance between the pointers, in bytes, must be an exact multiple
624624
/// of the size of `T`.
625625
///
626-
/// As a consequence, the absolute distance between the pointers, **in bytes**, computed on
626+
/// As a consequence, the absolute distance between the pointers, in bytes, computed on
627627
/// mathematical integers (without "wrapping around"), cannot overflow an `isize`. This is
628628
/// implied by the in-bounds requirement, and the fact that no allocated object can be larger
629629
/// than `isize::MAX`.

library/core/src/ptr/mut_ptr.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -404,37 +404,26 @@ impl<T: ?Sized> *mut T {
404404
if self.is_null() { None } else { Some(unsafe { &*(self as *const MaybeUninit<T>) }) }
405405
}
406406

407-
/// Calculates the offset from a pointer.
407+
/// Adds an offset to a pointer.
408408
///
409409
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
410410
/// offset of `3 * size_of::<T>()` bytes.
411411
///
412412
/// # Safety
413413
///
414-
/// If any of the following conditions are violated, the result is Undefined
415-
/// Behavior:
416-
///
417-
/// * If the computed offset, **in bytes**, is non-zero, then both the starting and resulting
418-
/// pointer must be either in bounds or at the end of the same [allocated object].
419-
/// (If it is zero, then the function is always well-defined.)
414+
/// The resulting pointer's address is computed as the address of the original pointer plus the
415+
/// computed offset, `count * size_of::<T>()`, with infinite precision.
420416
///
421-
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
417+
/// If the following condition is violated, the result is Undefined Behavior:
422418
///
423-
/// * The offset being in bounds cannot rely on "wrapping around" the address
424-
/// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
425-
///
426-
/// The compiler and standard library generally tries to ensure allocations
427-
/// never reach a size where an offset is a concern. For instance, `Vec`
428-
/// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
429-
/// `vec.as_ptr().add(vec.len())` is always safe.
419+
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
420+
/// [allocated object], and the entire memory range between `self` and the result must be in
421+
/// bounds of that allocated object
430422
///
431-
/// Most platforms fundamentally can't even construct such an allocation.
432-
/// For instance, no known 64-bit platform can ever serve a request
433-
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
434-
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
435-
/// more than `isize::MAX` bytes with things like Physical Address
436-
/// Extension. As such, memory acquired directly from allocators or memory
437-
/// mapped files *may* be too large to handle with this function.
423+
/// Allocated objects can never be larger than `isize::MAX` and they never "wrap around" the
424+
/// edge of the address space, so as a consequence of this, `count * size_of::<T>()` must
425+
/// fit in an `isize` and adding the computed offset to `self` produces a resulting address
426+
/// that fits into a `usize`.
438427
///
439428
/// Consider using [`wrapping_offset`] instead if these constraints are
440429
/// difficult to satisfy. The only advantage of this method is that it

0 commit comments

Comments
 (0)