#[repr(transparent)]
pub struct ZeroTriePerfectHash<Store: ?Sized> { /* private fields */ }
Expand description

A data structure that compactly maps from byte strings to integers.

For more information, see ZeroTrie.

Examples

use litemap::LiteMap;
use zerotrie::ZeroTriePerfectHash;

let mut map = LiteMap::<&[u8], usize>::new_vec();
map.insert("foo".as_bytes(), 1);
map.insert("bår".as_bytes(), 2);
map.insert("båzzøø".as_bytes(), 3);

let trie = ZeroTriePerfectHash::try_from(&map)?;

assert_eq!(trie.get("foo".as_bytes()), Some(1));
assert_eq!(trie.get("bår".as_bytes()), Some(2));
assert_eq!(trie.get("båzzøø".as_bytes()), Some(3));
assert_eq!(trie.get("bazzoo".as_bytes()), None);

Implementations§

source§

impl<Store> ZeroTriePerfectHash<Store>

source

pub const fn into_zerotrie(self) -> ZeroTrie<Store>

Wrap this specific ZeroTrie variant into a ZeroTrie.

source

pub const fn from_store(store: Store) -> Self

Create a trie directly from a store.

If the store does not contain valid bytes, unexpected behavior may occur.

source

pub fn take_store(self) -> Store

Takes the byte store from this trie.

source

pub fn convert_store<X: From<Store>>(self) -> ZeroTriePerfectHash<X>

Converts this trie’s store to a different store implementing the From trait.

For example, use this to change ZeroTriePerfectHash<Vec<u8>> to ZeroTriePerfectHash<Cow<[u8]>>.

Examples
use std::borrow::Cow;
use zerotrie::ZeroTriePerfectHash;

let trie: ZeroTriePerfectHash<Vec<u8>> = ZeroTriePerfectHash::from_bytes(b"abc\x85").to_owned();
let cow: ZeroTriePerfectHash<Cow<[u8]>> = trie.convert_store();

assert_eq!(cow.get(b"abc"), Some(5));
source§

impl<Store> ZeroTriePerfectHash<Store>where Store: AsRef<[u8]> + ?Sized,

source

pub fn get<K>(&self, key: K) -> Option<usize>where K: AsRef<[u8]>,

Queries the trie for a string.

source

pub fn is_empty(&self) -> bool

Returns true if the trie is empty.

source

pub fn byte_len(&self) -> usize

Returns the size of the trie in number of bytes.

To get the number of keys in the trie, use .iter().count():

use zerotrie::ZeroTriePerfectHash;

// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::from_bytes(b"abc\x80def\x81");

assert_eq!(8, trie.byte_len());
assert_eq!(2, trie.iter().count());
source

pub fn as_bytes(&self) -> &[u8]

Returns the bytes contained in the underlying store.

source

pub fn as_borrowed(&self) -> &ZeroTriePerfectHash<[u8]>

Returns this trie as a reference transparent over a byte slice.

source§

impl<Store> ZeroTriePerfectHash<Store>where Store: AsRef<[u8]> + ?Sized,

source

pub fn to_owned(&self) -> ZeroTriePerfectHash<Vec<u8>>

Converts a possibly-borrowed $name to an owned one.

Enabled with the alloc Cargo feature.

Examples
use zerotrie::ZeroTriePerfectHash;

let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::from_bytes(b"abc\x85");
let owned: ZeroTriePerfectHash<Vec<u8>> = trie.to_owned();

assert_eq!(trie.get(b"abc"), Some(5));
assert_eq!(owned.get(b"abc"), Some(5));
source

pub fn iter(&self) -> impl Iterator<Item = (Vec<u8>, usize)> + '_

Returns an iterator over the key/value pairs in this trie.

Enabled with the alloc Cargo feature.

Examples
use zerotrie::ZeroTriePerfectHash;

// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTriePerfectHash<[u8]> = ZeroTriePerfectHash::from_bytes(b"abc\x80def\x81");

