Download this file
| 1 | use std::convert::TryFrom; |
| 2 | use std::hash::Hasher; |
| 3 | |
| 4 | // An minimalistic hasher for client-ids! |
| 5 | // Client-ids are already random only have a size of maximum 64 bit. No reason to spin up |
| 6 | // cryptographic functions every time a client is queried. |
| 7 | #[derive(Default)] |
| 8 | pub struct ClientHasher { |
| 9 | prefix: u64, |
| 10 | } |
| 11 | |
| 12 | impl Hasher for ClientHasher { |
| 13 | #[inline] |
| 14 | fn finish(&self) -> u64 { |
| 15 | self.prefix |
| 16 | } |
| 17 | |
| 18 | #[inline] |
| 19 | fn write(&mut self, bytes: &[u8]) { |
| 20 | debug_assert!(bytes.len() <= 8); |
| 21 | // we only expect a single value to be written |
| 22 | debug_assert!(self.prefix == 0); |
| 23 | self.prefix = if bytes.len() == 4 { |
| 24 | u32::from_ne_bytes(<[u8; 4]>::try_from(bytes).unwrap()) as u64 |
| 25 | } else { |
| 26 | u64::from_ne_bytes(<[u8; 8]>::try_from(bytes).unwrap()) |
| 27 | }; |
| 28 | } |
| 29 | } |
| 30 | |