diff --git a/lib.rs b/lib.rs index 1abff27..a020dc4 100644 --- a/lib.rs +++ b/lib.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![deny(missing_docs)] - //! # Fx Hash //! //! This hashing algorithm was extracted from the Rustc compiler. This is the same hashing @@ -25,10 +23,11 @@ //! potentially cause quadratic behavior in `HashMap`s. So it is not recommended to expose //! this hash in places where collissions or DDOS attacks may be a concern. +use std::iter::FromIterator; use std::collections::{HashMap, HashSet}; use std::default::Default; use std::hash::{BuildHasherDefault, Hash, Hasher}; -use std::ops::BitXor; +use std::ops::{BitXor, Deref, DerefMut}; extern crate byteorder; use byteorder::{ByteOrder, NativeEndian}; @@ -38,16 +37,99 @@ pub type FxBuildHasher = BuildHasherDefault; /// A `HashMap` using a default Fx hasher. /// -/// Use `FxHashMap::default()`, not `new()` to create a new `FxHashMap`. -/// To create with a reserved capacity, use `FxHashMap::with_capacity_and_hasher(num, Default::default())`. -pub type FxHashMap = HashMap; +#[repr(transparent)] +#[derive(Debug, Clone, Default)] +pub struct FxHashMap(HashMap); + +impl FxHashMap { + #[inline] + pub fn new() -> Self { + Self(HashMap::with_hasher(FxBuildHasher::default())) + } + + #[inline] + pub fn with_capacity(n: usize) -> Self { + Self(HashMap::with_capacity_and_hasher(n, FxBuildHasher::default())) + } +} + +impl Deref for FxHashMap { + type Target = HashMap; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for FxHashMap { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl FromIterator<(K, V)> for FxHashMap +where + K: Eq + Hash +{ + #[inline] + fn from_iter(iter: I) -> Self + where + I: IntoIterator + { + let inner_set = HashMap::::from_iter(iter); + Self(inner_set) + } +} /// A `HashSet` using a default Fx hasher. -/// -/// Note: Use `FxHashSet::default()`, not `new()` to create a new `FxHashSet`. -/// To create with a reserved capacity, use `FxHashSet::with_capacity_and_hasher(num, Default::default())`. -pub type FxHashSet = HashSet; +#[repr(transparent)] +#[derive(Debug, Clone, Default)] +pub struct FxHashSet(HashSet); + +impl FxHashSet { + #[inline] + pub fn new() -> Self { + Self(HashSet::with_hasher(FxBuildHasher::default())) + } + + #[inline] + pub fn with_capacity(n: usize) -> Self { + Self(HashSet::with_capacity_and_hasher(n, FxBuildHasher::default())) + } +} + +impl DerefMut for FxHashSet { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Deref for FxHashSet { + type Target = HashSet; + + #[inline] + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl FromIterator for FxHashSet +where + V: Eq + Hash +{ + #[inline] + fn from_iter(iter: I) -> Self + where + I: IntoIterator + { + let inner_set = HashSet::::from_iter(iter); + Self(inner_set) + } +} + const ROTATE: u32 = 5; const SEED64: u64 = 0x51_7c_c1_b7_27_22_0a_95; const SEED32: u32 = 0x9e_37_79_b9; @@ -58,7 +140,7 @@ const SEED: usize = SEED32 as usize; const SEED: usize = SEED64 as usize; trait HashWord { - fn hash_word(&mut self, Self); + fn hash_word(&mut self, _: Self); } macro_rules! impl_hash_word {