use crate::reader;
use core::borrow::Borrow;
#[cfg(feature = "alloc")]
use crate::{
builder::nonconst::ZeroTrieBuilder, builder::slice_indices::ByteSliceWithIndices,
error::ZeroTrieBuildError,
};
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, collections::BTreeMap, collections::VecDeque, string::String, vec::Vec};
#[cfg(feature = "litemap")]
use litemap::LiteMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct ZeroTrie<Store>(pub(crate) ZeroTrieFlavor<Store>);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ZeroTrieFlavor<Store> {
SimpleAscii(ZeroTrieSimpleAscii<Store>),
PerfectHash(ZeroTriePerfectHash<Store>),
ExtendedCapacity(ZeroTrieExtendedCapacity<Store>),
}
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = zerotrie))]
#[allow(clippy::exhaustive_structs)] pub struct ZeroTrieSimpleAscii<Store: ?Sized> {
#[doc(hidden)] pub store: Store,
}
impl<Store: ?Sized> ZeroTrieSimpleAscii<Store> {
#[allow(unsafe_code)] fn transparent_ref_from_store(s: &Store) -> &Self {
unsafe {
&*(s as *const Store as *const Self)
}
}
}
impl<Store> ZeroTrieSimpleAscii<Store> {
#[inline]
pub const fn into_zerotrie(self) -> ZeroTrie<Store> {
ZeroTrie(ZeroTrieFlavor::SimpleAscii(self))
}
}
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = zerotrie))]
#[allow(clippy::exhaustive_structs)] pub struct ZeroAsciiIgnoreCaseTrie<Store: ?Sized> {
#[doc(hidden)] pub store: Store,
}
impl<Store: ?Sized> ZeroAsciiIgnoreCaseTrie<Store> {
#[allow(unsafe_code)] fn transparent_ref_from_store(s: &Store) -> &Self {
unsafe {
&*(s as *const Store as *const Self)
}
}
}
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = zerotrie))]
#[allow(clippy::exhaustive_structs)] pub struct ZeroTriePerfectHash<Store: ?Sized> {
#[doc(hidden)] pub store: Store,
}
impl<Store: ?Sized> ZeroTriePerfectHash<Store> {
#[allow(unsafe_code)] fn transparent_ref_from_store(s: &Store) -> &Self {
unsafe {
&*(s as *const Store as *const Self)
}
}
}
impl<Store> ZeroTriePerfectHash<Store> {
#[inline]
pub const fn into_zerotrie(self) -> ZeroTrie<Store> {
ZeroTrie(ZeroTrieFlavor::PerfectHash(self))
}
}
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = zerotrie))]
#[allow(clippy::exhaustive_structs)] pub struct ZeroTrieExtendedCapacity<Store: ?Sized> {
#[doc(hidden)] pub store: Store,
}
impl<Store: ?Sized> ZeroTrieExtendedCapacity<Store> {
#[allow(unsafe_code)] fn transparent_ref_from_store(s: &Store) -> &Self {
unsafe {
&*(s as *const Store as *const Self)
}
}
}
impl<Store> ZeroTrieExtendedCapacity<Store> {
#[inline]
pub const fn into_zerotrie(self) -> ZeroTrie<Store> {
ZeroTrie(ZeroTrieFlavor::ExtendedCapacity(self))
}
}
macro_rules! impl_zerotrie_subtype {
($name:ident, $iter_element:ty, $iter_fn:path, $iter_ty:ty, $cnv_fn:path) => {
impl<Store> $name<Store> {
#[inline]
pub const fn from_store(store: Store) -> Self {
Self { store }
}
#[inline]
pub fn into_store(self) -> Store {
self.store
}
#[doc = concat!("For example, use this to change `", stringify!($name), "<Vec<u8>>` to `", stringify!($name), "<Cow<[u8]>>`.")]
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie: ", stringify!($name), "<Vec<u8>> = ", stringify!($name), "::from_bytes(b\"abc\\x85\").to_owned();")]
#[doc = concat!("let cow: ", stringify!($name), "<Cow<[u8]>> = trie.convert_store();")]
pub fn convert_store<X: From<Store>>(self) -> $name<X> {
$name::<X>::from_store(X::from(self.store))
}
}
impl<Store> $name<Store>
where
Store: AsRef<[u8]> + ?Sized,
{
pub fn get<K>(&self, key: K) -> Option<usize> where K: AsRef<[u8]> {
reader::get_parameterized::<Self>(self.store.as_ref(), key.as_ref())
}
#[inline]
pub fn is_empty(&self) -> bool {
self.store.as_ref().is_empty()
}
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie: &", stringify!($name), "<[u8]> = ", stringify!($name), "::from_bytes(b\"abc\\x80def\\x81\");")]
#[inline]
pub fn byte_len(&self) -> usize {
self.store.as_ref().len()
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.store.as_ref()
}
#[inline]
pub fn as_borrowed(&self) -> &$name<[u8]> {
$name::from_bytes(self.store.as_ref())
}
#[inline]
pub fn as_borrowed_slice(&self) -> $name<&[u8]> {
$name::from_store(self.store.as_ref())
}
}
impl<Store> AsRef<$name<[u8]>> for $name<Store>
where
Store: AsRef<[u8]> + ?Sized,
{
#[inline]
fn as_ref(&self) -> &$name<[u8]> {
self.as_borrowed()
}
}
#[cfg(feature = "alloc")]
impl<Store> $name<Store>
where
Store: AsRef<[u8]> + ?Sized,
{
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie: &", stringify!($name), "<[u8]> = ", stringify!($name), "::from_bytes(b\"abc\\x85\");")]
#[doc = concat!("let owned: ", stringify!($name), "<Vec<u8>> = trie.to_owned();")]
#[inline]
pub fn to_owned(&self) -> $name<Vec<u8>> {
$name::from_store(
Vec::from(self.store.as_ref()),
)
}
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie: &", stringify!($name), "<[u8]> = ", stringify!($name), "::from_bytes(b\"abc\\x80def\\x81\");")]
#[inline]
pub fn iter(&self) -> $iter_ty {
$iter_fn(self.as_bytes())
}
}
impl $name<[u8]> {
#[inline]
pub fn from_bytes(trie: &[u8]) -> &Self {
Self::transparent_ref_from_store(trie)
}
}
#[cfg(feature = "alloc")]
impl $name<Vec<u8>> {
pub(crate) fn try_from_tuple_slice(items: ByteSliceWithIndices) -> Result<Self, ZeroTrieBuildError> {
use crate::options::ZeroTrieWithOptions;
ZeroTrieBuilder::<VecDeque<u8>>::from_sorted_tuple_slice(
items,
Self::OPTIONS,
)
.map(|s| Self {
store: s.to_bytes(),
})
}
pub fn try_from_btree_map_str<K>(items: &BTreeMap<K, usize>) -> Result<Self, ZeroTrieBuildError>
where
K: Borrow<str>
{
let tuples: Vec<(&[u8], usize)> = items
.iter()
.map(|(k, v)| (k.borrow().as_bytes(), *v))
.collect();
let byte_str_slice = ByteSliceWithIndices::from_byte_slice(&tuples);
Self::try_from_tuple_slice(byte_str_slice)
}
}
#[cfg(feature = "alloc")]
impl<K> FromIterator<(K, usize)> for $name<Vec<u8>>
where
K: AsRef<[u8]>
{
fn from_iter<T: IntoIterator<Item = (K, usize)>>(iter: T) -> Self {
use crate::options::ZeroTrieWithOptions;
use crate::builder::nonconst::ZeroTrieBuilder;
ZeroTrieBuilder::<VecDeque<u8>>::from_bytes_iter(
iter,
Self::OPTIONS
)
.map(|s| Self {
store: s.to_bytes(),
})
.unwrap()
}
}
#[cfg(feature = "alloc")]
impl<'a, K> TryFrom<&'a BTreeMap<K, usize>> for $name<Vec<u8>>
where
K: Borrow<[u8]>
{
type Error = crate::error::ZeroTrieBuildError;
fn try_from(map: &'a BTreeMap<K, usize>) -> Result<Self, Self::Error> {
let tuples: Vec<(&[u8], usize)> = map
.iter()
.map(|(k, v)| (k.borrow(), *v))
.collect();
let byte_str_slice = ByteSliceWithIndices::from_byte_slice(&tuples);
Self::try_from_tuple_slice(byte_str_slice)
}
}
#[cfg(feature = "alloc")]
impl<Store> $name<Store>
where
Store: AsRef<[u8]> + ?Sized
{
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie = ", stringify!($name), "::from_bytes(b\"abc\\x81def\\x82\");")]
#[doc = concat!("let recovered_trie: ", stringify!($name), "<Vec<u8>> = items")]
pub fn to_btreemap(&self) -> BTreeMap<$iter_element, usize> {
self.iter().collect()
}
#[allow(dead_code)] pub(crate) fn to_btreemap_bytes(&self) -> BTreeMap<Box<[u8]>, usize> {
self.iter().map(|(k, v)| ($cnv_fn(k), v)).collect()
}
}
#[cfg(feature = "alloc")]
impl<Store> From<&$name<Store>> for BTreeMap<$iter_element, usize>
where
Store: AsRef<[u8]> + ?Sized,
{
#[inline]
fn from(other: &$name<Store>) -> Self {
other.to_btreemap()
}
}
#[cfg(feature = "litemap")]
impl<'a, K, S> TryFrom<&'a LiteMap<K, usize, S>> for $name<Vec<u8>>
where
K: Borrow<[u8]>,
S: litemap::store::StoreIterable<'a, K, usize>,
{
type Error = crate::error::ZeroTrieBuildError;
fn try_from(map: &'a LiteMap<K, usize, S>) -> Result<Self, Self::Error> {
let tuples: Vec<(&[u8], usize)> = map
.iter()
.map(|(k, v)| (k.borrow(), *v))
.collect();
let byte_str_slice = ByteSliceWithIndices::from_byte_slice(&tuples);
Self::try_from_tuple_slice(byte_str_slice)
}
}
#[cfg(feature = "litemap")]
impl<Store> $name<Store>
where
Store: AsRef<[u8]> + ?Sized,
{
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie = ", stringify!($name), "::from_bytes(b\"abc\\x81def\\x82\");")]
#[doc = concat!("let recovered_trie: ", stringify!($name), "<Vec<u8>> = items")]
pub fn to_litemap(&self) -> LiteMap<$iter_element, usize> {
self.iter().collect()
}
#[allow(dead_code)] pub(crate) fn to_litemap_bytes(&self) -> LiteMap<Box<[u8]>, usize> {
self.iter().map(|(k, v)| ($cnv_fn(k), v)).collect()
}
#[cfg(feature = "serde")]
pub(crate) fn to_litemap_serde(&self) -> LiteMap<crate::serde::SerdeByteStrOwned, usize> {
self.iter().map(|(k, v)| (crate::serde::SerdeByteStrOwned($cnv_fn(k)), v)).collect()
}
}
#[cfg(feature = "litemap")]
impl<Store> From<&$name<Store>> for LiteMap<$iter_element, usize>
where
Store: AsRef<[u8]> + ?Sized,
{
#[inline]
fn from(other: &$name<Store>) -> Self {
other.to_litemap()
}
}
#[cfg(feature = "litemap")]
impl $name<Vec<u8>>
{
#[cfg(feature = "serde")]
pub(crate) fn try_from_serde_litemap(items: &LiteMap<crate::serde::SerdeByteStrOwned, usize>) -> Result<Self, ZeroTrieBuildError> {
let tuples: Vec<(&[u8], usize)> = items.iter().map(|(k, v)| (k.as_bytes(), *v)).collect();
let byte_str_slice = ByteSliceWithIndices::from_byte_slice(&tuples);
Self::try_from_tuple_slice(byte_str_slice)
}
}
impl Borrow<$name<[u8]>> for $name<&[u8]> {
#[inline]
fn borrow(&self) -> &$name<[u8]> {
self.as_borrowed()
}
}
#[cfg(feature = "alloc")]
impl Borrow<$name<[u8]>> for $name<Box<[u8]>> {
#[inline]
fn borrow(&self) -> &$name<[u8]> {
self.as_borrowed()
}
}
#[cfg(feature = "alloc")]
impl Borrow<$name<[u8]>> for $name<Vec<u8>> {
#[inline]
fn borrow(&self) -> &$name<[u8]> {
self.as_borrowed()
}
}
#[cfg(feature = "alloc")]
impl alloc::borrow::ToOwned for $name<[u8]> {
type Owned = $name<Box<[u8]>>;
#[doc = concat!("This impl allows [`", stringify!($name), "`] to be used inside of a [`Cow`](alloc::borrow::Cow).")]
#[doc = concat!("Note that it is also possible to use `", stringify!($name), "<ZeroVec<u8>>` for a similar result.")]
#[doc = concat!("use zerotrie::", stringify!($name), ";")]
#[doc = concat!("let trie: Cow<", stringify!($name), "<[u8]>> = Cow::Borrowed(", stringify!($name), "::from_bytes(b\"abc\\x85\"));")]
fn to_owned(&self) -> Self::Owned {
let bytes: &[u8] = self.store.as_ref();
$name::from_store(
Vec::from(bytes).into_boxed_slice(),
)
}
}
#[cfg(feature = "zerovec")]
#[allow(unsafe_code)] unsafe impl<Store> zerovec::ule::VarULE for $name<Store>
where
Store: zerovec::ule::VarULE,
{
#[inline]
fn validate_bytes(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
Store::validate_bytes(bytes)
}
#[inline]
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
Self::transparent_ref_from_store(Store::from_bytes_unchecked(bytes))
}
}
#[cfg(feature = "zerofrom")]
impl<'zf, Store1, Store2> zerofrom::ZeroFrom<'zf, $name<Store1>> for $name<Store2>
where
Store2: zerofrom::ZeroFrom<'zf, Store1>,
{
#[inline]
fn zero_from(other: &'zf $name<Store1>) -> Self {
$name::from_store(zerofrom::ZeroFrom::zero_from(&other.store))
}
}
};
}
#[cfg(feature = "alloc")]
fn string_to_box_u8(input: String) -> Box<[u8]> {
input.into_boxed_str().into_boxed_bytes()
}
#[doc(hidden)] #[cfg(feature = "alloc")]
pub type ZeroTrieStringIterator<'a> =
core::iter::Map<reader::ZeroTrieIterator<'a>, fn((Vec<u8>, usize)) -> (String, usize)>;
impl_zerotrie_subtype!(
ZeroTrieSimpleAscii,
String,
reader::get_iter_ascii_or_panic,
ZeroTrieStringIterator<'_>,
string_to_box_u8
);
impl_zerotrie_subtype!(
ZeroAsciiIgnoreCaseTrie,
String,
reader::get_iter_ascii_or_panic,
ZeroTrieStringIterator<'_>,
string_to_box_u8
);
impl_zerotrie_subtype!(
ZeroTriePerfectHash,
Vec<u8>,
reader::get_iter_phf,
reader::ZeroTrieIterator<'_>,
Vec::into_boxed_slice
);
impl_zerotrie_subtype!(
ZeroTrieExtendedCapacity,
Vec<u8>,
reader::get_iter_phf,
reader::ZeroTrieIterator<'_>,
Vec::into_boxed_slice
);
#[allow(unused_macro_rules)] macro_rules! impl_dispatch {
($self:ident, $inner_fn:ident()) => {
match $self.0 {
ZeroTrieFlavor::SimpleAscii(subtype) => subtype.$inner_fn(),
ZeroTrieFlavor::PerfectHash(subtype) => subtype.$inner_fn(),
ZeroTrieFlavor::ExtendedCapacity(subtype) => subtype.$inner_fn(),
}
};
($self:ident, $inner_fn:ident().into_zerotrie()) => {
match $self.0 {
ZeroTrieFlavor::SimpleAscii(subtype) => subtype.$inner_fn().into_zerotrie(),
ZeroTrieFlavor::PerfectHash(subtype) => subtype.$inner_fn().into_zerotrie(),
ZeroTrieFlavor::ExtendedCapacity(subtype) => subtype.$inner_fn().into_zerotrie(),
}
};
(&$self:ident, $inner_fn:ident()) => {
match &$self.0 {
ZeroTrieFlavor::SimpleAscii(subtype) => subtype.$inner_fn(),
ZeroTrieFlavor::PerfectHash(subtype) => subtype.$inner_fn(),
ZeroTrieFlavor::ExtendedCapacity(subtype) => subtype.$inner_fn(),
}
};
(&$self:ident, $inner_fn:ident($arg:ident)) => {
match &$self.0 {
ZeroTrieFlavor::SimpleAscii(subtype) => subtype.$inner_fn($arg),
ZeroTrieFlavor::PerfectHash(subtype) => subtype.$inner_fn($arg),
ZeroTrieFlavor::ExtendedCapacity(subtype) => subtype.$inner_fn($arg),
}
};
(&$self:ident, $trait:ident::$inner_fn:ident()) => {
match &$self.0 {
ZeroTrieFlavor::SimpleAscii(subtype) => {
ZeroTrie(ZeroTrieFlavor::SimpleAscii($trait::$inner_fn(subtype)))
}
ZeroTrieFlavor::PerfectHash(subtype) => {
ZeroTrie(ZeroTrieFlavor::PerfectHash($trait::$inner_fn(subtype)))
}
ZeroTrieFlavor::ExtendedCapacity(subtype) => {
ZeroTrie(ZeroTrieFlavor::ExtendedCapacity($trait::$inner_fn(subtype)))
}
}
};
}
impl<Store> ZeroTrie<Store> {
pub fn into_store(self) -> Store {
impl_dispatch!(self, into_store())
}
pub fn convert_store<NewStore>(self) -> ZeroTrie<NewStore>
where
NewStore: From<Store>,
{
impl_dispatch!(self, convert_store().into_zerotrie())
}
}
impl<Store> ZeroTrie<Store>
where
Store: AsRef<[u8]>,
{
pub fn get<K>(&self, key: K) -> Option<usize>
where
K: AsRef<[u8]>,
{
impl_dispatch!(&self, get(key))
}
pub fn is_empty(&self) -> bool {
impl_dispatch!(&self, is_empty())
}
pub fn byte_len(&self) -> usize {
impl_dispatch!(&self, byte_len())
}
}
#[cfg(feature = "alloc")]
impl<Store> ZeroTrie<Store>
where
Store: AsRef<[u8]>,
{
pub fn to_btreemap(&self) -> BTreeMap<Box<[u8]>, usize> {
impl_dispatch!(&self, to_btreemap_bytes())
}
}
#[cfg(feature = "litemap")]
impl<Store> ZeroTrie<Store>
where
Store: AsRef<[u8]>,
{
#[cfg(feature = "serde")]
pub fn to_litemap(&self) -> LiteMap<Box<[u8]>, usize> {
impl_dispatch!(&self, to_litemap_bytes())
}
#[cfg(feature = "serde")]
pub(crate) fn to_litemap_serde(&self) -> LiteMap<crate::serde::SerdeByteStrOwned, usize> {
impl_dispatch!(&self, to_litemap_serde())
}
}
#[cfg(feature = "alloc")]
impl ZeroTrie<Vec<u8>> {
pub(crate) fn try_from_tuple_slice(
items: ByteSliceWithIndices,
) -> Result<Self, ZeroTrieBuildError> {
let is_all_ascii = items.is_all_ascii();
if is_all_ascii && items.len() < 512 {
ZeroTrieSimpleAscii::try_from_tuple_slice(items).map(|x| x.into_zerotrie())
} else {
ZeroTriePerfectHash::try_from_tuple_slice(items).map(|x| x.into_zerotrie())
}
}
}
#[cfg(feature = "alloc")]
impl<K> FromIterator<(K, usize)> for ZeroTrie<Vec<u8>>
where
K: AsRef<[u8]>,
{
fn from_iter<T: IntoIterator<Item = (K, usize)>>(iter: T) -> Self {
let items = Vec::from_iter(iter);
let mut items: Vec<(&[u8], usize)> = items.iter().map(|(k, v)| (k.as_ref(), *v)).collect();
items.sort();
let byte_str_slice = ByteSliceWithIndices::from_byte_slice(&items);
#[expect(clippy::unwrap_used)] Self::try_from_tuple_slice(byte_str_slice).unwrap()
}
}
#[cfg(feature = "databake")]
impl<Store> databake::Bake for ZeroTrie<Store>
where
Store: databake::Bake,
{
fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
use databake::*;
let inner = impl_dispatch!(&self, bake(env));
quote! { #inner.into_zerotrie() }
}
}
#[cfg(feature = "databake")]
impl<Store> databake::BakeSize for ZeroTrie<Store>
where
Store: databake::BakeSize,
{
fn borrows_size(&self) -> usize {
impl_dispatch!(&self, borrows_size())
}
}
#[cfg(feature = "zerofrom")]
impl<'zf, Store1, Store2> zerofrom::ZeroFrom<'zf, ZeroTrie<Store1>> for ZeroTrie<Store2>
where
Store2: zerofrom::ZeroFrom<'zf, Store1>,
{
fn zero_from(other: &'zf ZeroTrie<Store1>) -> Self {
use zerofrom::ZeroFrom;
impl_dispatch!(&other, ZeroFrom::zero_from())
}
}