#[repr(transparent)]pub struct ZeroTrieSimpleAscii<Store>where
    Store: ?Sized,{ /* private fields */ }Expand description
A data structure that compactly maps from ASCII strings to integers.
For more information, see ZeroTrie.
§Examples
use litemap::LiteMap;
use zerotrie::ZeroTrieSimpleAscii;
let mut map = LiteMap::new_vec();
map.insert(&b"foo"[..], 1);
map.insert(b"bar", 2);
map.insert(b"bazzoo", 3);
let trie = ZeroTrieSimpleAscii::try_from(&map)?;
assert_eq!(trie.get(b"foo"), Some(1));
assert_eq!(trie.get(b"bar"), Some(2));
assert_eq!(trie.get(b"bazzoo"), Some(3));
assert_eq!(trie.get(b"unknown"), None);
The trie can only store ASCII bytes; a string with non-ASCII always returns None:
use zerotrie::ZeroTrieSimpleAscii;
// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");
assert!(matches!(trie.get(b"ab\xFF"), None));Implementations§
Source§impl<const N: usize> ZeroTrieSimpleAscii<[u8; N]>
 
impl<const N: usize> ZeroTrieSimpleAscii<[u8; N]>
Sourcepub const fn from_sorted_u8_tuples(
    tuples: &[(&[u8], usize)],
) -> ZeroTrieSimpleAscii<[u8; N]>
 
pub const fn from_sorted_u8_tuples( tuples: &[(&[u8], usize)], ) -> ZeroTrieSimpleAscii<[u8; N]>
Const Constructor: Creates an ZeroTrieSimpleAscii from a sorted slice of keys and values.
This function needs to know the exact length of the resulting trie at compile time. To
figure out N, first set N to be too large (say 0xFFFF), then look at the resulting
compile error which will tell you how to set N, like this:
the evaluated program panicked at ‘Buffer too large. Size needed: 17’
That error message says you need to set N to 17.
Also see Self::from_sorted_str_tuples.
§Panics
Panics if items is not sorted or if N is not correct.
§Examples
Create a const ZeroTrieSimpleAscii at compile time:
use zerotrie::ZeroTrieSimpleAscii;
// The required capacity for this trie happens to be 17 bytes
const TRIE: ZeroTrieSimpleAscii<[u8; 17]> =
    ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
        (b"bar", 2),
        (b"bazzoo", 3),
        (b"foo", 1),
    ]);
assert_eq!(TRIE.get(b"foo"), Some(1));
assert_eq!(TRIE.get(b"bar"), Some(2));
assert_eq!(TRIE.get(b"bazzoo"), Some(3));
assert_eq!(TRIE.get(b"unknown"), None);Panics if strings are not sorted:
const TRIE: ZeroTrieSimpleAscii<[u8; 17]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"foo", 1),
    (b"bar", 2),
    (b"bazzoo", 3),
]);Panics if capacity is too small:
const TRIE: ZeroTrieSimpleAscii<[u8; 15]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"bar", 2),
    (b"bazzoo", 3),
    (b"foo", 1),
]);Panics if capacity is too large:
const TRIE: ZeroTrieSimpleAscii<[u8; 20]> = ZeroTrieSimpleAscii::from_sorted_u8_tuples(&[
    (b"bar", 2),
    (b"bazzoo", 3),
    (b"foo", 1),
]);Sourcepub const fn from_sorted_str_tuples(
    tuples: &[(&str, usize)],
) -> ZeroTrieSimpleAscii<[u8; N]>
 
pub const fn from_sorted_str_tuples( tuples: &[(&str, usize)], ) -> ZeroTrieSimpleAscii<[u8; N]>
Const Constructor: Creates an ZeroTrieSimpleAscii from a sorted slice of keys and values.
This function needs to know the exact length of the resulting trie at compile time. To
figure out N, first set N to be too large (say 0xFFFF), then look at the resulting
compile error which will tell you how to set N, like this:
the evaluated program panicked at ‘Buffer too large. Size needed: 17’
That error message says you need to set N to 17.
Also see Self::from_sorted_u8_tuples.
§Panics
Panics if items is not sorted, if N is not correct, or if any of the strings contain
non-ASCII characters.
§Examples
Create a const ZeroTrieSimpleAscii at compile time:
use zerotrie::ZeroTrieSimpleAscii;
// The required capacity for this trie happens to be 17 bytes
const TRIE: ZeroTrieSimpleAscii<[u8; 17]> =
    ZeroTrieSimpleAscii::from_sorted_str_tuples(&[
        ("bar", 2),
        ("bazzoo", 3),
        ("foo", 1),
    ]);
