diff --git a/Cargo.toml b/Cargo.toml index e6686c4..2dffe86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "rustc-hash" -version = "1.1.0" +version = "1.2.0" authors = ["The Rust Project Developers"] description = "A speedy, non-cryptographic hashing algorithm used by rustc" license = "Apache-2.0/MIT" readme = "README.md" keywords = ["hash", "hasher", "fxhash", "rustc"] repository = "https://github.com/rust-lang/rustc-hash" -edition = "2018" +edition = "2021" [features] default = ["std"] diff --git a/README.md b/README.md index 36b13ca..546a529 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,5 @@ The `std` feature is on by default to enable collections. It can be turned off in `Cargo.toml` like so: ```toml -rustc-hash = { version = "1.1", default-features = false } +rustc-hash = { version = "1.2", default-features = false } ``` diff --git a/src/lib.rs b/src/lib.rs index 62fa223..379c703 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,21 +28,18 @@ mod random_state; mod seeded_state; -use core::convert::TryInto; use core::default::Default; -#[cfg(feature = "std")] -use core::hash::BuildHasherDefault; -use core::hash::Hasher; +use core::hash::{BuildHasher, Hasher}; #[cfg(feature = "std")] use std::collections::{HashMap, HashSet}; /// Type alias for a hash map that uses the Fx hashing algorithm. #[cfg(feature = "std")] -pub type FxHashMap = HashMap>; +pub type FxHashMap = HashMap; /// Type alias for a hash set that uses the Fx hashing algorithm. #[cfg(feature = "std")] -pub type FxHashSet = HashSet>; +pub type FxHashSet = HashSet; #[cfg(feature = "rand")] pub use random_state::{FxHashMapRand, FxHashSetRand, FxRandomState}; @@ -297,13 +294,30 @@ fn hash_bytes(bytes: &[u8]) -> u64 { multiply_mix(s0, s1) ^ (len as u64) } +/// An implementation of [`BuildHasher`] that produces [`FxHasher`]s. +/// +/// ``` +/// use std::hash::BuildHasher; +/// use rustc_hash::FxBuildHasher; +/// assert_ne!(FxBuildHasher.hash_one(1), FxBuildHasher.hash_one(2)); +/// ``` +#[derive(Copy, Clone, Default)] +pub struct FxBuildHasher; + +impl BuildHasher for FxBuildHasher { + type Hasher = FxHasher; + fn build_hasher(&self) -> FxHasher { + FxHasher::default() + } +} + #[cfg(test)] mod tests { #[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] compile_error!("The test suite only supports 64 bit and 32 bit usize"); - use crate::FxHasher; - use core::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher}; + use crate::{FxBuildHasher, FxHasher}; + use core::hash::{BuildHasher, Hash, Hasher}; macro_rules! test_hash { ( @@ -312,7 +326,7 @@ mod tests { )* ) => { $( - assert_eq!(BuildHasherDefault::::default().hash_one($value), $result); + assert_eq!(FxBuildHasher.hash_one($value), $result); )* }; }