#![deny(missing_docs, rust_2018_idioms)]
#![forbid(unsafe_code)]
use gix_hash::ObjectId;
pub use hashbrown::{hash_map, hash_set, hash_table, Equivalent};
pub mod sync {
pub struct ObjectIdMap<V> {
shards: [parking_lot::Mutex<super::HashMap<gix_hash::ObjectId, V>>; 256],
}
impl<V> Default for ObjectIdMap<V> {
fn default() -> Self {
Self {
shards: std::array::from_fn(|_| parking_lot::Mutex::new(super::HashMap::default())),
}
}
}
impl<V> ObjectIdMap<V> {
pub fn insert(&self, key: gix_hash::ObjectId, value: V) -> Option<V> {
self.shards[key.as_slice()[0] as usize].lock().insert(key, value)
}
}
}
pub mod hash {
#[derive(Default, Clone, Copy)]
pub struct Hasher(u64);
macro_rules! panic_other_writers {
($func:ident, $type:ty) => {
#[cold]
fn $func(&mut self, _i: $type) {
panic!("This hasher only supports manually verified `Hash` implementations")
}
};
}
impl std::hash::Hasher for Hasher {
fn finish(&self) -> u64 {
self.0
}
#[inline(always)]
fn write(&mut self, bytes: &[u8]) {
self.0 = u64::from_ne_bytes(bytes[..8].try_into().unwrap());
}
panic_other_writers!(write_u8, u8);
panic_other_writers!(write_u16, u16);
panic_other_writers!(write_u32, u32);
panic_other_writers!(write_u64, u64);
panic_other_writers!(write_u128, u128);
panic_other_writers!(write_usize, usize);
panic_other_writers!(write_i8, i8);
panic_other_writers!(write_i16, i16);
panic_other_writers!(write_i32, i32);
panic_other_writers!(write_i64, i64);
panic_other_writers!(write_i128, i128);
panic_other_writers!(write_isize, isize);
}
#[derive(Default, Clone, Copy)]
pub struct Builder;
impl std::hash::BuildHasher for Builder {
type Hasher = Hasher;
fn build_hasher(&self) -> Self::Hasher {
Hasher::default()
}
}
}
pub type HashMap<K, V> = hashbrown::HashMap<K, V, hash::Builder>;
pub type HashSet<T = ObjectId> = hashbrown::HashSet<T, hash::Builder>;