Struct zerotrie::ZeroTrieSimpleAscii
source · #[repr(transparent)]pub struct ZeroTrieSimpleAscii<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);
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)]) -> Self
pub const fn from_sorted_u8_tuples(tuples: &[(&[u8], usize)]) -> Self
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.
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)]) -> Self
pub const fn from_sorted_str_tuples(tuples: &[(&str, usize)]) -> Self
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.
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 const fn into_zerotrie(self) -> ZeroTrie<Store>
pub const fn into_zerotrie(self) -> ZeroTrie<Store>
Wrap this specific ZeroTrie variant into a ZeroTrie.
sourcepub const fn from_store(store: Store) -> Self
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.
sourcepub fn take_store(self) -> Store
pub fn take_store(self) -> Store
Takes the byte store from this trie.
sourcepub fn convert_store<X: From<Store>>(self) -> ZeroTrieSimpleAscii<X>
pub fn convert_store<X: From<Store>>(self) -> ZeroTrieSimpleAscii<X>
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>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> ZeroTrieSimpleAscii<Store>where Store: AsRef<[u8]> + ?Sized,
sourcepub fn get<K>(&self, key: K) -> Option<usize>where
K: AsRef<[u8]>,
pub fn get<K>(&self, key: K) -> Option<usize>where K: AsRef<[u8]>,
Queries the trie for a string.
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.
source§impl<Store> ZeroTrieSimpleAscii<Store>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> ZeroTrieSimpleAscii<Store>where Store: AsRef<[u8]> + ?Sized,
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) -> impl Iterator<Item = (String, usize)> + '_
pub fn iter(&self) -> impl Iterator<Item = (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]) -> &Self
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> ZeroTrieSimpleAscii<Store>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> ZeroTrieSimpleAscii<Store>where Store: AsRef<[u8]> + ?Sized,
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>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> ZeroTrieSimpleAscii<Store>where Store: AsRef<[u8]> + ?Sized,
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> Bake for ZeroTrieSimpleAscii<Store>where
Store: Bake + ?Sized,
impl<Store> Bake for ZeroTrieSimpleAscii<Store>where Store: Bake + ?Sized,
source§fn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
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 + ?Sized> Clone for ZeroTrieSimpleAscii<Store>
impl<Store: Clone + ?Sized> 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: Default + ?Sized> Default for ZeroTrieSimpleAscii<Store>
impl<Store: Default + ?Sized> Default for ZeroTrieSimpleAscii<Store>
source§fn default() -> ZeroTrieSimpleAscii<Store>
fn default() -> ZeroTrieSimpleAscii<Store>
source§impl<'de, 'data, Store> Deserialize<'de> for ZeroTrieSimpleAscii<Store>where
Store: From<&'data [u8]> + From<Vec<u8>> + 'data,
'de: 'data,
impl<'de, 'data, Store> Deserialize<'de> for ZeroTrieSimpleAscii<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>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<Store> From<&ZeroTrieSimpleAscii<Store>> for BTreeMap<String, usize>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> From<&ZeroTrieSimpleAscii<Store>> for BTreeMap<String, usize>where Store: AsRef<[u8]> + ?Sized,
source§fn from(other: &ZeroTrieSimpleAscii<Store>) -> Self
fn from(other: &ZeroTrieSimpleAscii<Store>) -> Self
source§impl<Store> From<&ZeroTrieSimpleAscii<Store>> for LiteMap<String, usize>where
Store: AsRef<[u8]> + ?Sized,
impl<Store> From<&ZeroTrieSimpleAscii<Store>> for LiteMap<String, usize>where Store: AsRef<[u8]> + ?Sized,
source§fn from(other: &ZeroTrieSimpleAscii<Store>) -> Self
fn from(other: &ZeroTrieSimpleAscii<Store>) -> Self
source§impl<'a, K> FromIterator<(K, usize)> for ZeroTrieSimpleAscii<Vec<u8>>where
K: AsRef<[u8]>,
impl<'a, K> FromIterator<(K, usize)> for ZeroTrieSimpleAscii<Vec<u8>>where K: AsRef<[u8]>,
source§impl<Store: PartialEq + ?Sized> PartialEq for ZeroTrieSimpleAscii<Store>
impl<Store: PartialEq + ?Sized> PartialEq for ZeroTrieSimpleAscii<Store>
source§fn eq(&self, other: &ZeroTrieSimpleAscii<Store>) -> bool
fn eq(&self, other: &ZeroTrieSimpleAscii<Store>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl ToOwned for ZeroTrieSimpleAscii<[u8]>
impl ToOwned for ZeroTrieSimpleAscii<[u8]>
source§fn to_owned(&self) -> Self::Owned
fn to_owned(&self) -> Self::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));
§type Owned = ZeroTrieSimpleAscii<Box<[u8]>>
type Owned = ZeroTrieSimpleAscii<Box<[u8]>>
1.63.0 · source§fn clone_into(&self, target: &mut Self::Owned)
fn clone_into(&self, target: &mut Self::Owned)
source§impl<'a, K> TryFrom<&'a BTreeMap<K, usize>> for ZeroTrieSimpleAscii<Vec<u8>>where
K: Borrow<[u8]>,
impl<'a, K> TryFrom<&'a BTreeMap<K, usize>> for ZeroTrieSimpleAscii<Vec<u8>>where K: Borrow<[u8]>,
source§impl<'a, K, S> TryFrom<&'a LiteMap<K, usize, S>> for ZeroTrieSimpleAscii<Vec<u8>>where
K: Borrow<[u8]>,
S: StoreIterable<'a, K, usize>,
impl<'a, K, S> TryFrom<&'a LiteMap<K, usize, S>> for ZeroTrieSimpleAscii<Vec<u8>>where K: Borrow<[u8]>, S: StoreIterable<'a, K, usize>,
source§impl<Store> VarULE for ZeroTrieSimpleAscii<Store>where
Store: VarULE,
impl<Store> VarULE for ZeroTrieSimpleAscii<Store>where Store: VarULE,
source§fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError>
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError>
&[u8]
. Read moresource§unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self
&[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 moresource§fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError>
fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError>
source§fn as_byte_slice(&self) -> &[u8] ⓘ
fn as_byte_slice(&self) -> &[u8] ⓘ
source§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>) -> Self
fn zero_from(other: &'zf ZeroTrieSimpleAscii<Store1>) -> Self
C
into a struct that may retain references into C
.impl<Store: Copy + ?Sized> Copy for ZeroTrieSimpleAscii<Store>
impl<Store: Eq + ?Sized> Eq for ZeroTrieSimpleAscii<Store>
impl<Store: ?Sized> StructuralEq for ZeroTrieSimpleAscii<Store>
impl<Store: ?Sized> StructuralPartialEq for ZeroTrieSimpleAscii<Store>
Auto Trait Implementations§
impl<Store: ?Sized> RefUnwindSafe for ZeroTrieSimpleAscii<Store>where Store: RefUnwindSafe,
impl<Store: ?Sized> Send for ZeroTrieSimpleAscii<Store>where Store: Send,
impl<Store: ?Sized> Sync for ZeroTrieSimpleAscii<Store>where Store: Sync,
impl<Store: ?Sized> Unpin for ZeroTrieSimpleAscii<Store>where Store: Unpin,
impl<Store: ?Sized> UnwindSafe for ZeroTrieSimpleAscii<Store>where Store: UnwindSafe,
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> EncodeAsVarULE<T> for Twhere
T: VarULE + ?Sized,
impl<T> EncodeAsVarULE<T> for Twhere T: VarULE + ?Sized,
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()