Trait Offset

Source
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 pointers a
  • add(sub(a, b), b) == a when sub(a, b) succeeds
  • add(ZERO, a) == a for all pointers a

Required Associated Types§

Source

type Error

Error type returned when pointer difference cannot be represented.

Required Methods§

Source

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.

Source

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Computes pointer difference without bounds checking.

§Safety

The difference between a and b must be representable in Self.

Source

unsafe fn add(self, a: *const u8) -> *mut u8

Adds the offset to a base pointer.

§Safety

The resulting pointer must be valid for the intended use.

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.

Implementations on Foreign Types§

Source§

impl Offset for i8

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for i16

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for i32

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for i64

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for i128

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for isize

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroI8

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroI16

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroI32

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroI64

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroI128

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Source§

impl Offset for NonZeroIsize

Source§

type Error = IntegerOffsetError

Source§

fn sub(a: *mut u8, b: *mut u8) -> Result<Self, Self::Error>

Source§

unsafe fn sub_unchecked(a: *mut u8, b: *mut u8) -> Self

Source§

unsafe fn add(self, a: *const u8) -> *mut u8

Implementors§