Skip to content

Commit a5ada48

Browse files
authored
Make CallHasher specific to ahash::RandomState (#261)
* Make CallHasher specific to ahash::RandomState * Eliminate BuildHasherExt trait * Rename argument of CallHasher to random_state
1 parent 3a54154 commit a5ada48

3 files changed

Lines changed: 24 additions & 87 deletions

File tree

‎src/lib.rs‎

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ mod specialize;
146146
pub use crate::random_state::RandomState;
147147

148148
use core::hash::BuildHasher;
149-
use core::hash::Hash;
150-
use core::hash::Hasher;
151149

152150
#[cfg(feature = "std")]
153151
/// A convenience trait that can be used together with the type aliases defined to
@@ -248,63 +246,6 @@ impl Default for AHasher {
248246
}
249247
}
250248

251-
/// Used for specialization. (Sealed)
252-
pub(crate) trait BuildHasherExt: BuildHasher {
253-
#[doc(hidden)]
254-
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64;
255-
256-
#[doc(hidden)]
257-
fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64;
258-
259-
#[doc(hidden)]
260-
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64;
261-
}
262-
263-
impl<B: BuildHasher> BuildHasherExt for B {
264-
#[inline]
265-
#[cfg(feature = "specialize")]
266-
default fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
267-
let mut hasher = self.build_hasher();
268-
value.hash(&mut hasher);
269-
hasher.finish()
270-
}
271-
#[inline]
272-
#[cfg(not(feature = "specialize"))]
273-
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
274-
let mut hasher = self.build_hasher();
275-
value.hash(&mut hasher);
276-
hasher.finish()
277-
}
278-
#[inline]
279-
#[cfg(feature = "specialize")]
280-
default fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
281-
let mut hasher = self.build_hasher();
282-
value.hash(&mut hasher);
283-
hasher.finish()
284-
}
285-
#[inline]
286-
#[cfg(not(feature = "specialize"))]
287-
fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
288-
let mut hasher = self.build_hasher();
289-
value.hash(&mut hasher);
290-
hasher.finish()
291-
}
292-
#[inline]
293-
#[cfg(feature = "specialize")]
294-
default fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
295-
let mut hasher = self.build_hasher();
296-
value.hash(&mut hasher);
297-
hasher.finish()
298-
}
299-
#[inline]
300-
#[cfg(not(feature = "specialize"))]
301-
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
302-
let mut hasher = self.build_hasher();
303-
value.hash(&mut hasher);
304-
hasher.finish()
305-
}
306-
}
307-
308249
// #[inline(never)]
309250
// #[doc(hidden)]
310251
// pub fn hash_test(input: &[u8]) -> u64 {
@@ -318,6 +259,8 @@ mod test {
318259
use crate::convert::Convert;
319260
use crate::specialize::CallHasher;
320261
use crate::*;
262+
use core::hash::Hash;
263+
use core::hash::Hasher;
321264
use std::collections::HashMap;
322265

323266
#[test]

‎src/random_state.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ cfg_if::cfg_if! {
1010
use crate::fallback_hash::*;
1111
}
1212
}
13-
cfg_if::cfg_if! {
14-
if #[cfg(feature = "specialize")]{
15-
use crate::BuildHasherExt;
16-
}
17-
}
1813
cfg_if::cfg_if! {
1914
if #[cfg(feature = "std")] {
2015
extern crate std as alloc;
@@ -466,9 +461,9 @@ impl BuildHasher for RandomState {
466461
}
467462

468463
#[cfg(feature = "specialize")]
469-
impl BuildHasherExt for RandomState {
464+
impl RandomState {
470465
#[inline]
471-
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
466+
pub(crate) fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
472467
let mut hasher = AHasherU64 {
473468
buffer: self.k1,
474469
pad: self.k0,
@@ -478,14 +473,14 @@ impl BuildHasherExt for RandomState {
478473
}
479474

480475
#[inline]
481-
fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
476+
pub(crate) fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
482477
let mut hasher = AHasherFixed(self.build_hasher());
483478
value.hash(&mut hasher);
484479
hasher.finish()
485480
}
486481

487482
#[inline]
488-
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
483+
pub(crate) fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
489484
let mut hasher = AHasherStr(self.build_hasher());
490485
value.hash(&mut hasher);
491486
hasher.finish()

‎src/specialize.rs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::RandomState;
12
use core::hash::BuildHasher;
23
use core::hash::Hash;
34
use core::hash::Hasher;
@@ -7,8 +8,6 @@ extern crate alloc;
78
#[cfg(feature = "std")]
89
extern crate std as alloc;
910

10-
#[cfg(feature = "specialize")]
11-
use crate::BuildHasherExt;
1211
#[cfg(feature = "specialize")]
1312
use alloc::string::String;
1413
#[cfg(feature = "specialize")]
@@ -18,7 +17,7 @@ use alloc::vec::Vec;
1817
/// Rather than using a Hasher generically which can hash any value, this provides a way to get a specialized hash
1918
/// for a specific type. So this may be faster for primitive types.
2019
pub(crate) trait CallHasher {
21-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64;
20+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64;
2221
}
2322

2423
#[cfg(not(feature = "specialize"))]
@@ -27,8 +26,8 @@ where
2726
T: Hash + ?Sized,
2827
{
2928
#[inline]
30-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
31-
let mut hasher = build_hasher.build_hasher();
29+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
30+
let mut hasher = random_state.build_hasher();
3231
value.hash(&mut hasher);
3332
hasher.finish()
3433
}
@@ -40,8 +39,8 @@ where
4039
T: Hash + ?Sized,
4140
{
4241
#[inline]
43-
default fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
44-
let mut hasher = build_hasher.build_hasher();
42+
default fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
43+
let mut hasher = random_state.build_hasher();
4544
value.hash(&mut hasher);
4645
hasher.finish()
4746
}
@@ -52,8 +51,8 @@ macro_rules! call_hasher_impl_u64 {
5251
#[cfg(feature = "specialize")]
5352
impl CallHasher for $typ {
5453
#[inline]
55-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
56-
build_hasher.hash_as_u64(value)
54+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
55+
random_state.hash_as_u64(value)
5756
}
5857
}
5958
};
@@ -80,8 +79,8 @@ macro_rules! call_hasher_impl_fixed_length{
8079
#[cfg(feature = "specialize")]
8180
impl CallHasher for $typ {
8281
#[inline]
83-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
84-
build_hasher.hash_as_fixed_length(value)
82+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
83+
random_state.hash_as_fixed_length(value)
8584
}
8685
}
8786
};
@@ -99,32 +98,32 @@ call_hasher_impl_fixed_length!(&isize);
9998
#[cfg(feature = "specialize")]
10099
impl CallHasher for [u8] {
101100
#[inline]
102-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
103-
build_hasher.hash_as_str(value)
101+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
102+
random_state.hash_as_str(value)
104103
}
105104
}
106105

107106
#[cfg(feature = "specialize")]
108107
impl CallHasher for Vec<u8> {
109108
#[inline]
110-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
111-
build_hasher.hash_as_str(value)
109+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
110+
random_state.hash_as_str(value)
112111
}
113112
}
114113

115114
#[cfg(feature = "specialize")]
116115
impl CallHasher for str {
117116
#[inline]
118-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
119-
build_hasher.hash_as_str(value)
117+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
118+
random_state.hash_as_str(value)
120119
}
121120
}
122121

123122
#[cfg(all(feature = "specialize"))]
124123
impl CallHasher for String {
125124
#[inline]
126-
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
127-
build_hasher.hash_as_str(value)
125+
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
126+
random_state.hash_as_str(value)
128127
}
129128
}
130129

0 commit comments

Comments
 (0)