git_hashtable/lib.rs
1//! Customized HashMap and Hasher implementation optimized for using `ObjectId`s as keys.
2//!
3//! The crate mirrors `std::collections` in layout for familiarity.
4#![deny(missing_docs, rust_2018_idioms)]
5#![forbid(unsafe_code)]
6
7use git_hash::ObjectId;
8pub use hashbrown::{hash_map, hash_set, raw, Equivalent};
9
10///
11pub mod hash {
12 /// A Hasher for usage with HashMap keys that are already robust hashes (like an `ObjectId`).
13 /// The first `8` bytes of the hash are used as the `HashMap` hash
14 #[derive(Default, Clone, Copy)]
15 pub struct Hasher(u64);
16
17 impl std::hash::Hasher for Hasher {
18 fn finish(&self) -> u64 {
19 self.0
20 }
21
22 #[inline(always)]
23 fn write(&mut self, bytes: &[u8]) {
24 self.0 = u64::from_ne_bytes(bytes[..8].try_into().unwrap());
25 }
26 }
27
28 /// A Hasher for usage with HashMap keys that are already robust hashes (like an `ObjectId`).
29 /// The first `8` bytes of the hash are used as the `HashMap` hash
30 #[derive(Default, Clone, Copy)]
31 pub struct Builder;
32 impl std::hash::BuildHasher for Builder {
33 type Hasher = Hasher;
34
35 fn build_hasher(&self) -> Self::Hasher {
36 Hasher::default()
37 }
38 }
39}
40
41/// A HashMap for usage with keys that are already robust hashes (like an `ObjectId`).
42/// The first `8` bytes of the hash are used as the `HashMap` hash
43pub type HashMap<K, V> = hashbrown::HashMap<K, V, hash::Builder>;
44/// A HashSet for usage with keys that are already robust hashes (like an `ObjectId`).
45/// The first `8` bytes of the hash are used as the `HashMap` hash
46pub type HashSet<T = ObjectId> = hashbrown::HashSet<T, hash::Builder>;