pub trait ByteKey: TrieKey {
type Borrowed<'a>: AsRef<[u8]> + 'a
where Self: 'a;
// Required methods
fn bytes(&self) -> &[u8] ⓘ;
fn from_bytes(bytes: &[u8]) -> Self;
fn as_borrowed<'a>(bytes: &'a [u8]) -> Self::Borrowed<'a>;
}Expand description
A key type that can be converted to and from a byte slice while preserving ordering.
Implementations must ensure that byte-order comparison matches the key
type’s natural ordering: for all a, b, a.bytes().cmp(b.bytes())
must equal a.cmp(b).
The from_bytes reconstruction is only ever called with byte sequences
originally produced by bytes, so implementations may assume valid
input. The same applies to as_borrowed: it is only ever called with
bytes that originated from a bytes() call of the same key type, so e.g.
the String impl may assume valid UTF-8 and skip validation.
ByteKey is a subtrait of TrieKey; the byte representation is also
available via TrieKey::as_bytes. The bytes method is the
ByteKey-specific accessor used by radix tries that manage their own key
storage (NibbleTrie, NibTrie); TrieKey::as_bytes is used by
BitTrie and the storage backends. Distinct names avoid ambiguity when
both traits are in scope.
Required Associated Types§
Sourcetype Borrowed<'a>: AsRef<[u8]> + 'a
where
Self: 'a
type Borrowed<'a>: AsRef<[u8]> + 'a where Self: 'a
Borrowed view of the key, constructible from &[u8] without allocation.
This is the natural zero-alloc form handed back by iteration: Vec<u8>
→ &'a [u8], String → &'a str. It
satisfies AsRef<[u8]> so callers can recover the raw bytes when
needed. The [Borrow] equivalence contract (Eq/Ord/Hash matching Self)
is already guaranteed by this trait’s byte-order invariant and is not
re-imposed as a bound here; the trie compares raw buf bytes directly.
Required Methods§
Sourcefn from_bytes(bytes: &[u8]) -> Self
fn from_bytes(bytes: &[u8]) -> Self
Reconstruct an owned key from its byte representation. Allocates.
from_bytes(k.bytes()) must produce a value equivalent to k. Use this
only when you need an owned K (e.g. collecting into a Vec<K>); for
iteration over keys already stored in the trie, prefer as_borrowed.
Sourcefn as_borrowed<'a>(bytes: &'a [u8]) -> Self::Borrowed<'a>
fn as_borrowed<'a>(bytes: &'a [u8]) -> Self::Borrowed<'a>
View bytes as the borrowed key form, with no allocation.
as_borrowed(k.bytes()) yields a value that compares equal to k. Only
ever called with bytes produced by a bytes() call of the same key
type, so impls may skip validation (e.g. the String impl assumes valid
UTF-8).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".