Skip to main content

fixed_map/map/storage/
hashbrown.rs

1use core::hash::Hash;
2use core::iter;
3
4use crate::map::{Entry, MapStorage, OccupiedEntry, VacantEntry};
5
6type S = ::hashbrown::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/// [`MapStorage`] for dynamic types, using [`hashbrown::HashMap`].
12///
13/// This allows for dynamic types such as `&'static str` or `u32` to be used as
14/// a [`Key`][crate::Key].
15///
16/// # Examples
17///
18/// ```
19/// use fixed_map::{Key, Map};
20///
21/// #[derive(Clone, Copy, Key)]
22/// enum MyKey {
23///     First(u32),
24///     Second,
25/// }
26///
27/// let mut map = Map::new();
28/// map.insert(MyKey::First(1), 10);
29/// assert_eq!(map.get(MyKey::First(1)).copied(), Some(10));
30/// assert_eq!(map.get(MyKey::First(2)), None);
31/// assert_eq!(map.get(MyKey::Second), None);
32/// ```
33#[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>
124        = iter::Map<
125        ::hashbrown::hash_map::Iter<'this, K, V>,
126        fn((&'this K, &'this V)) -> (K, &'this V),
127    >
128    where
129        K: 'this,
130        V: 'this;
131    type Keys<'this>
132        = iter::Copied<::hashbrown::hash_map::Keys<'this, K, V>>
133    where
134        K: 'this,
135        V: 'this;
136    type Values<'this>
137        = ::hashbrown::hash_map::Values<'this, K, V>
138    where
139        K: 'this,
140        V: 'this;
141    type IterMut<'this>
142        = iter::Map<
143        ::hashbrown::hash_map::IterMut<'this, K, V>,
144        fn((&'this K, &'this mut V)) -> (K, &'this mut V),
145    >
146    where
147        K: 'this,
148        V: 'this;
149    type ValuesMut<'this>
150        = ::hashbrown::hash_map::ValuesMut<'this, K, V>
151    where
152        K: 'this,
153        V: 'this;
154    type IntoIter = ::hashbrown::hash_map::IntoIter<K, V>;
155    type Occupied<'this>
156        = Occupied<'this, K, V>
157    where
158        K: 'this,
159        V: 'this;
160    type Vacant<'this>
161        = Vacant<'this, K, V>
162    where
163        K: 'this,
164        V: 'this;
165
166    #[inline]
167    fn empty() -> Self {
168        Self {
169            inner: ::hashbrown::HashMap::new(),
170        }
171    }
172
173    #[inline]
174    fn len(&self) -> usize {
175        self.inner.len()
176    }
177
178    #[inline]
179    fn is_empty(&self) -> bool {
180        self.inner.is_empty()
181    }
182
183    #[inline]
184    fn insert(&mut self, key: K, value: V) -> Option<V> {
185        self.inner.insert(key, value)
186    }
187
188    #[inline]
189    fn contains_key(&self, key: K) -> bool {
190        self.inner.contains_key(&key)
191    }
192
193    #[inline]
194    fn get(&self, key: K) -> Option<&V> {
195        self.inner.get(&key)
196    }
197
198    #[inline]
199    fn get_mut(&mut self, key: K) -> Option<&mut V> {
200        self.inner.get_mut(&key)
201    }
202
203    #[inline]
204    fn remove(&mut self, key: K) -> Option<V> {
205        self.inner.remove(&key)
206    }
207
208    #[inline]
209    fn retain<F>(&mut self, mut func: F)
210    where
211        F: FnMut(K, &mut V) -> bool,
212    {
213        self.inner.retain(|&k, v| func(k, v));
214    }
215
216    #[inline]
217    fn clear(&mut self) {
218        self.inner.clear();
219    }
220
221    #[inline]
222    fn iter(&self) -> Self::Iter<'_> {
223        let map: fn(_) -> _ = |(k, v): (&K, &V)| (*k, v);
224        self.inner.iter().map(map)
225    }
226
227    #[inline]
228    fn keys(&self) -> Self::Keys<'_> {
229        self.inner.keys().copied()
230    }
231
232    #[inline]
233    fn values(&self) -> Self::Values<'_> {
234        self.inner.values()
235    }
236
237    #[inline]
238    fn iter_mut(&mut self) -> Self::IterMut<'_> {
239        let map: fn(_) -> _ = |(k, v): (&K, &mut V)| (*k, v);
240        self.inner.iter_mut().map(map)
241    }
242
243    #[inline]
244    fn values_mut(&mut self) -> Self::ValuesMut<'_> {
245        self.inner.values_mut()
246    }
247
248    #[inline]
249    fn into_iter(self) -> Self::IntoIter {
250        self.inner.into_iter()
251    }
252
253    #[inline]
254    fn entry(&mut self, key: K) -> Entry<'_, Self, K, V> {
255        match self.inner.entry(key) {
256            HashMapEntry::Occupied(entry) => Entry::Occupied(entry),
257            HashMapEntry::Vacant(entry) => Entry::Vacant(entry),
258        }
259    }
260}