Skip to main content

runtime_context/
hasher.rs

1use std::{
2    any::TypeId,
3    collections::HashMap,
4    hash::{BuildHasherDefault, Hasher},
5};
6
7/// A hasher for `TypeId`s that takes advantage of its known characteristics.
8#[derive(Debug, Default)]
9pub struct TypeIdHasher(u64);
10
11/// A `HashMap` optimized for `TypeId` keys.
12pub type TypeMap<V> = HashMap<TypeId, V, BuildHasherDefault<TypeIdHasher>>;
13
14impl Hasher for TypeIdHasher {
15    fn write(&mut self, _: &[u8]) {
16        unimplemented!("This TypeIdHasher can only handle u64s")
17    }
18
19    fn write_u64(&mut self, i: u64) {
20        self.0 = i;
21    }
22
23    fn finish(&self) -> u64 {
24        self.0
25    }
26}