use core::{
fmt::{
Debug,
Display,
},
hash::Hash,
};
use num_traits::{
FromPrimitive,
PrimInt,
ToPrimitive,
Unsigned,
};
pub trait TokenType:
'static
+ PrimInt
+ FromPrimitive
+ ToPrimitive
+ Unsigned
+ Hash
+ Default
+ Debug
+ Display
+ Send
+ Sync
{
}
impl<T> TokenType for T where
T: 'static
+ PrimInt
+ FromPrimitive
+ ToPrimitive
+ Unsigned
+ Hash
+ Default
+ Debug
+ Display
+ Send
+ Sync
{
}
pub type Pair<T> = (T, T);
cfg_if::cfg_if! {
if #[cfg(all(feature = "fast-hash", feature = "std"))] {
pub type WCHashMap<K, V> = std::collections::HashMap<K, V, foldhash::fast::FixedState>;
pub fn hash_map_new<K, V>() -> WCHashMap<K, V> {
WCHashMap::with_hasher(foldhash::fast::FixedState::default())
}
pub fn hash_map_with_capacity<K, V>(capacity: usize) -> WCHashMap<K, V> {
WCHashMap::with_capacity_and_hasher(capacity, foldhash::fast::FixedState::default())
}
pub type WCHashIter<'a, K, V> = std::collections::hash_map::Iter<'a, K, V>;
pub type WCHashSet<V> = std::collections::HashSet<V, foldhash::fast::FixedState>;
} else if #[cfg(feature = "fast-hash")] {
pub type WCHashMap<K, V> = hashbrown::HashMap<K, V, foldhash::fast::FixedState>;
pub fn hash_map_new<K, V>() -> WCHashMap<K, V> {
WCHashMap::with_hasher(foldhash::fast::FixedState::default())
}
pub fn hash_map_with_capacity<K, V>(capacity: usize) -> WCHashMap<K, V> {
WCHashMap::with_capacity_and_hasher(capacity, foldhash::fast::FixedState::default())
}
pub type WCHashIter<'a, K, V> = hashbrown::hash_map::Iter<'a, K, V>;
pub type WCHashSet<V> = hashbrown::HashSet<V, foldhash::fast::FixedState>;
} else if #[cfg(feature = "std")] {
pub type WCHashMap<K, V> = std::collections::HashMap<K, V>;
pub fn hash_map_new<K, V>() -> WCHashMap<K, V> {
WCHashMap::new()
}
pub fn hash_map_with_capacity<K, V>(capacity: usize) -> WCHashMap<K, V> {
WCHashMap::with_capacity(capacity)
}
pub type WCHashIter<'a, K, V> = std::collections::hash_map::Iter<'a, K, V>;
pub type WCHashSet<V> = std::collections::HashSet<V>;
} else {
pub type WCHashMap<K, V> = hashbrown::HashMap<K, V>;
pub fn hash_map_new<K, V>() -> WCHashMap<K, V> {
WCHashMap::new()
}
pub fn hash_map_with_capacity<K, V>(capacity: usize) -> WCHashMap<K, V> {
WCHashMap::with_capacity(capacity)
}
pub type WCHashIter<'a, K, V> = hashbrown::hash_map::Iter<'a, K, V>;
pub type WCHashSet<V> = hashbrown::HashSet<V>;
}
}
#[cfg(test)]
mod tests {
use core::marker::PhantomData;
use super::*;
#[test]
fn test_common_token_types() {
struct IsToken<T: TokenType>(PhantomData<T>);
let _: IsToken<u16>;
let _: IsToken<u32>;
let _: IsToken<u64>;
let _: IsToken<usize>;
}
}