Skip to content

Commit

Permalink
various updates based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 11, 2024
1 parent 5835079 commit 0e627de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
27 changes: 17 additions & 10 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ impl<T: ?Sized> *const T {
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -399,7 +399,7 @@ impl<T: ?Sized> *const T {
unsafe { intrinsics::offset(self, count) }
}

/// Calculates the offset from a pointer in bytes.
/// Adds a signed offset in bytes to a pointer.
///
/// `count` is in units of **bytes**.
///
Expand Down Expand Up @@ -810,15 +810,19 @@ impl<T: ?Sized> *const T {

/// Adds an offset to a pointer.
///
/// This can only move the pointer forward (or not move it). If you need to move forward or
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
/// which takes a signed offset.
///
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -861,7 +865,7 @@ impl<T: ?Sized> *const T {
unsafe { intrinsics::offset(self, count) }
}

/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
/// Adds an offset in bytes to a pointer.
///
/// `count` is in units of bytes.
///
Expand All @@ -884,15 +888,19 @@ impl<T: ?Sized> *const T {

/// Subtracts an offset from a pointer.
///
/// This can only move the pointer backward (or not move it). If you need to move forward or
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
/// which takes a signed offset.
///
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -943,8 +951,7 @@ impl<T: ?Sized> *const T {
}
}

/// Calculates the offset from a pointer in bytes (convenience for
/// `.byte_offset((count as isize).wrapping_neg())`).
/// Subtracts an offset in bytes from a pointer.
///
/// `count` is in units of bytes.
///
Expand Down
27 changes: 17 additions & 10 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ impl<T: ?Sized> *mut T {
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -399,7 +399,7 @@ impl<T: ?Sized> *mut T {
unsafe { intrinsics::offset(self, count) }
}

/// Calculates the offset from a pointer in bytes.
/// Adds a signed offset in bytes to a pointer.
///
/// `count` is in units of **bytes**.
///
Expand Down Expand Up @@ -891,15 +891,19 @@ impl<T: ?Sized> *mut T {

/// Adds an offset to a pointer.
///
/// This can only move the pointer forward (or not move it). If you need to move forward or
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
/// which takes a signed offset.
///
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -942,7 +946,7 @@ impl<T: ?Sized> *mut T {
unsafe { intrinsics::offset(self, count) }
}

/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
/// Adds an offset in bytes to a pointer.
///
/// `count` is in units of bytes.
///
Expand All @@ -965,15 +969,19 @@ impl<T: ?Sized> *mut T {

/// Subtracts an offset from a pointer.
///
/// This can only move the pointer backward (or not move it). If you need to move forward or
/// backward depending on the value, then you might want [`offset`](#method.offset) instead
/// which takes a signed offset.
///
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined Behavior:
///
/// * The computed offset, `count * size_of::<T>()` bytes (using unbounded arithmetic),
/// must fit in an `isize`.
/// * The offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
/// "wrapping around"), must fit in an `isize`.
///
/// * If the computed offset is non-zero, then `self` must be derived from a pointer to some
/// [allocated object], and the entire memory range between `self` and the result must be in
Expand Down Expand Up @@ -1024,8 +1032,7 @@ impl<T: ?Sized> *mut T {
}
}

/// Calculates the offset from a pointer in bytes (convenience for
/// `.byte_offset((count as isize).wrapping_neg())`).
/// Subtracts an offset in bytes from a pointer.
///
/// `count` is in units of bytes.
///
Expand Down

0 comments on commit 0e627de

Please sign in to comment.