Skip to main content

Dropless

Trait Dropless 

Source
pub trait Dropless {
    // Required methods
    unsafe fn from_bytes(bytes: &[u8]) -> &Self;
    unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64;
    unsafe fn eq(a: &[u8], b: &[u8]) -> bool;

    // Provided methods
    fn as_byte_ptr(&self) -> NonNull<u8> { ... }
    fn layout(&self) -> Layout { ... }
}
Expand description

A trait for types that can be stored in a dropless interner.

The Dropless trait provides methods for working with types that can be stored in a DroplessInterner or DroplessInternSet. It defines how instances of a type are converted to and from raw byte representations, hashed, and compared for equality. This is useful for interning values without requiring ownership.

§Safety

Implementing the Dropless trait requires careful attention to alignment and memory safety. The methods in this trait are marked as unsafe because they rely on the caller to ensure that the provided byte slices are properly aligned and valid for the type. Misuse of these methods can lead to undefined behavior.

§Examples

use any_intern::Dropless;
use std::alloc::Layout;
use std::ptr::NonNull;

#[derive(PartialEq, Eq, Hash, Debug)]
struct MyType(u32);

impl Dropless for MyType {
    fn as_byte_ptr(&self) -> NonNull<u8> {
        NonNull::from_ref(self).cast::<u8>()
    }

    fn layout(&self) -> Layout {
        Layout::for_value(self)
    }

    unsafe fn from_bytes(bytes: &[u8]) -> &Self {
        let ptr = bytes.as_ptr().cast::<Self>();
        unsafe { ptr.as_ref().unwrap_unchecked() }
    }

    unsafe fn hash<S: std::hash::BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64 {
        let this = unsafe { Self::from_bytes(bytes) };
        build_hasher.hash_one(this)
    }

    unsafe fn eq(a: &[u8], b: &[u8]) -> bool {
        unsafe { Self::from_bytes(a) == Self::from_bytes(b) }
    }
}

Required Methods§

Source

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Converts a byte slice into a reference to the type.

The byte slice is guaranteed to be well aligned and correct data for the type.

§Safety

Undefined behavior if any conditions below are not met.

  • Implementation should interpret the byte slice into the type correctly.
  • Caller should give well aligned data for the type.
Source

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Computes a hash value for the type using the provided byte slice.

The byte slice is guaranteed to be well aligned and correct data for the type.

§Safety

Undefined behavior if any conditions below are not met.

  • Implementation should interpret the byte slice into the type correctly.
  • Caller should give well aligned data for the type.
Source

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Compares two byte slices for equality as instances of the type.

The byte slices are guaranteed to be well aligned and correct data for the type.

§Safety

Undefined behavior if any conditions below are not met.

  • Implementation should interpret the byte slice into the type correctly.
  • Caller should give well aligned data for the type.

Provided Methods§

Source

fn as_byte_ptr(&self) -> NonNull<u8>

Returns pointer to the instance.

Source

fn layout(&self) -> Layout

Returns layout of the type.

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 Dropless for bool

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for char

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for i8

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for i16

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for i32

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for i64

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for i128

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for isize

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for str

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for u8

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for u16

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for u32

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for u64

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for u128

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl Dropless for usize

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl<T: Dropless + Hash + Eq> Dropless for Option<T>

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl<T: Dropless + Hash + Eq> Dropless for [T]

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl<T: Dropless + Hash + Eq, E: Dropless + Hash + Eq> Dropless for Result<T, E>

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Source§

impl<T: Dropless + Hash + Eq, const N: usize> Dropless for [T; N]

Source§

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Source§

unsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64

Source§

unsafe fn eq(a: &[u8], b: &[u8]) -> bool

Implementors§