assert_eq!(TRIE.get(b"foo"), Some(1));
assert_eq!(TRIE.get(b"bar"), Some(2));
assert_eq!(TRIE.get(b"bazzoo"), Some(3));
assert_eq!(TRIE.get(b"unknown"), None);Panics if the strings are not ASCII:
const TRIE: ZeroTrieSimpleAscii<[u8; 100]> = ZeroTrieSimpleAscii::from_sorted_str_tuples(&[
    ("bár", 2),
    ("båzzöo", 3),
    ("foo", 1),
]);Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub fn cursor(&self) -> ZeroTrieSimpleAsciiCursor<'_>
 
pub fn cursor(&self) -> ZeroTrieSimpleAsciiCursor<'_>
Gets a cursor into the current trie.
Useful to query a trie with data that is not a slice.
This is currently supported only on ZeroTrieSimpleAscii
and ZeroAsciiIgnoreCaseTrie.
§Examples
Get a value out of a trie by writing it to the cursor:
use core::fmt::Write;
use zerotrie::ZeroTrieSimpleAscii;
// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");
// Get out the value for "abc"
let mut cursor = trie.cursor();
write!(&mut cursor, "abc");
assert_eq!(cursor.take_value(), Some(0));Find the longest prefix match:
use zerotrie::ZeroTrieSimpleAscii;
// A trie with two values: "abc" and "abcdef"
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");
// Find the longest prefix of the string "abcdxy":
let query = b"abcdxy";
let mut longest_prefix = 0;
let mut cursor = trie.cursor();
for (i, b) in query.iter().enumerate() {
    // Checking is_empty() is not required, but it is
    // good for efficiency
    if cursor.is_empty() {
        break;
    }
    if cursor.take_value().is_some() {
        longest_prefix = i;
    }
    cursor.step(*b);
}
// The longest prefix is "abc" which is length 3:
assert_eq!(longest_prefix, 3);Source§impl<'a> ZeroTrieSimpleAscii<&'a [u8]>
 
impl<'a> ZeroTrieSimpleAscii<&'a [u8]>
Sourcepub fn into_cursor(self) -> ZeroTrieSimpleAsciiCursor<'a>
 
pub fn into_cursor(self) -> ZeroTrieSimpleAsciiCursor<'a>
Same as ZeroTrieSimpleAscii::cursor() but moves self to avoid
having to doubly anchor the trie to the stack.
Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub const fn into_zerotrie(self) -> ZeroTrie<Store>
 
pub const fn into_zerotrie(self) -> ZeroTrie<Store>
Wrap this specific ZeroTrie variant into a ZeroTrie.
Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub const fn from_store(store: Store) -> ZeroTrieSimpleAscii<Store>
 
pub const fn from_store(store: Store) -> ZeroTrieSimpleAscii<Store>
Create a trie directly from a store.
If the store does not contain valid bytes, unexpected behavior may occur.
Sourcepub fn into_store(self) -> Store
 
pub fn into_store(self) -> Store
Takes the byte store from this trie.
Sourcepub fn convert_store<X>(self) -> ZeroTrieSimpleAscii<X>where
    X: From<Store>,
 
pub fn convert_store<X>(self) -> ZeroTrieSimpleAscii<X>where
    X: From<Store>,
Converts this trie’s store to a different store implementing the From trait.
For example, use this to change ZeroTrieSimpleAscii<Vec<u8>> to ZeroTrieSimpleAscii<Cow<[u8]>>.
§Examples
use std::borrow::Cow;
use zerotrie::ZeroTrieSimpleAscii;
let trie: ZeroTrieSimpleAscii<Vec<u8>> = ZeroTrieSimpleAscii::from_bytes(b"abc\x85").to_owned();
let cow: ZeroTrieSimpleAscii<Cow<[u8]>> = trie.convert_store();
assert_eq!(cow.get(b"abc"), Some(5));Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub fn byte_len(&self) -> usize
 
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::ZeroTrieSimpleAscii;
// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::from_bytes(b"abc\x80def\x81");
assert_eq!(8, trie.byte_len());
assert_eq!(2, trie.iter().count());Sourcepub fn as_borrowed(&self) -> &ZeroTrieSimpleAscii<[u8]>
 
pub fn as_borrowed(&self) -> &ZeroTrieSimpleAscii<[u8]>
Returns this trie as a reference transparent over a byte slice.
Sourcepub fn as_borrowed_slice(&self) -> ZeroTrieSimpleAscii<&[u8]>
 
pub fn as_borrowed_slice(&self) -> ZeroTrieSimpleAscii<&[u8]>
Returns a trie with a store borrowing from this trie.
Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub fn to_owned(&self) -> ZeroTrieSimpleAscii<Vec<u8>>
 
