veloren_common/util/
grid_hasher.rs

1use std::hash::{BuildHasher, Hasher};
2
3#[derive(Copy, Clone, Default)]
4pub struct GridHasher(u64);
5
6// It's extremely unlikely that the spatial grid can be used to viably DOS the
7// server given that clients only have control over their player and a handful
8// of entities in their near vicinity. For this reason, we just use an xor hash,
9// which should keep collisions relatively low since the spatial coherence of
10// the grid is distributed fairly evenly with the output of the hash function.
11impl Hasher for GridHasher {
12    fn finish(&self) -> u64 { self.0 }
13
14    fn write(&mut self, _: &[u8]) {
15        panic!("Hashing arbitrary bytes is unimplemented");
16    }
17
18    fn write_i32(&mut self, x: i32) { self.0 = self.0.wrapping_mul(113989) ^ self.0 ^ x as u64; }
19}
20
21impl BuildHasher for GridHasher {
22    type Hasher = Self;
23
24    fn build_hasher(&self) -> Self::Hasher { *self }
25}