@@ -404,37 +404,26 @@ impl<T: ?Sized> *mut T {
404
404
if self . is_null ( ) { None } else { Some ( unsafe { & * ( self as * const MaybeUninit < T > ) } ) }
405
405
}
406
406
407
- /// Calculates the offset from a pointer.
407
+ /// Adds an offset to a pointer.
408
408
///
409
409
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
410
410
/// offset of `3 * size_of::<T>()` bytes.
411
411
///
412
412
/// # Safety
413
413
///
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.
420
416
///
421
- /// * The computed offset, **in bytes**, cannot overflow an `isize`.
417
+ /// If the following condition is violated, the result is Undefined Behavior:
422
418
///
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
430
422
///
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`.
438
427
///
439
428
/// Consider using [`wrapping_offset`] instead if these constraints are
440
429
/// difficult to satisfy. The only advantage of this method is that it
0 commit comments