/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hash32-0.3.1/src/fnv.rs
Line | Count | Source |
1 | | use crate::Hasher as _; |
2 | | |
3 | | const BASIS: u32 = 0x811c9dc5; |
4 | | const PRIME: u32 = 0x1000193; |
5 | | |
6 | | /// 32-bit Fowler-Noll-Vo hasher |
7 | | pub struct Hasher { |
8 | | state: u32, |
9 | | } |
10 | | |
11 | | impl Default for Hasher { |
12 | 0 | fn default() -> Self { |
13 | 0 | Hasher { state: BASIS } |
14 | 0 | } |
15 | | } |
16 | | |
17 | | impl crate::Hasher for Hasher { |
18 | | #[inline] |
19 | 0 | fn finish32(&self) -> u32 { |
20 | 0 | self.state |
21 | 0 | } |
22 | | } |
23 | | |
24 | | impl core::hash::Hasher for Hasher { |
25 | | #[inline] |
26 | 0 | fn write(&mut self, bytes: &[u8]) { |
27 | 0 | for byte in bytes { |
28 | 0 | self.state ^= u32::from(*byte); |
29 | 0 | self.state = self.state.wrapping_mul(PRIME); |
30 | 0 | } |
31 | 0 | } |
32 | | |
33 | | #[inline] |
34 | 0 | fn finish(&self) -> u64 { |
35 | 0 | self.finish32().into() |
36 | 0 | } |
37 | | } |