1use core::hash::Hash;
2use core::iter;
3
4use crate::map::{Entry, MapStorage, OccupiedEntry, VacantEntry};
5
6type S = ::hashbrown::hash_map::DefaultHashBuilder;
7type Occupied<'a, K, V> = ::hashbrown::hash_map::OccupiedEntry<'a, K, V, S>;
8type Vacant<'a, K, V> = ::hashbrown::hash_map::VacantEntry<'a, K, V, S>;
9type HashMapEntry<'a, K, V> = ::hashbrown::hash_map::Entry<'a, K, V, S>;
10
11#[repr(transparent)]
34pub struct HashbrownMapStorage<K, V> {
35 inner: ::hashbrown::HashMap<K, V>,
36}
37
38impl<K, V> Clone for HashbrownMapStorage<K, V>
39where
40 K: Clone,
41 V: Clone,
42{
43 #[inline]
44 fn clone(&self) -> Self {
45 Self {
46 inner: self.inner.clone(),
47 }
48 }
49}
50
51impl<K, V> PartialEq for HashbrownMapStorage<K, V>
52where
53 K: Eq + Hash,
54 V: PartialEq,
55{
56 #[inline]
57 fn eq(&self, other: &Self) -> bool {
58 self.inner.eq(&other.inner)
59 }
60}
61
62impl<K, V> Eq for HashbrownMapStorage<K, V>
63where
64 K: Eq + Hash,
65 V: Eq,
66{
67}
68
69impl<'a, K, V> OccupiedEntry<'a, K, V> for Occupied<'a, K, V>
70where
71 K: Copy,
72{
73 #[inline]
74 fn key(&self) -> K {
75 *self.key()
76 }
77
78 #[inline]
79 fn get(&self) -> &V {
80 self.get()
81 }
82
83 #[inline]
84 fn get_mut(&mut self) -> &mut V {
85 self.get_mut()
86 }
87
88 #[inline]
89 fn into_mut(self) -> &'a mut V {
90 self.into_mut()
91 }
92
93 #[inline]
94 fn insert(&mut self, value: V) -> V {
95 self.insert(value)
96 }
97
98 #[inline]
99 fn remove(self) -> V {
100 self.remove()
101 }
102}
103
104impl<'a, K, V> VacantEntry<'a, K, V> for Vacant<'a, K, V>
105where
106 K: Copy + Hash,
107{
108 #[inline]
109 fn key(&self) -> K {
110 *self.key()
111 }
112
113 #[inline]
114 fn insert(self, value: V) -> &'a mut V {
115 self.insert(value)
116 }
117}
118
119impl<K, V> MapStorage<K, V> for HashbrownMapStorage<K, V>
120where
121 K: Copy + Eq + Hash,
122{
123 type Iter<'this> = iter::Map<::hashbrown::hash_map::Iter<'this, K, V>, fn((&'this K, &'this V)) -> (K, &'this V)> where K: 'this, V: 'this;
124 type Keys<'this> = iter::Copied<::hashbrown::hash_map::Keys<'this, K, V>> where K: 'this, V: 'this;
125 type Values<'this> = ::hashbrown::hash_map::Values<'this, K, V> where K: 'this, V: 'this;
126 type IterMut<'this> = iter::Map<::hashbrown::hash_map::IterMut<'this, K, V>, fn((&'this K, &'this mut V)) -> (K, &'this mut V)> where K: 'this, V: 'this;
127 type ValuesMut<'this> = ::hashbrown::hash_map::ValuesMut<'this, K, V> where K: 'this, V: 'this;
128 type IntoIter = ::hashbrown::hash_map::IntoIter<K, V>;
129 type Occupied<'this> = Occupied<'this, K, V> where K: 'this, V: 'this;
130 type Vacant<'this> = Vacant<'this, K, V> where K: 'this, V: 'this;
131
132 #[inline]
133 fn empty() -> Self {
134 Self {
135 inner: ::hashbrown::HashMap::new(),
136 }
137 }
138
139 #[inline]
140 fn len(&self) -> usize {
141 self.inner.len()
142 }
143
144 #[inline]
145 fn is_empty(&self) -> bool {
146 self.inner.is_empty()
147 }
148
149 #[inline]
150 fn insert(&mut self, key: K, value: V) -> Option<V> {
151 self.inner.insert(key, value)
152 }
153
154 #[inline]
155 fn contains_key(&self, key: K) -> bool {
156 self.inner.contains_key(&key)
157 }
158
159 #[inline]
160 fn get(&self, key: K) -> Option<&V> {
161 self.inner.get(&key)
162 }
163
164 #[inline]
165 fn get_mut(&mut self, key: K) -> Option<&mut V> {
166 self.inner.get_mut(&key)
167 }
168
169 #[inline]
170 fn remove(&mut self, key: K) -> Option<V> {
171 self.inner.remove(&key)
172 }
173
174 #[inline]
175 fn retain<F>(&mut self, mut func: F)
176 where
177 F: FnMut(K, &mut V) -> bool,
178 {
179 self.inner.retain(|&k, v| func(k, v));
180 }
181
182 #[inline]
183 fn clear(&mut self) {
184 self.inner.clear();
185 }
186
187 #[inline]
188 fn iter(&self) -> Self::Iter<'_> {
189 let map: fn(_) -> _ = |(k, v): (&K, &V)| (*k, v);
190 self.inner.iter().map(map)
191 }
192
193 #[inline]
194 fn keys(&self) -> Self::Keys<'_> {
195 self.inner.keys().copied()
196 }
197
198 #[inline]
199 fn values(&self) -> Self::Values<'_> {
200 self.inner.values()
201 }
202
203 #[inline]
204 fn iter_mut(&mut self) -> Self::IterMut<'_> {
205 let map: fn(_) -> _ = |(k, v): (&K, &mut V)| (*k, v);
206 self.inner.iter_mut().map(map)
207 }
208
209 #[inline]
210 fn values_mut(&mut self) -> Self::ValuesMut<'_> {
211 self.inner.values_mut()
212 }
213
214 #[inline]
215 fn into_iter(self) -> Self::IntoIter {
216 self.inner.into_iter()
217 }
218
219 #[inline]
220 fn entry(&mut self, key: K) -> Entry<'_, Self, K, V> {
221 match self.inner.entry(key) {
222 HashMapEntry::Occupied(entry) => Entry::Occupied(entry),
223 HashMapEntry::Vacant(entry) => Entry::Vacant(entry),
224 }
225 }
226}