pub fn to_owned(&self) -> ZeroTrieSimpleAscii<Vec<u8>>
Converts a possibly-borrowed $name to an owned one.
✨ Enabled with the alloc Cargo feature.
§Examples
use zerotrie::ZeroTrieSimpleAscii;
let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::from_bytes(b"abc\x85");
let owned: ZeroTrieSimpleAscii<Vec<u8>> = trie.to_owned();
assert_eq!(trie.get(b"abc"), Some(5));
assert_eq!(owned.get(b"abc"), Some(5));Sourcepub fn iter(
    &self,
) -> Map<ZeroTrieIterator<'_>, fn(_: (Vec<u8>, usize)) -> (String, usize)>
 
pub fn iter( &self, ) -> Map<ZeroTrieIterator<'_>, fn(_: (Vec<u8>, usize)) -> (String, usize)>
Returns an iterator over the key/value pairs in this trie.
✨ Enabled with the alloc Cargo feature.
§Examples
use zerotrie::ZeroTrieSimpleAscii;
// A trie with two values: "abc" and "abcdef"
let trie: &ZeroTrieSimpleAscii<[u8]> = ZeroTrieSimpleAscii::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 ZeroTrieSimpleAscii<[u8]>
 
impl ZeroTrieSimpleAscii<[u8]>
Sourcepub fn from_bytes(trie: &[u8]) -> &ZeroTrieSimpleAscii<[u8]>
 
pub fn from_bytes(trie: &[u8]) -> &ZeroTrieSimpleAscii<[u8]>
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> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub fn to_btreemap(&self) -> BTreeMap<String, usize>
 
pub fn to_btreemap(&self) -> BTreeMap<String, usize>
Exports the data from this ZeroTrie type into a BTreeMap.
✨ Enabled with the alloc Cargo feature.
§Examples
use zerotrie::ZeroTrieSimpleAscii;
use std::collections::BTreeMap;
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x81def\x82");
let items = trie.to_btreemap();
assert_eq!(items.len(), 2);
let recovered_trie: ZeroTrieSimpleAscii<Vec<u8>> = items
    .into_iter()
    .collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());Source§impl<Store> ZeroTrieSimpleAscii<Store>
 
impl<Store> ZeroTrieSimpleAscii<Store>
Sourcepub fn to_litemap(&self) -> LiteMap<String, usize>
 
pub fn to_litemap(&self) -> LiteMap<String, usize>
Exports the data from this ZeroTrie type into a LiteMap.
✨ Enabled with the litemap Cargo feature.
§Examples
use zerotrie::ZeroTrieSimpleAscii;
use litemap::LiteMap;
let trie = ZeroTrieSimpleAscii::from_bytes(b"abc\x81def\x82");
let items = trie.to_litemap();
assert_eq!(items.len(), 2);
let recovered_trie: ZeroTrieSimpleAscii<Vec<u8>> = items
    .iter()
    .map(|(k, v)| (k, *v))
    .collect();
assert_eq!(trie.as_bytes(), recovered_trie.as_bytes());Trait Implementations§
Source§impl<Store> AsRef<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Store>
 
impl<Store> AsRef<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Store>
Source§fn as_ref(&self) -> &ZeroTrieSimpleAscii<[u8]>
 
fn as_ref(&self) -> &ZeroTrieSimpleAscii<[u8]>
Source§impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<&[u8]>
 
impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<&[u8]>
Source§impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Box<[u8]>>
 
impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Box<[u8]>>
Source§impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Vec<u8>>
 
impl Borrow<ZeroTrieSimpleAscii<[u8]>> for ZeroTrieSimpleAscii<Vec<u8>>
Source§impl<Store> Clone for ZeroTrieSimpleAscii<Store>
 
impl<Store> Clone for ZeroTrieSimpleAscii<Store>
Source§fn clone(&self) -> ZeroTrieSimpleAscii<Store>
 
fn clone(&self) -> ZeroTrieSimpleAscii<Store>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Store> Debug for ZeroTrieSimpleAscii<Store>
 
impl<Store> Debug for ZeroTrieSimpleAscii<Store>
Source§impl<Store> Default for ZeroTrieSimpleAscii<Store>
 
impl<Store> Default for ZeroTrieSimpleAscii<Store>
Source§fn default() -> ZeroTrieSimpleAscii<Store>
 
fn default() -> ZeroTrieSimpleAscii<Store>
Source§impl<'data, 'de, Store> Deserialize<'de> for ZeroTrieSimpleAscii<Store>
 
impl<'data, 'de, Store> Deserialize<'de> for ZeroTrieSimpleAscii<Store>
Source§fn deserialize<D>(
    deserializer: D,
) -> Result<ZeroTrieSimpleAscii<Store>, <D as Deserializer<'de>>::Error>where
    D: Deserializer<'de>,
 
fn deserialize<D>(
    deserializer: D,
) -> Result<ZeroTrieSimpleAscii<Store>, <D as Deserializer<'de>>::Error>where
    D: Deserializer<'de>,
Source§impl<'a, K> FromIterator<(K, usize)> for ZeroTrieSimpleAscii<Vec<u8>>
 
