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§
Sourceunsafe fn from_bytes(bytes: &[u8]) -> &Self
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.
Sourceunsafe fn hash<S: BuildHasher>(build_hasher: &S, bytes: &[u8]) -> u64
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.
Sourceunsafe fn eq(a: &[u8], b: &[u8]) -> bool
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§
Sourcefn as_byte_ptr(&self) -> NonNull<u8>
fn as_byte_ptr(&self) -> NonNull<u8>
Returns pointer to the instance.
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.