Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 2 additions & 59 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ mod specialize;
pub use crate::random_state::RandomState;

use core::hash::BuildHasher;
use core::hash::Hash;
use core::hash::Hasher;

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

/// Used for specialization. (Sealed)
pub(crate) trait BuildHasherExt: BuildHasher {
#[doc(hidden)]
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64;

#[doc(hidden)]
fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64;

#[doc(hidden)]
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64;
}

impl<B: BuildHasher> BuildHasherExt for B {
#[inline]
#[cfg(feature = "specialize")]
default fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[inline]
#[cfg(not(feature = "specialize"))]
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[inline]
#[cfg(feature = "specialize")]
default fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[inline]
#[cfg(not(feature = "specialize"))]
fn hash_as_fixed_length<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[inline]
#[cfg(feature = "specialize")]
default fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
#[inline]
#[cfg(not(feature = "specialize"))]
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = self.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
}

// #[inline(never)]
// #[doc(hidden)]
// pub fn hash_test(input: &[u8]) -> u64 {
Expand All @@ -318,6 +259,8 @@ mod test {
use crate::convert::Convert;
use crate::specialize::CallHasher;
use crate::*;
use core::hash::Hash;
use core::hash::Hasher;
use std::collections::HashMap;

#[test]
Expand Down
13 changes: 4 additions & 9 deletions src/random_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ cfg_if::cfg_if! {
use crate::fallback_hash::*;
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "specialize")]{
use crate::BuildHasherExt;
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
extern crate std as alloc;
Expand Down Expand Up @@ -466,9 +461,9 @@ impl BuildHasher for RandomState {
}

#[cfg(feature = "specialize")]
impl BuildHasherExt for RandomState {
impl RandomState {
#[inline]
fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
pub(crate) fn hash_as_u64<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = AHasherU64 {
buffer: self.k1,
pad: self.k0,
Expand All @@ -478,14 +473,14 @@ impl BuildHasherExt for RandomState {
}

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

#[inline]
fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
pub(crate) fn hash_as_str<T: Hash + ?Sized>(&self, value: &T) -> u64 {
let mut hasher = AHasherStr(self.build_hasher());
value.hash(&mut hasher);
hasher.finish()
Expand Down
37 changes: 18 additions & 19 deletions src/specialize.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::RandomState;
use core::hash::BuildHasher;
use core::hash::Hash;
use core::hash::Hasher;
Expand All @@ -7,8 +8,6 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;

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

#[cfg(not(feature = "specialize"))]
Expand All @@ -27,8 +26,8 @@ where
T: Hash + ?Sized,
{
#[inline]
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
let mut hasher = build_hasher.build_hasher();
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
let mut hasher = random_state.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
Expand All @@ -40,8 +39,8 @@ where
T: Hash + ?Sized,
{
#[inline]
default fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
let mut hasher = build_hasher.build_hasher();
default fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
let mut hasher = random_state.build_hasher();
value.hash(&mut hasher);
hasher.finish()
}
Expand All @@ -52,8 +51,8 @@ macro_rules! call_hasher_impl_u64 {
#[cfg(feature = "specialize")]
impl CallHasher for $typ {
#[inline]
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
build_hasher.hash_as_u64(value)
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
random_state.hash_as_u64(value)
}
}
};
Expand All @@ -80,8 +79,8 @@ macro_rules! call_hasher_impl_fixed_length{
#[cfg(feature = "specialize")]
impl CallHasher for $typ {
#[inline]
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
build_hasher.hash_as_fixed_length(value)
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
random_state.hash_as_fixed_length(value)
}
}
};
Expand All @@ -99,32 +98,32 @@ call_hasher_impl_fixed_length!(&isize);
#[cfg(feature = "specialize")]
impl CallHasher for [u8] {
#[inline]
fn get_hash<H: Hash + ?Sized, B: BuildHasher>(value: &H, build_hasher: &B) -> u64 {
build_hasher.hash_as_str(value)
fn get_hash<H: Hash + ?Sized>(value: &H, random_state: &RandomState) -> u64 {
random_state.hash_as_str(value)
}
}

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

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

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

Expand Down
Loading