pub unsafe trait Storable {
type BytesRef<'a>: GenericCow<Borrowed = [u8]>
where Self: 'a;
type AlignedRef<'a>: GenericCow<Borrowed = Self>;
const CONST_BYTES_LEN: bool;
const TRIVIAL_CMP: bool = false;
const OPTIMIZE_INT: bool = false;
// Required methods
fn to_bytes(&self) -> Self::BytesRef<'_>;
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>;
unsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering;
// Provided methods
fn bytes_len(&self) -> usize { ... }
unsafe fn cmp_bytes_by_ord_unchecked(a: &[u8], b: &[u8]) -> Ordering
where Self: Ord { ... }
}
Expand description
Types that can be stored
Any type that implements Storable
can be used as a key or value in a
Db
.
Types that have a fixed-length byte representation should additionally
implement StorableConstBytesLen
.
Several constants must be set correctly to enable various optimizations.
Safety
CONST_BYTES_LEN
must only betrue
ifto_bytes
always returns a pointer to a byte slice with the same length.TRIVIAL_CMP
must only betrue
ifcmp_bytes_unchecked
, when receiving two arguments which have been returned by theto_bytes
method of the same type, performs a lexicographical comparison of those two byte slices.OPTIMIZE_INT
must only be true if the type is equivalent toc_uint
or the C typesize_t
and the byte representation is in native byte order.to_bytes
must return a byte representation that can be safely passed tofrom_bytes_unchecked
(of the same type).cmp_bytes_unchecked
must be stable, i.e. always return the same result for the same input (for the same type).cmp_bytes_unchecked
must never unwind.
Required Associated Types§
sourcetype BytesRef<'a>: GenericCow<Borrowed = [u8]>
where
Self: 'a
type BytesRef<'a>: GenericCow<Borrowed = [u8]> where Self: 'a
Byte representation as GenericCow
This can be a simple reference (&'a Self
) or a
a smart-pointer (like Owned<[u8]>
) which drops the byte
representation when the smart-pointer is dropped.
sourcetype AlignedRef<'a>: GenericCow<Borrowed = Self>
type AlignedRef<'a>: GenericCow<Borrowed = Self>
Aligned version of Self
as GenericCow
This can be a simple reference (&'a Self
) if there
are no requirements for memory alignment, or can be a smart-pointer
(like Owned<Self>
) which drops the re-aligned copy when the
smart-pointer is dropped.
Required Associated Constants§
sourceconst CONST_BYTES_LEN: bool
const CONST_BYTES_LEN: bool
Does byte representation have fixed length?
If this constant is true
, then trait StorableConstBytesLen
should
also be implemented.
Provided Associated Constants§
sourceconst TRIVIAL_CMP: bool = false
const TRIVIAL_CMP: bool = false
Does Storable::cmp_bytes_unchecked
perform a trivial (byte wise)
lexicographical comparison?
sourceconst OPTIMIZE_INT: bool = false
const OPTIMIZE_INT: bool = false
Is type equivalent to c_uint
or the C type size_t
, and is its
byte representation in native byte order?
Required Methods§
sourceunsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>
Converts from byte slice
Safety
The passed byte representation (bytes
) must have been created with
<Self as Storable>::to_bytes
on the same
platform (same endianess / word size).
sourceunsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering
unsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering
Compares byte representation
Safety
The passed byte representations (a
and b
) must have been created
with <Self as Storable>::to_bytes
on the same
platform (same endianess / word size).
Provided Methods§
sourceunsafe fn cmp_bytes_by_ord_unchecked(a: &[u8], b: &[u8]) -> Orderingwhere
Self: Ord,
unsafe fn cmp_bytes_by_ord_unchecked(a: &[u8], b: &[u8]) -> Orderingwhere Self: Ord,
Compares byte representation using Ord
This function is provided for convenient implementation of
Storable::cmp_bytes_unchecked
where desired.
Safety
The passed byte representations (a
and b
) must have been created
with <Self as Storable>::to_bytes
on the same
platform (same endianess / word size).