impl<'a, K> FromIterator<(K, usize)> for ZeroTrieSimpleAscii<Vec<u8>>
Source§fn from_iter<T>(iter: T) -> ZeroTrieSimpleAscii<Vec<u8>>where
    T: IntoIterator<Item = (K, usize)>,
 
fn from_iter<T>(iter: T) -> ZeroTrieSimpleAscii<Vec<u8>>where
    T: IntoIterator<Item = (K, usize)>,
Source§impl<Store> PartialEq for ZeroTrieSimpleAscii<Store>
 
impl<Store> PartialEq for ZeroTrieSimpleAscii<Store>
Source§impl<Store> Serialize for ZeroTrieSimpleAscii<Store>
 
impl<Store> Serialize for ZeroTrieSimpleAscii<Store>
Source§fn serialize<S>(
    &self,
    serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
    S: Serializer,
 
fn serialize<S>(
    &self,
    serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
    S: Serializer,
Source§impl ToOwned for ZeroTrieSimpleAscii<[u8]>
 
impl ToOwned for ZeroTrieSimpleAscii<[u8]>
Source§fn to_owned(&self) -> <ZeroTrieSimpleAscii<[u8]> as ToOwned>::Owned
 
fn to_owned(&self) -> <ZeroTrieSimpleAscii<[u8]> as ToOwned>::Owned
This impl allows ZeroTrieSimpleAscii to be used inside of a Cow.
Note that it is also possible to use ZeroTrieSimpleAscii<ZeroVec<u8>> for a similar result.
✨ Enabled with the alloc Cargo feature.
§Examples
use std::borrow::Cow;
use zerotrie::ZeroTrieSimpleAscii;
let trie: Cow<ZeroTrieSimpleAscii<[u8]>> = Cow::Borrowed(ZeroTrieSimpleAscii::from_bytes(b"abc\x85"));
assert_eq!(trie.get(b"abc"), Some(5));1.63.0 · Source§fn clone_into(&self, target: &mut Self::Owned)
 
fn clone_into(&self, target: &mut Self::Owned)
Source§impl<Store> VarULE for ZeroTrieSimpleAscii<Store>where
    Store: VarULE,
 
impl<Store> VarULE for ZeroTrieSimpleAscii<Store>where
    Store: VarULE,
Source§fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>
 
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>
&[u8]. Read moreSource§unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &ZeroTrieSimpleAscii<Store>
 
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &ZeroTrieSimpleAscii<Store>
&[u8], and return it as &Self with the same lifetime, assuming
that this byte slice has previously been run through Self::parse_bytes() with
success. Read moreSource§impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTrieSimpleAscii<Store1>> for ZeroTrieSimpleAscii<Store2>where
    Store2: ZeroFrom<'zf, Store1>,
 
impl<'zf, Store1, Store2> ZeroFrom<'zf, ZeroTrieSimpleAscii<Store1>> for ZeroTrieSimpleAscii<Store2>where
    Store2: ZeroFrom<'zf, Store1>,
Source§fn zero_from(
    other: &'zf ZeroTrieSimpleAscii<Store1>,
) -> ZeroTrieSimpleAscii<Store2>
 
fn zero_from( other: &'zf ZeroTrieSimpleAscii<Store1>, ) -> ZeroTrieSimpleAscii<Store2>
C into a struct that may retain references into C.impl<Store> Copy for ZeroTrieSimpleAscii<Store>
impl<Store> Eq for ZeroTrieSimpleAscii<Store>
impl<Store> StructuralPartialEq for ZeroTrieSimpleAscii<Store>where
    Store: ?Sized,
Auto Trait Implementations§
impl<Store> Freeze for ZeroTrieSimpleAscii<Store>
impl<Store> RefUnwindSafe for ZeroTrieSimpleAscii<Store>where
    Store: RefUnwindSafe + ?Sized,
impl<Store> Send for ZeroTrieSimpleAscii<Store>
impl<Store> Sync for ZeroTrieSimpleAscii<Store>
impl<Store> Unpin for ZeroTrieSimpleAscii<Store>
impl<Store> UnwindSafe for ZeroTrieSimpleAscii<Store>where
    Store: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
Source§impl<T> EncodeAsVarULE<T> for T
 
impl<T> EncodeAsVarULE<T> for T
Source§fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
 
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
cb with a piecewise list of byte slices that when concatenated
produce the memory pattern of the corresponding instance of T. Read moreSource§fn encode_var_ule_len(&self) -> usize
 
fn encode_var_ule_len(&self) -> usize
VarULE typeSource§fn encode_var_ule_write(&self, dst: &mut [u8])
 
fn encode_var_ule_write(&self, dst: &mut [u8])
VarULE type to the dst buffer. dst should
be the size of Self::encode_var_ule_len()Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more