Delta

Trait Delta 

Source
pub unsafe trait Delta: 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

Delta trait generalizes differences in memory locations to types like i8 and i16

Note: certain invariants must be upheld to fulfill the unsafe contract of this trait, these invariants are detailed in each function

This trait is intended to be used with RelPtr

Required Associated Types§

Source

type Error

Error of Delta::sub

Required Methods§

Source

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

The difference between two pointers

Note: for all values of a: *mut u8, you must enforce that Delta::sub(a, a) == Delta::ZERO and that the following function does not panic for all values of a and b

 fn for_all_a_b(a: *mut u8, b: *mut u8) {
     if let Some(x) = Self::sub(a, b) {
         unsafe { assert_eq!(Self::add(x, b), a) }
     }
 }
Source

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

The difference between two pointers

Note: for all values of a: *mut u8, you must enforce that Delta::sub(a, a) == Delta::ZERO and that the following function does not panic for all values of a and b if the difference between a and b is valid

 fn for_all_a_b(a: *mut u8, b: *mut u8) {
     unsafe { assert_eq!(Self::add(Self::sub_unchecked(a, b), b), a) }
 }

Safety:

If the difference between a and b is not representable by Self is UB

Source

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

Adds the difference (in self) to the pointer a

Note: for all values of a: *mut u8, you must enforce that Delta::add(Delta::ZERO, a) == a and that the following function does not panic for all values of a and b

 fn for_all_a_b(a: *mut u8, b: *mut u8) {
     if let Some(x) = Self::sub(a, b) {
         unsafe { assert_eq!(Self::add(x, b), a) }
     }
 }
§Safety

TODO

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 Delta for i8

Source§

type Error = IntegerDeltaError

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 Delta for i16

Source§

type Error = IntegerDeltaError

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 Delta for i32

Source§

type Error = IntegerDeltaError

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 Delta for i64

Source§

type Error = IntegerDeltaError

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 Delta for i128

Source§

type Error = IntegerDeltaError

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 Delta for isize

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroI8

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroI16

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroI32

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroI64

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroI128

Source§

type Error = IntegerDeltaError

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 Delta for NonZeroIsize

Source§

type Error = IntegerDeltaError

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§