[][src]Trait rel_ptr::Delta

pub unsafe trait Delta: Copy + Eq {
    type Error;
    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; }

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

Associated Types

type Error

Error of Delta::sub

Loading content...

Required methods

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

This example is not tested
 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) }
     }
 }

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

This example is not tested
 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

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

This example is not tested
 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

Loading content...

Implementations on Foreign Types

impl Delta for i8[src]

type Error = IntegerDeltaError

impl Delta for i16[src]

type Error = IntegerDeltaError

impl Delta for i32[src]

type Error = IntegerDeltaError

impl Delta for i64[src]

type Error = IntegerDeltaError

impl Delta for i128[src]

type Error = IntegerDeltaError

impl Delta for isize[src]

type Error = IntegerDeltaError

impl Delta for NonZeroI8[src]

type Error = IntegerDeltaError

impl Delta for NonZeroI16[src]

type Error = IntegerDeltaError

impl Delta for NonZeroI32[src]

type Error = IntegerDeltaError

impl Delta for NonZeroI64[src]

type Error = IntegerDeltaError

impl Delta for NonZeroI128[src]

type Error = IntegerDeltaError

impl Delta for NonZeroIsize[src]

type Error = IntegerDeltaError

Loading content...

Implementors

Loading content...