Sha256: 66be06fde8558429da23a290584d4b9fae665bf64c2578db4fe5f5f3ee864869

Contents?: true

Size: 1.24 KB

Versions: 24

Compression:

Stored size: 1.24 KB

Contents

/// A trait for adding some helper routines to pointers.
pub(crate) trait Pointer {
    /// Returns the distance, in units of `T`, between `self` and `origin`.
    ///
    /// # Safety
    ///
    /// Same as `ptr::offset_from` in addition to `self >= origin`.
    unsafe fn distance(self, origin: Self) -> usize;

    /// Casts this pointer to `usize`.
    ///
    /// Callers should not convert the `usize` back to a pointer if at all
    /// possible. (And if you believe it's necessary, open an issue to discuss
    /// why. Otherwise, it has the potential to violate pointer provenance.)
    /// The purpose of this function is just to be able to do arithmetic, i.e.,
    /// computing offsets or alignments.
    fn as_usize(self) -> usize;
}

impl<T> Pointer for *const T {
    unsafe fn distance(self, origin: *const T) -> usize {
        // TODO: Replace with `ptr::sub_ptr` once stabilized.
        usize::try_from(self.offset_from(origin)).unwrap_unchecked()
    }

    fn as_usize(self) -> usize {
        self as usize
    }
}

impl<T> Pointer for *mut T {
    unsafe fn distance(self, origin: *mut T) -> usize {
        (self as *const T).distance(origin as *const T)
    }

    fn as_usize(self) -> usize {
        (self as *const T).as_usize()
    }
}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-29.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-28.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-27.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-26.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-25.0.2 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-25.0.1 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-25.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-24.0.0 ./ext/cargo-vendor/aho-corasick-1.1.3/src/packed/ext.rs
wasmtime-23.0.2 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-22.0.0 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-21.0.1 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-20.0.2 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-20.0.0 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-18.0.3 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-17.0.1 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-17.0.0 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-16.0.0 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-15.0.1 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs
wasmtime-15.0.0 ./ext/cargo-vendor/aho-corasick-1.1.2/src/packed/ext.rs