Reduce indirections.

This commit is contained in:
Stephen Chung
2020-12-24 23:22:50 +08:00
parent 809b813def
commit 8f969b1ab5
6 changed files with 79 additions and 80 deletions

View File

@@ -22,23 +22,21 @@ use crate::Shared;
///
/// Panics when hashing any data type other than a [`NonZeroU64`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct StraightHasher(u64);
pub struct StraightHasher(NonZeroU64);
impl Hasher for StraightHasher {
#[inline(always)]
fn finish(&self) -> u64 {
self.0
self.0.get()
}
#[inline(always)]
fn write(&mut self, bytes: &[u8]) {
let mut key = [0_u8; 8];
key.copy_from_slice(&bytes[..8]); // Panics if fewer than 8 bytes
self.0 = u64::from_le_bytes(key);
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
if self.0 == 0 {
self.0 = 42;
}
self.0 = NonZeroU64::new(u64::from_le_bytes(key))
.unwrap_or_else(|| NonZeroU64::new(42).unwrap());
}
}
@@ -51,7 +49,7 @@ impl BuildHasher for StraightHasherBuilder {
#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
StraightHasher(1)
StraightHasher(NonZeroU64::new(42).unwrap())
}
}