use std::borrow::Borrow; use std::collections::{hash_map, HashMap}; use std::fmt::{self, Debug}; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::{Deref, DerefMut, Index}; use std::panic::UnwindSafe; /// A [`HashMap`](std::collections::HashMap) using [`RandomState`](crate::RandomState) to hash the items. /// Requires the `std` feature to be enabled. #[derive(Clone)] pub struct AHashMap(HashMap); impl AHashMap where K: Hash + Eq, S: BuildHasher + Default, { pub fn new() -> Self { AHashMap(HashMap::with_hasher(S::default())) } pub fn with_capacity(capacity: usize) -> Self { AHashMap(HashMap::with_capacity_and_hasher(capacity, S::default())) } } impl AHashMap where K: Hash + Eq, S: BuildHasher, { pub fn with_hasher(hash_builder: S) -> Self { AHashMap(HashMap::with_hasher(hash_builder)) } pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self { AHashMap(HashMap::with_capacity_and_hasher(capacity, hash_builder)) } } impl Deref for AHashMap { type Target = HashMap; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for AHashMap { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl UnwindSafe for AHashMap where K: UnwindSafe, V: UnwindSafe, { } impl PartialEq for AHashMap where K: Eq + Hash, V: PartialEq, S: BuildHasher, { fn eq(&self, other: &AHashMap) -> bool { self.0.eq(&other.0) } } impl Eq for AHashMap where K: Eq + Hash, V: Eq, S: BuildHasher, { } impl Index<&Q> for AHashMap where K: Eq + Hash + Borrow, Q: Eq + Hash, S: BuildHasher, { type Output = V; /// Returns a reference to the value corresponding to the supplied key. /// /// # Panics /// /// Panics if the key is not present in the `HashMap`. #[inline] fn index(&self, key: &Q) -> &V { self.0.index(key) } } impl Debug for AHashMap where K: Eq + Hash + Debug, V: Debug, S: BuildHasher, { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(fmt) } } impl FromIterator<(K, V)> for AHashMap where K: Eq + Hash, S: BuildHasher + Default, { fn from_iter>(iter: T) -> Self { AHashMap(HashMap::from_iter(iter)) } } impl<'a, K, V, S> IntoIterator for &'a AHashMap { type Item = (&'a K, &'a V); type IntoIter = hash_map::Iter<'a, K, V>; fn into_iter(self) -> Self::IntoIter { (&self.0).iter() } } impl<'a, K, V, S> IntoIterator for &'a mut AHashMap { type Item = (&'a K, &'a mut V); type IntoIter = hash_map::IterMut<'a, K, V>; fn into_iter(self) -> Self::IntoIter { (&mut self.0).iter_mut() } } impl IntoIterator for AHashMap { type Item = (K, V); type IntoIter = hash_map::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl Extend<(K, V)> for AHashMap where K: Eq + Hash, S: BuildHasher, { #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter) } } impl<'a, K, V, S> Extend<(&'a K, &'a V)> for AHashMap where K: Eq + Hash + Copy + 'a, V: Copy + 'a, S: BuildHasher, { #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter) } } impl Default for AHashMap where K: Eq + Hash, S: BuildHasher + Default, { #[inline] fn default() -> AHashMap { AHashMap::with_hasher(Default::default()) } }