diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-20 14:29:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-20 14:29:45 +0200 |
commit | 2d1a6b2d432408d74eb57e0bda3f7434617e1070 (patch) | |
tree | 7e21f8126edfdfd9c40b4b51ba5626c6440442d9 /src/libs/intertrait/hasher.rs | |
parent | 7863d9859a5cbce99c3769e4fdb40283115d358d (diff) |
refactor: reorganize folder hierarchy
Diffstat (limited to 'src/libs/intertrait/hasher.rs')
-rw-r--r-- | src/libs/intertrait/hasher.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libs/intertrait/hasher.rs b/src/libs/intertrait/hasher.rs new file mode 100644 index 0000000..e7f110d --- /dev/null +++ b/src/libs/intertrait/hasher.rs @@ -0,0 +1,48 @@ +#![allow(clippy::module_name_repetitions)] + +/** + * Originally from Intertrait by CodeChain + * + * <https://github.com/CodeChain-io/intertrait> + * <https://crates.io/crates/intertrait/0.2.2> + * + * Licensed under either of + * + * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>) + * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>) + + * at your option. +*/ +use std::convert::TryInto; +use std::hash::{BuildHasherDefault, Hasher}; +use std::mem::size_of; + +/// A simple `Hasher` implementation tuned for performance. +#[derive(Default)] +pub struct FastHasher(u64); + +/// A `BuildHasher` for `FastHasher`. +pub type BuildFastHasher = BuildHasherDefault<FastHasher>; + +impl Hasher for FastHasher +{ + fn finish(&self) -> u64 + { + self.0 + } + + fn write(&mut self, bytes: &[u8]) + { + let mut bytes = bytes; + while bytes.len() > size_of::<u64>() { + let (u64_bytes, remaining) = bytes.split_at(size_of::<u64>()); + + self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap()); + + bytes = remaining; + } + self.0 ^= bytes + .iter() + .fold(0u64, |result, b| (result << 8) | u64::from(*b)); + } +} |