pub unsafe trait Storable: Ord {
type AlignedRef<'a>: GenericCow<Self>;
type BytesRef<'a>: Deref<Target = [u8]>
where
Self: 'a;
const CONST_BYTES_LEN: bool;
const OPTIMIZE_INT: bool = false;
const TRIVIAL_CMP: bool = false;
fn to_bytes(&self) -> Self::BytesRef<'_>;
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>;
fn bytes_len(&self) -> usize { ... }
unsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering { ... }
}
Expand description
Types that can be stored
Any type that implements Storable
can be used as a key or value in a
Db
. Implementation is unsafe
and must be correct and not
change between (re-)opening environments.
Types that have a fixed-length byte representation should additionally
implement StorableConstBytesLen
.
The generic associated type AlignedRef
is an
ordinary shared reference or a smart-pointer holding a copy for the
purpose of memory alignment. The generic associated type
BytesRef
is usually &[u8]
(where bytes can be
read directly from memory) or Vec<u8>
(where bytes need to be copied).
Several constants must be set correctly to enable various optimizations.
Required Associated Types
type AlignedRef<'a>: GenericCow<Self>
type AlignedRef<'a>: GenericCow<Self>
Pointer to aligned version of Self
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>
or Vec
) which drops the re-aligned
copy when the smart-pointer is dropped.
Required Associated Constants
const CONST_BYTES_LEN: bool
const CONST_BYTES_LEN: bool
Does byte representation have fixed length?
Provided Associated Constants
const OPTIMIZE_INT: bool = false
const OPTIMIZE_INT: bool = false
const TRIVIAL_CMP: bool = false
const TRIVIAL_CMP: bool = false
Is Ord
consistent with lexicographical sorting of binary representation?
Required Methods
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self::AlignedRef<'_>
Converts from byte slice
Provided Methods
unsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering
unsafe fn cmp_bytes_unchecked(a: &[u8], b: &[u8]) -> Ordering
Compares byte representation