rune/
hash.rs

1//! Utilities for working with hashes.
2
3use crate::alloc::HashMap;
4
5use core::hash::{BuildHasher, Hasher};
6
7const SEED: u64 = 18446744073709551557u64;
8
9#[doc(inline)]
10pub use rune_core::hash::{
11    Hash, IntoHash, NonZeroHash, ParametersBuilder, ToTypeHash, TooManyParameters,
12};
13
14/// A hash map suitable for storing values with hash keys.
15pub(crate) type Map<T> = HashMap<Hash, T, HashBuildHasher>;
16
17#[derive(Default, Clone, Copy)]
18pub(crate) struct HashBuildHasher;
19
20impl BuildHasher for HashBuildHasher {
21    type Hasher = HashHasher;
22
23    #[inline]
24    fn build_hasher(&self) -> Self::Hasher {
25        HashHasher(SEED)
26    }
27}
28
29pub(crate) struct HashHasher(u64);
30
31impl Hasher for HashHasher {
32    #[inline]
33    fn finish(&self) -> u64 {
34        self.0
35    }
36
37    #[inline]
38    fn write(&mut self, _: &[u8]) {
39        panic!("Hash hashers assume that 64-bit hashes are already random")
40    }
41
42    #[inline]
43    fn write_u64(&mut self, hash: u64) {
44        self.0 ^= hash;
45    }
46}