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
use std::hash::Hasher as StdHasher;

use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Update};
use fnv::FnvHasher;

#[derive(Default)]
pub struct Fnv(FnvHasher);

impl OutputSizeUser for Fnv {
    type OutputSize = digest::typenum::U8;
}

impl HashMarker for Fnv {}

impl Clone for Fnv {
    fn clone(&self) -> Self {
        Self(FnvHasher::with_key(self.0.finish()))
    }
}

impl Update for Fnv {
    fn update(&mut self, data: &[u8]) {
        self.0.write(data);
    }
}

impl FixedOutput for Fnv {
    fn finalize_into(self, out: &mut Output<Self>) {
        let result = self.0.finish();
        let bytes = result.to_ne_bytes();
        out.copy_from_slice(&bytes);
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;

    use super::Fnv;
    use crate::tests::hash;

    #[test]
    fn test_fnv() {
        assert_snapshot!(hash::<Fnv>(""), @"25232284E49CF2CB");
        assert_snapshot!(hash::<Fnv>("hello"), @"0BBDAA8046D830A4");
    }
}