let mut it = trie.iter();
assert_eq!(it.next(), Some(("abc".into(), 0)));
assert_eq!(it.next(), Some(("abcdef".into(), 1)));
assert_eq!(it.next(), None);
source§

impl ZeroTriePerfectHash<[u8]>

source

pub fn from_bytes(trie: &[u8]) -> &Self

Casts from a byte slice to a reference to a trie with the same lifetime.

If the bytes are not a valid trie, unexpected behavior may occur.

source§

impl<Store> ZeroTriePerfectHash<Store>where Store: AsRef<[u8]> + ?Sized,

source

pub fn to_btreemap(&self) -> BTreeMap<Vec<u8>, usize>

Exports the data from this ZeroTrie type into a BTreeMap.

Enabled with the alloc Cargo feature.

Examples
use zerotrie::ZeroTriePerfectHash;
use std::collections::BTreeMap;

let trie = ZeroTriePerfectHash::from_bytes(b"abc\x81def\x82");
let items = trie.to_btreemap();

assert_eq!(items.len(), 2);

let recovered_trie: ZeroTriePerfectHash<Vec<u8>> = items
    .into_iter()
    .collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());
source§

impl<Store> ZeroTriePerfectHash<Store>where Store: AsRef<[u8]> + ?Sized,

source

pub fn to_litemap(&self) -> LiteMap<Vec<u8>, usize>

Exports the data from this ZeroTrie type into a LiteMap.

Enabled with the litemap Cargo feature.

Examples
use zerotrie::ZeroTriePerfectHash;
use litemap::LiteMap;

let trie = ZeroTriePerfectHash::from_bytes(b"abc\x81def\x82");

let items = trie.to_litemap();
assert_eq!(items.len(), 2);

let recovered_trie: ZeroTriePerfectHash<Vec<u8>> = items
    .iter()
    .map(|(k, v)| (k, *v))
    .collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());

Trait Implementations§

source§

impl<Store> Bake for ZeroTriePerfectHash<Store>where Store: Bake + ?Sized,

source§

fn bake(&self, env: &CrateEnv) -> TokenStream

Returns a TokenStream that would evaluate to self. Read more
source§

impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<&[u8]>

source§

fn borrow(&self) -> &ZeroTriePerfectHash<[u8]>

Immutably borrows from an owned value. Read more
source§

impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Box<[u8]>>

source§

fn borrow(&self) -> &ZeroTriePerfectHash<[u8]>

Immutably borrows from an owned value. Read more
source§

impl Borrow<ZeroTriePerfectHash<[u8]>> for ZeroTriePerfectHash<Vec<u8>>

source§

fn borrow(&self) -> &ZeroTriePerfectHash<[u8]>

Immutably borrows from an owned value. Read more
source§

impl<Store: Clone + ?Sized> Clone for ZeroTriePerfectHash<Store>

source§

fn clone(&self) -> ZeroTriePerfectHash<Store>

Returns a copy 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<Store: Debug + ?Sized> Debug for ZeroTriePerfectHash<Store>

source§

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

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

impl<Store: Default + ?Sized> Default for ZeroTriePerfectHash<Store>

source§

fn default() -> ZeroTriePerfectHash<Store>

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

impl<'de, 'data, Store> Deserialize<'de> for ZeroTriePerfectHash<Store>where Store: From<&'data [u8]> + From<Vec<u8>> + 'data, 'de: 'data,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<Store> From<&ZeroTriePerfectHash<Store>> for BTreeMap<Vec<u8>, usize>where Store: AsRef<[u8]> + ?Sized,

source§

fn from(other: &ZeroTriePerfectHash<Store>) -> Self

Converts to this type from the input type.
source§

impl<Store> From<&ZeroTriePerfectHash<Store>> for LiteMap<Vec<u8>, usize>where Store: AsRef<[u8]> + ?Sized,

source§

fn from(other: &ZeroTriePerfectHash<Store>) -> Self

Converts to this type from the input type.
source§

impl<'a, K> FromIterator<(K, usize)> for ZeroTriePerfectHash<Vec<u8>>where K: AsRef<[u8]>,

