Skip to main content

ascending_graphics/atlas_set/
hash.rs

1use core::hash::{Hash, Hasher};
2
3/// this is great for reducing ram and increasing speed but it can also increase the chance of a collision
4/// returning since there is no Value to compare against when one is found. This should be fine as any call
5/// to a contains will tell us if it collides or not.
6///
7#[derive(Debug, Eq, PartialEq, Clone, Copy, Default)]
8pub struct Prehashed(pub u64);
9
10impl Prehashed {
11    #[must_use]
12    pub fn new<T: Hash + Eq>(value: T) -> Self {
13        let mut hasher = ahash::AHasher::default();
14        value.hash(&mut hasher);
15        Self(hasher.finish())
16    }
17
18    pub fn from_hash(hash: u64) -> Self {
19        Self(hash)
20    }
21}
22
23impl Hash for Prehashed {
24    fn hash<S: Hasher>(&self, state: &mut S) {
25        self.0.hash(state)
26    }
27}
28
29pub struct Prehasher {
30    hash: u64,
31}
32
33impl Prehasher {
34    /// Constructs a new PreHasher from a already made hashed u64.
35    ///
36    /// # Notes
37    ///
38    #[must_use]
39    pub fn new(hash: u64) -> Self {
40        Self { hash }
41    }
42}
43
44impl Default for Prehasher {
45    fn default() -> Self {
46        Self::new(0)
47    }
48}
49
50impl Hasher for Prehasher {
51    fn write(&mut self, _bytes: &[u8]) {
52        panic!("unsupported operation");
53    }
54
55    fn write_u64(&mut self, i: u64) {
56        self.hash = i;
57    }
58
59    fn finish(&self) -> u64 {
60        self.hash
61    }
62}