TrieHard

Enum TrieHard 

Source
pub enum TrieHard<'a, T> {
    U8(TrieHardSized<'a, T, u8>),
    U16(TrieHardSized<'a, T, u16>),
    U32(TrieHardSized<'a, T, u32>),
    U64(TrieHardSized<'a, T, u64>),
    U128(TrieHardSized<'a, T, u128>),
    U256(TrieHardSized<'a, T, U256>),
}
Expand description

Enumeration of all the possible sizes of trie-hard tries. An instance of this enum can be created from any set of arbitrary string or byte slices. The variant returned will depend on the number of distinct bytes contained in the set.

let trie = ["and", "ant", "dad", "do", "dot"]
    .into_iter()
    .collect::<TrieHard<'_, _>>();

assert!(trie.get("dad").is_some());
assert!(trie.get("do").is_some());
assert!(trie.get("don't").is_none());

Note: This enum has a very large variant which dominates the size for the enum. That means that a small trie using u8s for storage will take up way (32x) more storage than it needs to. If you are concerned about extra space (and you know ahead of time the trie size needed), you should extract the inner, [TrieHardSized] which will use only the size required.

Variants§

§

U8(TrieHardSized<'a, T, u8>)

Trie-hard using u8s for storage. For sets with 1..=8 unique bytes

§

U16(TrieHardSized<'a, T, u16>)

Trie-hard using u16s for storage. For sets with 9..=16 unique bytes

§

U32(TrieHardSized<'a, T, u32>)

Trie-hard using u32s for storage. For sets with 17..=32 unique bytes

§

U64(TrieHardSized<'a, T, u64>)

Trie-hard using u64s for storage. For sets with 33..=64 unique bytes

§

U128(TrieHardSized<'a, T, u128>)

Trie-hard using u128s for storage. For sets with 65..=126 unique bytes

§

U256(TrieHardSized<'a, T, U256>)

Trie-hard using U256s for storage. For sets with 129.. unique bytes

Implementations§

Source§

impl<'a, T> TrieHard<'a, T>
where T: 'a + Copy,

Source

pub fn new(values: Vec<(&'a [u8], T)>) -> Self

Create an instance of a trie-hard trie with the given keys and values. The variant returned will be determined based on the number of unique bytes in the keys.

let trie = TrieHard::new(vec![
    (b"and", 0),
    (b"ant", 1),
    (b"dad", 2),
    (b"do", 3),
    (b"dot", 4)
]);

// Only 5 unique characters produces a u8 TrieHard
assert!(matches!(trie, TrieHard::U8(_)));

assert_eq!(trie.get("dad"), Some(2));
assert_eq!(trie.get("do"), Some(3));
assert!(trie.get("don't").is_none());
Source

pub fn get<K: AsRef<[u8]>>(&self, raw_key: K) -> Option<T>

Get the value stored for the given key. Any key type can be used here as long as the type implements AsRef<[u8]>. The byte slice referenced will serve as the actual key.

let trie = ["and", "ant", "dad", "do", "dot"]
    .into_iter()
    .collect::<TrieHard<'_, _>>();

assert!(trie.get("dad".to_owned()).is_some());
assert!(trie.get(b"do").is_some());
assert!(trie.get(b"don't".to_vec()).is_none());
Source

pub fn get_from_bytes(&self, key: &[u8]) -> Option<T>

Get the value stored for the given byte-slice key

let trie = ["and", "ant", "dad", "do", "dot"]
    .into_iter()
    .collect::<TrieHard<'_, _>>();

assert!(trie.get_from_bytes(b"dad").is_some());
assert!(trie.get_from_bytes(b"do").is_some());
assert!(trie.get_from_bytes(b"don't").is_none());
Source

pub fn iter(&self) -> TrieIter<'_, 'a, T>

Create an iterator over the entire trie. Emitted items will be ordered by their keys

let trie = ["dad", "ant", "and", "dot", "do"]
    .into_iter()
    .collect::<TrieHard<'_, _>>();

assert_eq!(
    trie.iter().map(|(_, v)| v).collect::<Vec<_>>(),
    ["and", "ant", "dad", "do", "dot"]
);

Create an iterator over the portion of the trie starting with the given prefix

let trie = ["dad", "ant", "and", "dot", "do"]
    .into_iter()
    .collect::<TrieHard<'_, _>>();

assert_eq!(
    trie.prefix_search("d").map(|(_, v)| v).collect::<Vec<_>>(),
    ["dad", "do", "dot"]
);

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for TrieHard<'a, T>

Source§

fn clone(&self) -> TrieHard<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for TrieHard<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Default for TrieHard<'a, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> FromIterator<&'a T> for TrieHard<'a, &'a T>
where T: 'a + AsRef<[u8]> + ?Sized,

Source§

fn from_iter<I: IntoIterator<Item = &'a T>>(values: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for TrieHard<'a, T>

§

impl<'a, T> RefUnwindSafe for TrieHard<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for TrieHard<'a, T>
where T: Send,

§

impl<'a, T> Sync for TrieHard<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for TrieHard<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for TrieHard<'a, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.