source§

fn from_iter<T: IntoIterator<Item = (K, usize)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<Store: PartialEq + ?Sized> PartialEq for ZeroTriePerfectHash<Store>

source§

fn eq(&self, other: &ZeroTriePerfectHash<Store>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Store> Serialize for ZeroTriePerfectHash<Store>where Store: AsRef<[u8]>,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl ToOwned for ZeroTriePerfectHash<[u8]>

source§

fn to_owned(&self) -> Self::Owned

This impl allows ZeroTriePerfectHash to be used inside of a Cow.

Note that it is also possible to use ZeroTriePerfectHash<ZeroVec<u8>> for a similar result.

Enabled with the alloc Cargo feature.

Examples
use std::borrow::Cow;
use zerotrie::ZeroTriePerfectHash;

let trie: Cow<ZeroTriePerfectHash<[u8]>> = Cow::Borrowed(ZeroTriePerfectHash::from_bytes(b"abc\x85"));
assert_eq!(trie.get(b"abc"), Some(5));
§

type Owned = ZeroTriePerfectHash<Box<[u8]>>

The resulting type after obtaining ownership.
1.63.0 · source§

fn clone_into(&self, target: &mut Self::Owned)

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

impl<'a, K> TryFrom<&'a BTreeMap<K, usize>> for ZeroTriePerfectHash<Vec<u8>>where K: Borrow<[u8]>,

§

type Error = Error

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

fn try_from(map: &'a BTreeMap<K, usize>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a, K, S> TryFrom<&'a LiteMap<K, usize, S>> for ZeroTriePerfectHash<Vec<u8>>where K: Borrow<[u8]>, S: StoreIterable<'a, K, usize>,

§

type Error = Error

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

fn try_from(map: &'a LiteMap<K, usize, S>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<Store> VarULE for ZeroTriePerfectHash<Store>where Store: VarULE,

source§

fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError>

Validates a byte slice, &[u8]. Read more
source§

unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self

Takes a byte slice, &[u8], and return it as &Self with the same lifetime, assuming that this byte slice has previously been run through Self::parse_byte_slice() with success. Read more
source§

fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError>

Parses a byte slice, &[u8], and return it as &Self with the same lifetime. Read more
source§

fn as_byte_slice(&self) -> &[u8]

Given &Self, returns a &[u8] with the same lifetime. Read more
source§

fn to_boxed(&self) -> Box<Self>

Allocate on the heap as a Box<T>
source§

impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTriePerfectHash<Store1>> for ZeroTriePerfectHash<Store2>where Store2: ZeroFrom<'zf, Store1>,

source§

fn zero_from(other: &'zf ZeroTriePerfectHash<Store1>) -> Self

Clone the other C into a struct that may retain references into C.
source§

impl<Store: Copy + ?Sized> Copy for ZeroTriePerfectHash<Store>

source§

impl<Store: Eq + ?Sized> Eq for ZeroTriePerfectHash<Store>

source§

impl<Store: ?Sized> StructuralEq for ZeroTriePerfectHash<Store>

source§

impl<Store: ?Sized> StructuralPartialEq for ZeroTriePerfectHash<Store>

Auto Trait Implementations§

§

impl<Store: ?Sized> RefUnwindSafe for ZeroTriePerfectHash<Store>where Store: RefUnwindSafe,

§

impl<Store: ?Sized> Send for ZeroTriePerfectHash<Store>where Store: Send,

§

impl<Store: ?Sized> Sync for ZeroTriePerfectHash<Store>where Store: Sync,

§

impl<Store: ?Sized> Unpin for ZeroTriePerfectHash<Store>where Store: Unpin,

§

impl<Store: ?Sized> UnwindSafe for ZeroTriePerfectHash<Store>where Store: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> EncodeAsVarULE<T> for Twhere T: VarULE + ?Sized,

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more
source§

fn encode_var_ule_len(&self) -> usize

Return the length, in bytes, of the corresponding VarULE type
source§

fn encode_var_ule_write(&self, dst: &mut [u8])

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len()
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,