hax_frontend_exporter/
deterministic_hash.rs

1//! Stolen from <https://github.com/Wassasin/deterministic-hash/blob/main/src/lib.rs>
2use core::hash::Hasher;
3
4/// Wrapper around any hasher to make it deterministic.
5#[derive(Default)]
6pub struct DeterministicHasher<T: Hasher>(T);
7
8/// Implementation of hasher that forces all bytes written to be platform agnostic.
9impl<T: Hasher> core::hash::Hasher for DeterministicHasher<T> {
10    fn finish(&self) -> u64 {
11        self.0.finish()
12    }
13
14    fn write(&mut self, bytes: &[u8]) {
15        self.0.write(bytes);
16    }
17
18    fn write_u8(&mut self, i: u8) {
19        self.write(&i.to_le_bytes())
20    }
21
22    fn write_u16(&mut self, i: u16) {
23        self.write(&i.to_le_bytes())
24    }
25
26    fn write_u32(&mut self, i: u32) {
27        self.write(&i.to_le_bytes())
28    }
29
30    fn write_u64(&mut self, i: u64) {
31        self.write(&i.to_le_bytes())
32    }
33
34    fn write_u128(&mut self, i: u128) {
35        self.write(&i.to_le_bytes())
36    }
37
38    fn write_usize(&mut self, i: usize) {
39        self.write(&(i as u64).to_le_bytes())
40    }
41
42    fn write_i8(&mut self, i: i8) {
43        self.write_u8(i as u8)
44    }
45
46    fn write_i16(&mut self, i: i16) {
47        self.write_u16(i as u16)
48    }
49
50    fn write_i32(&mut self, i: i32) {
51        self.write_u32(i as u32)
52    }
53
54    fn write_i64(&mut self, i: i64) {
55        self.write_u64(i as u64)
56    }
57
58    fn write_i128(&mut self, i: i128) {
59        self.write_u128(i as u128)
60    }
61
62    fn write_isize(&mut self, i: isize) {
63        self.write_usize(i as usize)
64    }
65}