highway/
macros.rs

1macro_rules! impl_write {
2    ($hasher_struct:ty) => {
3        #[cfg(feature = "std")]
4        impl ::std::io::Write for $hasher_struct {
5            fn write(&mut self, bytes: &[u8]) -> ::std::io::Result<usize> {
6                $crate::HighwayHash::append(self, bytes);
7                Ok(bytes.len())
8            }
9            fn flush(&mut self) -> ::std::io::Result<()> {
10                Ok(())
11            }
12        }
13    };
14}
15
16macro_rules! impl_hasher {
17    ($hasher_struct:ty) => {
18        impl ::core::hash::Hasher for $hasher_struct {
19            fn write(&mut self, bytes: &[u8]) {
20                $crate::HighwayHash::append(self, bytes);
21            }
22            fn finish(&self) -> u64 {
23                // Reasons why we need to clone. finalize64` mutates internal state so either we need our
24                // Hasher to consume itself or receive a mutable reference on `finish`. We receive neither,
25                // due to finish being a misnomer (additional writes could be expected) and it's intended
26                // for the hasher to merely return it's current state. The issue with HighwayHash is that
27                // there are several rounds of permutations when finalizing a value, and internal state is
28                // modified during that process. We work around these constraints by cloning the hasher and
29                // finalizing that one.
30                $crate::HighwayHash::finalize64(self.clone())
31            }
32        }
33    };
34}