blob: e7f110dd1d4d0ae91a4713caf3efd5549e0ae556 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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));
}
}
|