1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
#[cfg(feature = "std")]
pub mod sync;
#[cfg(feature = "std")]
pub use sync::{LazyMap, OnceMap};
pub mod unsync;
#[cfg(test)]
mod tests;
use core::{
borrow::Borrow,
hash::{BuildHasher, Hash, Hasher},
};
pub trait Equivalent<K: ?Sized> {
fn equivalent(&self, key: &K) -> bool;
}
impl<Q, K> Equivalent<K> for Q
where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
{
fn equivalent(&self, key: &K) -> bool {
self == key.borrow()
}
}
#[repr(transparent)]
#[derive(Hash)]
struct EquivalentCompat<Q: ?Sized>(Q);
impl<Q: ?Sized> EquivalentCompat<Q> {
#[inline]
fn new(q: &Q) -> &Self {
unsafe { &*(q as *const Q as *const Self) }
}
}
impl<Q, K> hashbrown::Equivalent<K> for EquivalentCompat<Q>
where
Q: Equivalent<K> + ?Sized,
{
#[inline]
fn equivalent(&self, key: &K) -> bool {
self.0.equivalent(key)
}
}
pub trait ToOwnedEquivalent<K>: Equivalent<K> {
fn to_owned_equivalent(&self) -> K;
}
impl<Q> ToOwnedEquivalent<Q::Owned> for Q
where
Q: alloc::borrow::ToOwned + Eq + ?Sized,
{
fn to_owned_equivalent(&self) -> Q::Owned {
self.to_owned()
}
}
fn hash_one<S: BuildHasher, Q: Hash + ?Sized>(hash_builder: &S, key: &Q) -> u64 {
let mut hasher = hash_builder.build_hasher();
key.hash(&mut hasher);
hasher.finish()
}
trait HashMapExt {
type Key;
type Value;
type Hasher;
fn get_raw_entry<Q>(&self, hash: u64, key: &Q) -> Option<(&Self::Key, &Self::Value)>
where
Q: Hash + Equivalent<Self::Key> + ?Sized;
fn get_raw_entry_mut<Q>(
&mut self,
hash: u64,
key: &Q,
) -> hashbrown::hash_map::RawEntryMut<Self::Key, Self::Value, Self::Hasher>
where
Q: Hash + Equivalent<Self::Key> + ?Sized;
}
impl<K, V, S> HashMapExt for hashbrown::HashMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
type Key = K;
type Value = V;
type Hasher = S;
fn get_raw_entry<Q>(&self, hash: u64, key: &Q) -> Option<(&Self::Key, &Self::Value)>
where
Q: Hash + Equivalent<Self::Key> + ?Sized,
{
self.raw_entry()
.from_key_hashed_nocheck(hash, EquivalentCompat::new(key))
}
fn get_raw_entry_mut<Q>(
&mut self,
hash: u64,
key: &Q,
) -> hashbrown::hash_map::RawEntryMut<Self::Key, Self::Value, Self::Hasher>
where
Q: Hash + Equivalent<Self::Key> + ?Sized,
{
self.raw_entry_mut()
.from_key_hashed_nocheck(hash, EquivalentCompat::new(key))
}
}
trait InfallibleResult {
type Ok;
fn unwrap_infallible(self) -> Self::Ok;
}
impl<T> InfallibleResult for Result<T, core::convert::Infallible> {
type Ok = T;
#[inline]
fn unwrap_infallible(self) -> T {
match self {
Ok(v) => v,
Err(void) => match void {},
}
}
}
#[cfg(feature = "ahash")]
use ahash::{AHasher as HasherInner, RandomState as RandomStateInner};
#[cfg(all(not(feature = "ahash"), feature = "std"))]
use std::collections::hash_map::{DefaultHasher as HasherInner, RandomState as RandomStateInner};
#[cfg(all(not(feature = "ahash"), not(feature = "std")))]
compile_error!("Either feature `ahash` or `std` must be enabled");
#[derive(Debug, Clone)]
pub struct RandomState(RandomStateInner);
#[derive(Debug, Clone, Default)]
pub struct DefaultHasher(HasherInner);
impl RandomState {
#[inline]
pub fn new() -> Self {
Self(RandomStateInner::new())
}
}
impl Default for RandomState {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl core::hash::BuildHasher for RandomState {
type Hasher = DefaultHasher;
#[inline]
fn build_hasher(&self) -> Self::Hasher {
DefaultHasher(self.0.build_hasher())
}
}
impl core::hash::Hasher for DefaultHasher {
#[inline]
fn finish(&self) -> u64 {
self.0.finish()
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}
#[inline]
fn write_u8(&mut self, i: u8) {
self.0.write_u8(i)
}
#[inline]
fn write_u16(&mut self, i: u16) {
self.0.write_u16(i)
}
#[inline]
fn write_u32(&mut self, i: u32) {
self.0.write_u32(i)
}
#[inline]
fn write_u64(&mut self, i: u64) {
self.0.write_u64(i)
}
#[inline]
fn write_u128(&mut self, i: u128) {
self.0.write_u128(i)
}
#[inline]
fn write_usize(&mut self, i: usize) {
self.0.write_usize(i)
}
}