blob: b1add36d37735fdd060d5f01c58215d40b62063f [file] [view]
Adds a signed offset to a pointer.
`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 offset in bytes, `count * size_of::<T>()`, computed on mathematical integers (without
"wrapping around"), must fit in an `isize`.
* Let `result` be `self.addr() + count * size_of::<T>()`, computed on mathematical integers.
This must fit in a `usize`.
* If the computed offset is non-zero, then `self` must be [derived from][crate::ptr#provenance] a pointer to some
[allocation], and the entire memory range between `self` and `result`
(i.e., `min(self.addr(), result)..max(self.addr(), result)`)
must be in bounds of that allocation.
Allocations can never be larger than `isize::MAX` bytes and they can only contain addresses
representable by `usize`, so technically the last condition implies the first two. This implies, for
instance, that `vec.as_ptr().offset(vec.len() as isize)` (for `vec: Vec<T>`) is always safe.
[allocation]: crate::ptr#allocation