pub unsafe trait Offset: Copy + Eq {
type Error;
// Required methods
fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>;
unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self;
unsafe fn add(self, a: *const u8) -> *mut u8;
}
Expand description
Trait for types that can represent pointer differences.
Generalizes pointer arithmetic to integer types like i8
, i16
, i32
.
Used internally by SelfRef
for offset-based pointer storage.
§Safety
Implementations must maintain these invariants:
sub(a, a) == ZERO
for all pointersa
add(sub(a, b), b) == a
whensub(a, b)
succeedsadd(ZERO, a) == a
for all pointersa
Required Associated Types§
Required Methods§
Sourcefn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>
fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>
Computes the difference between two pointers.
Returns Err
if the difference cannot be represented in Self
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.