Skip to content

Commit ebe0407

Browse files
committed
improve offset_from docs
1 parent 939b143 commit ebe0407

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

library/core/src/ptr/const_ptr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,31 @@ impl<T: ?Sized> *const T {
320320
/// * Both pointers must be *derived from* a pointer to the same object.
321321
/// (See below for an example.)
322322
///
323-
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
324-
///
325323
/// * The distance between the pointers, in bytes, must be an exact multiple
326324
/// of the size of `T`.
327325
///
326+
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
327+
///
328328
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
329329
///
330-
/// The compiler and standard library generally try to ensure allocations
331-
/// never reach a size where an offset is a concern. For instance, `Vec`
332-
/// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
333-
/// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
330+
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
331+
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
332+
/// the last two conditions. The standard library also generally ensures that allocations
333+
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
334+
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
335+
/// always satisfies the last two conditions.
334336
///
335-
/// Most platforms fundamentally can't even construct such an allocation.
337+
/// Most platforms fundamentally can't even construct such a large allocation.
336338
/// For instance, no known 64-bit platform can ever serve a request
337339
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
338340
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
339341
/// more than `isize::MAX` bytes with things like Physical Address
340342
/// Extension. As such, memory acquired directly from allocators or memory
341343
/// mapped files *may* be too large to handle with this function.
344+
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
345+
/// such large allocations either.)
346+
///
347+
/// [`add`]: #method.add
342348
///
343349
/// # Panics
344350
///

library/core/src/ptr/mut_ptr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -491,25 +491,31 @@ impl<T: ?Sized> *mut T {
491491
/// * Both pointers must be *derived from* a pointer to the same object.
492492
/// (See below for an example.)
493493
///
494-
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
495-
///
496494
/// * The distance between the pointers, in bytes, must be an exact multiple
497495
/// of the size of `T`.
498496
///
497+
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
498+
///
499499
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
500500
///
501-
/// The compiler and standard library generally try to ensure allocations
502-
/// never reach a size where an offset is a concern. For instance, `Vec`
503-
/// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
504-
/// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
501+
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
502+
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
503+
/// the last two conditions. The standard library also generally ensures that allocations
504+
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
505+
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
506+
/// always satisfies the last two conditions.
505507
///
506-
/// Most platforms fundamentally can't even construct such an allocation.
508+
/// Most platforms fundamentally can't even construct such a large allocation.
507509
/// For instance, no known 64-bit platform can ever serve a request
508510
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
509511
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
510512
/// more than `isize::MAX` bytes with things like Physical Address
511513
/// Extension. As such, memory acquired directly from allocators or memory
512514
/// mapped files *may* be too large to handle with this function.
515+
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
516+
/// such large allocations either.)
517+
///
518+
/// [`add`]: #method.add
513519
///
514520
/// # Panics
515521
///

0 commit comments

Comments
 (0)