|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::{ |
|
|
fmt, |
|
|
hash::{BuildHasher, Hash, Hasher}, |
|
|
ops::Deref, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Debug, Clone)] |
|
|
pub struct PreHashed<I, H = u64> { |
|
|
hash: H, |
|
|
inner: I, |
|
|
} |
|
|
|
|
|
impl<I, H> PreHashed<I, H> { |
|
|
|
|
|
|
|
|
|
|
|
pub fn new(hash: H, inner: I) -> Self { |
|
|
Self { hash, inner } |
|
|
} |
|
|
|
|
|
|
|
|
pub fn into_parts(self) -> (H, I) { |
|
|
(self.hash, self.inner) |
|
|
} |
|
|
|
|
|
fn inner(&self) -> &I { |
|
|
&self.inner |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I: Hash> PreHashed<I, u64> { |
|
|
|
|
|
fn new_from_builder<B: BuildHasher>(hasher: &B, inner: I) -> Self { |
|
|
Self::new(hasher.hash_one(&inner), inner) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I> Deref for PreHashed<I> { |
|
|
type Target = I; |
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
self.inner() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I, H> AsRef<I> for PreHashed<I, H> { |
|
|
fn as_ref(&self) -> &I { |
|
|
self.inner() |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I, H: Hash> Hash for PreHashed<I, H> { |
|
|
fn hash<S: Hasher>(&self, state: &mut S) { |
|
|
self.hash.hash(state) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I: Eq, H> Eq for PreHashed<I, H> {} |
|
|
|
|
|
impl<I: PartialEq, H> PartialEq for PreHashed<I, H> { |
|
|
|
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
self.inner.eq(&other.inner) |
|
|
} |
|
|
} |
|
|
|
|
|
impl<I: fmt::Display, H> fmt::Display for PreHashed<I, H> { |
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
|
self.inner.fmt(f) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)] |
|
|
pub struct PassThroughHash(u64); |
|
|
|
|
|
impl PassThroughHash { |
|
|
pub fn new() -> Self { |
|
|
Default::default() |
|
|
} |
|
|
} |
|
|
|
|
|
impl Hasher for PassThroughHash { |
|
|
fn write(&mut self, _bytes: &[u8]) { |
|
|
unimplemented!("do not use") |
|
|
} |
|
|
|
|
|
fn write_u64(&mut self, i: u64) { |
|
|
self.0 = i; |
|
|
} |
|
|
|
|
|
fn finish(&self) -> u64 { |
|
|
self.0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub trait BuildHasherExt: BuildHasher { |
|
|
type Hash; |
|
|
|
|
|
fn prehash<T: Hash>(&self, value: T) -> PreHashed<T, Self::Hash>; |
|
|
} |
|
|
|
|
|
impl<B: BuildHasher> BuildHasherExt for B { |
|
|
type Hash = u64; |
|
|
|
|
|
fn prehash<T: Hash>(&self, value: T) -> PreHashed<T, Self::Hash> { |
|
|
PreHashed::new_from_builder(self, value) |
|
|
} |
|
|
} |
|
|
|