aboutsummaryrefslogtreecommitdiff
path: root/src/libs/intertrait/hasher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/intertrait/hasher.rs')
-rw-r--r--src/libs/intertrait/hasher.rs48
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));
+ }
+}