use id_arena::Id;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hasher};
#[derive(Default, Copy, Clone, Debug)]
pub struct BuildIdHasher;
#[derive(Default, Copy, Clone, Debug)]
pub struct IdHasher {
hash: u64,
}
pub type IdHashMap<K, V> = HashMap<Id<K>, V, BuildIdHasher>;
pub type IdHashSet<T> = HashSet<Id<T>, BuildIdHasher>;
impl BuildHasher for BuildIdHasher {
type Hasher = IdHasher;
fn build_hasher(&self) -> IdHasher {
IdHasher { hash: 0 }
}
}
impl Hasher for IdHasher {
fn write_u32(&mut self, amt: u32) {
self.hash <<= 32;
self.hash |= amt as u64;
}
fn write_usize(&mut self, amt: usize) {
self.hash <<= 32;
self.hash |= amt as u64;
}
fn write(&mut self, _other: &[u8]) {
panic!("hashing an `Id` should only be usize/u32")
}
fn finish(&self) -> u64 {
self.hash
}
}