/src/wasm-tools/crates/wasmparser/src/collections/map.rs
Line | Count | Source |
1 | | //! Type definitions for a default map. |
2 | | |
3 | | use core::fmt::Debug; |
4 | | use core::{borrow::Borrow, hash::Hash, iter::FusedIterator, ops::Index}; |
5 | | |
6 | | #[cfg(all( |
7 | | feature = "hash-collections", |
8 | | not(feature = "prefer-btree-collections") |
9 | | ))] |
10 | | mod detail { |
11 | | use crate::collections::hash; |
12 | | use hashbrown::hash_map; |
13 | | |
14 | | pub type MapImpl<K, V> = hash_map::HashMap<K, V, hash::RandomState>; |
15 | | pub type EntryImpl<'a, K, V> = hash_map::Entry<'a, K, V, hash::RandomState>; |
16 | | pub type OccupiedEntryImpl<'a, K, V> = hash_map::OccupiedEntry<'a, K, V, hash::RandomState>; |
17 | | pub type VacantEntryImpl<'a, K, V> = hash_map::VacantEntry<'a, K, V, hash::RandomState>; |
18 | | pub type IterImpl<'a, K, V> = hash_map::Iter<'a, K, V>; |
19 | | pub type IterMutImpl<'a, K, V> = hash_map::IterMut<'a, K, V>; |
20 | | pub type IntoIterImpl<K, V> = hash_map::IntoIter<K, V>; |
21 | | pub type KeysImpl<'a, K, V> = hash_map::Keys<'a, K, V>; |
22 | | pub type ValuesImpl<'a, K, V> = hash_map::Values<'a, K, V>; |
23 | | pub type ValuesMutImpl<'a, K, V> = hash_map::ValuesMut<'a, K, V>; |
24 | | pub type IntoKeysImpl<K, V> = hash_map::IntoKeys<K, V>; |
25 | | pub type IntoValuesImpl<K, V> = hash_map::IntoValues<K, V>; |
26 | | } |
27 | | |
28 | | #[cfg(any( |
29 | | not(feature = "hash-collections"), |
30 | | feature = "prefer-btree-collections" |
31 | | ))] |
32 | | mod detail { |
33 | | use alloc::collections::btree_map; |
34 | | |
35 | | pub type MapImpl<K, V> = btree_map::BTreeMap<K, V>; |
36 | | pub type EntryImpl<'a, K, V> = btree_map::Entry<'a, K, V>; |
37 | | pub type OccupiedEntryImpl<'a, K, V> = btree_map::OccupiedEntry<'a, K, V>; |
38 | | pub type VacantEntryImpl<'a, K, V> = btree_map::VacantEntry<'a, K, V>; |
39 | | pub type IterImpl<'a, K, V> = btree_map::Iter<'a, K, V>; |
40 | | pub type IterMutImpl<'a, K, V> = btree_map::IterMut<'a, K, V>; |
41 | | pub type IntoIterImpl<K, V> = btree_map::IntoIter<K, V>; |
42 | | pub type KeysImpl<'a, K, V> = btree_map::Keys<'a, K, V>; |
43 | | pub type ValuesImpl<'a, K, V> = btree_map::Values<'a, K, V>; |
44 | | pub type ValuesMutImpl<'a, K, V> = btree_map::ValuesMut<'a, K, V>; |
45 | | pub type IntoKeysImpl<K, V> = btree_map::IntoKeys<K, V>; |
46 | | pub type IntoValuesImpl<K, V> = btree_map::IntoValues<K, V>; |
47 | | } |
48 | | |
49 | | /// A default key-value mapping. |
50 | | /// |
51 | | /// Provides an API compatible with both [`HashMap`] and [`BTreeMap`]. |
52 | | /// |
53 | | /// [`HashMap`]: hashbrown::HashMap |
54 | | /// [`BTreeMap`]: alloc::collections::BTreeMap |
55 | | #[derive(Debug, Clone)] |
56 | | pub struct Map<K, V> { |
57 | | inner: detail::MapImpl<K, V>, |
58 | | } |
59 | | |
60 | | impl<K, V> Default for Map<K, V> { |
61 | | #[inline] |
62 | 898k | fn default() -> Self { |
63 | 898k | Self { |
64 | 898k | inner: detail::MapImpl::default(), |
65 | 898k | } |
66 | 898k | } <wasmparser::collections::map::Map<wasmparser::validator::component_types::ResourceId, wasmparser::validator::component_types::ResourceId> as core::default::Default>::default Line | Count | Source | 62 | 16.1k | fn default() -> Self { | 63 | 16.1k | Self { | 64 | 16.1k | inner: detail::MapImpl::default(), | 65 | 16.1k | } | 66 | 16.1k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::ComponentAnyTypeId, wasmparser::validator::component_types::ComponentAnyTypeId> as core::default::Default>::default Line | Count | Source | 62 | 27.4k | fn default() -> Self { | 63 | 27.4k | Self { | 64 | 27.4k | inner: detail::MapImpl::default(), | 65 | 27.4k | } | 66 | 27.4k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::AliasableResourceId, usize> as core::default::Default>::default Line | Count | Source | 62 | 415k | fn default() -> Self { | 63 | 415k | Self { | 64 | 415k | inner: detail::MapImpl::default(), | 65 | 415k | } | 66 | 415k | } |
<wasmparser::collections::map::Map<wasmparser::readers::core::types::RecGroup, wasmparser::validator::types::RecGroupId> as core::default::Default>::default Line | Count | Source | 62 | 45.4k | fn default() -> Self { | 63 | 45.4k | Self { | 64 | 45.4k | inner: detail::MapImpl::default(), | 65 | 45.4k | } | 66 | 45.4k | } |
<wasmparser::collections::map::Map<u32, u32> as core::default::Default>::default Line | Count | Source | 62 | 394k | fn default() -> Self { | 63 | 394k | Self { | 64 | 394k | inner: detail::MapImpl::default(), | 65 | 394k | } | 66 | 394k | } |
|
67 | | } |
68 | | |
69 | | impl<K, V> Map<K, V> { |
70 | | /// Creates a new empty [`Map`]. |
71 | | #[inline] |
72 | 0 | pub fn new() -> Self { |
73 | 0 | Self::default() |
74 | 0 | } |
75 | | |
76 | | /// Clears the [`Map`], removing all elements. |
77 | | #[inline] |
78 | 19.0k | pub fn clear(&mut self) { |
79 | 19.0k | self.inner.clear() |
80 | 19.0k | } |
81 | | |
82 | | /// Returns the number of elements in the [`Map`]. |
83 | | #[inline] |
84 | 0 | pub fn len(&self) -> usize { |
85 | 0 | self.inner.len() |
86 | 0 | } |
87 | | |
88 | | /// Returns `true` if the [`Map`] contains no elements. |
89 | | #[inline] |
90 | 0 | pub fn is_empty(&self) -> bool { |
91 | 0 | self.inner.is_empty() |
92 | 0 | } |
93 | | |
94 | | /// Returns an iterator that yields the items in the [`Map`]. |
95 | | #[inline] |
96 | 0 | pub fn iter(&self) -> Iter<'_, K, V> { |
97 | 0 | Iter { |
98 | 0 | inner: self.inner.iter(), |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | /// Returns a mutable iterator that yields the items in the [`Map`]. |
103 | | #[inline] |
104 | 0 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { |
105 | 0 | IterMut { |
106 | 0 | inner: self.inner.iter_mut(), |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | /// Returns an iterator that yields the keys in the [`Map`]. |
111 | | #[inline] |
112 | 0 | pub fn keys(&self) -> Keys<'_, K, V> { |
113 | 0 | Keys { |
114 | 0 | inner: self.inner.keys(), |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | /// Creates a consuming iterator visiting all the keys in arbitrary order. |
119 | | /// |
120 | | /// The [`Map`] cannot be used after calling this. |
121 | | /// The iterator element type is `K`. |
122 | | #[inline] |
123 | 0 | pub fn into_keys(self) -> IntoKeys<K, V> { |
124 | 0 | IntoKeys { |
125 | 0 | inner: self.inner.into_keys(), |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | /// Returns an iterator that yields the values in the [`Map`]. |
130 | | #[inline] |
131 | 0 | pub fn values(&self) -> Values<'_, K, V> { |
132 | 0 | Values { |
133 | 0 | inner: self.inner.values(), |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | /// Creates a consuming iterator visiting all the values in arbitrary order. |
138 | | /// |
139 | | /// The [`Map`] cannot be used after calling this. |
140 | | /// The iterator element type is `V`. |
141 | | #[inline] |
142 | 0 | pub fn into_values(self) -> IntoValues<K, V> { |
143 | 0 | IntoValues { |
144 | 0 | inner: self.inner.into_values(), |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | /// Returns a mutable iterator that yields the values in the [`Map`]. |
149 | | #[inline] |
150 | 0 | pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { |
151 | 0 | ValuesMut { |
152 | 0 | inner: self.inner.values_mut(), |
153 | 0 | } |
154 | 0 | } |
155 | | } |
156 | | |
157 | | impl<K, V> Map<K, V> |
158 | | where |
159 | | K: Hash + Eq + Ord, |
160 | | { |
161 | | /// Reserves capacity for at least `additional` more elements to be inserted in the [`Map`]. |
162 | | #[inline] |
163 | 0 | pub fn reserve(&mut self, additional: usize) { |
164 | | #[cfg(all( |
165 | | feature = "hash-collections", |
166 | | not(feature = "prefer-btree-collections") |
167 | | ))] |
168 | 0 | self.inner.reserve(additional); |
169 | | #[cfg(any( |
170 | | not(feature = "hash-collections"), |
171 | | feature = "prefer-btree-collections" |
172 | | ))] |
173 | | let _ = additional; |
174 | 0 | } |
175 | | |
176 | | /// Returns true if `key` is contains in the [`Map`]. |
177 | | #[inline] |
178 | 0 | pub fn contains_key<Q>(&self, key: &Q) -> bool |
179 | 0 | where |
180 | 0 | K: Borrow<Q>, |
181 | 0 | Q: ?Sized + Hash + Eq + Ord, |
182 | | { |
183 | 0 | self.inner.contains_key(key) |
184 | 0 | } |
185 | | |
186 | | /// Returns a reference to the value corresponding to the `key`. |
187 | | #[inline] |
188 | 406k | pub fn get<Q>(&self, key: &Q) -> Option<&V> |
189 | 406k | where |
190 | 406k | K: Borrow<Q>, |
191 | 406k | Q: ?Sized + Hash + Eq + Ord, |
192 | | { |
193 | 406k | self.inner.get(key) |
194 | 406k | } <wasmparser::collections::map::Map<wasmparser::validator::component_types::ResourceId, wasmparser::validator::component_types::ResourceId>>::get::<wasmparser::validator::component_types::ResourceId> Line | Count | Source | 188 | 31.5k | pub fn get<Q>(&self, key: &Q) -> Option<&V> | 189 | 31.5k | where | 190 | 31.5k | K: Borrow<Q>, | 191 | 31.5k | Q: ?Sized + Hash + Eq + Ord, | 192 | | { | 193 | 31.5k | self.inner.get(key) | 194 | 31.5k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::ComponentAnyTypeId, wasmparser::validator::component_types::ComponentAnyTypeId>>::get::<wasmparser::validator::component_types::ComponentAnyTypeId> Line | Count | Source | 188 | 314k | pub fn get<Q>(&self, key: &Q) -> Option<&V> | 189 | 314k | where | 190 | 314k | K: Borrow<Q>, | 191 | 314k | Q: ?Sized + Hash + Eq + Ord, | 192 | | { | 193 | 314k | self.inner.get(key) | 194 | 314k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::AliasableResourceId, usize>>::get::<wasmparser::validator::component_types::AliasableResourceId> Line | Count | Source | 188 | 11.0k | pub fn get<Q>(&self, key: &Q) -> Option<&V> | 189 | 11.0k | where | 190 | 11.0k | K: Borrow<Q>, | 191 | 11.0k | Q: ?Sized + Hash + Eq + Ord, | 192 | | { | 193 | 11.0k | self.inner.get(key) | 194 | 11.0k | } |
<wasmparser::collections::map::Map<u32, u32>>::get::<u32> Line | Count | Source | 188 | 48.4k | pub fn get<Q>(&self, key: &Q) -> Option<&V> | 189 | 48.4k | where | 190 | 48.4k | K: Borrow<Q>, | 191 | 48.4k | Q: ?Sized + Hash + Eq + Ord, | 192 | | { | 193 | 48.4k | self.inner.get(key) | 194 | 48.4k | } |
|
195 | | |
196 | | /// Returns the key-value pair corresponding to the supplied key. |
197 | | /// |
198 | | /// The supplied key may be any borrowed form of the map's key type, but the ordering |
199 | | /// on the borrowed form *must* match the ordering on the key type. |
200 | | #[inline] |
201 | 0 | pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> |
202 | 0 | where |
203 | 0 | K: Borrow<Q>, |
204 | 0 | Q: ?Sized + Hash + Eq + Ord, |
205 | | { |
206 | 0 | self.inner.get_key_value(key) |
207 | 0 | } |
208 | | |
209 | | /// Returns a mutable reference to the value corresponding to the key. |
210 | | #[inline] |
211 | 0 | pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> |
212 | 0 | where |
213 | 0 | K: Borrow<Q>, |
214 | 0 | Q: ?Sized + Hash + Eq + Ord, |
215 | | { |
216 | 0 | self.inner.get_mut(key) |
217 | 0 | } |
218 | | |
219 | | /// Inserts a key-value pair into the [`Map`]. |
220 | | /// |
221 | | /// If the map did not have this key present, `None` is returned. |
222 | | /// |
223 | | /// If the map did have this key present, the value is updated, and the old |
224 | | /// value is returned. The key is not updated, though; this matters for |
225 | | /// types that can be `==` without being identical. |
226 | | #[inline] |
227 | 564k | pub fn insert(&mut self, key: K, value: V) -> Option<V> { |
228 | 564k | self.inner.insert(key, value) |
229 | 564k | } <wasmparser::collections::map::Map<wasmparser::validator::component_types::ResourceId, wasmparser::validator::component_types::ResourceId>>::insert Line | Count | Source | 227 | 7.36k | pub fn insert(&mut self, key: K, value: V) -> Option<V> { | 228 | 7.36k | self.inner.insert(key, value) | 229 | 7.36k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::ComponentAnyTypeId, wasmparser::validator::component_types::ComponentAnyTypeId>>::insert Line | Count | Source | 227 | 260k | pub fn insert(&mut self, key: K, value: V) -> Option<V> { | 228 | 260k | self.inner.insert(key, value) | 229 | 260k | } |
<wasmparser::collections::map::Map<wasmparser::validator::component_types::AliasableResourceId, usize>>::insert Line | Count | Source | 227 | 12.1k | pub fn insert(&mut self, key: K, value: V) -> Option<V> { | 228 | 12.1k | self.inner.insert(key, value) | 229 | 12.1k | } |
<wasmparser::collections::map::Map<u32, u32>>::insert Line | Count | Source | 227 | 284k | pub fn insert(&mut self, key: K, value: V) -> Option<V> { | 228 | 284k | self.inner.insert(key, value) | 229 | 284k | } |
|
230 | | |
231 | | /// Removes a key from the [`Map`], returning the value at the key if the key was previously in the map. |
232 | | #[inline] |
233 | 0 | pub fn remove<Q>(&mut self, key: &Q) -> Option<V> |
234 | 0 | where |
235 | 0 | K: Borrow<Q>, |
236 | 0 | Q: ?Sized + Hash + Eq + Ord, |
237 | | { |
238 | 0 | self.inner.remove(key) |
239 | 0 | } |
240 | | |
241 | | /// Removes a key from the [`Map`], returning the stored key and value if the key |
242 | | /// was previously in the map. |
243 | | /// |
244 | | /// The key may be any borrowed form of the map's key type, but the ordering |
245 | | /// on the borrowed form *must* match the ordering on the key type. |
246 | | #[inline] |
247 | 0 | pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
248 | 0 | where |
249 | 0 | K: Borrow<Q>, |
250 | 0 | Q: ?Sized + Hash + Ord, |
251 | | { |
252 | 0 | self.inner.remove_entry(key) |
253 | 0 | } |
254 | | |
255 | | /// Gets the given key's corresponding entry in the [`Map`] for in-place manipulation. |
256 | | #[inline] |
257 | 278k | pub fn entry(&mut self, key: K) -> Entry<'_, K, V> { |
258 | 278k | match self.inner.entry(key) { |
259 | 187k | detail::EntryImpl::Occupied(entry) => Entry::Occupied(OccupiedEntry { inner: entry }), |
260 | 91.4k | detail::EntryImpl::Vacant(entry) => Entry::Vacant(VacantEntry { inner: entry }), |
261 | | } |
262 | 278k | } |
263 | | |
264 | | /// Retains only the elements specified by the predicate. |
265 | | /// |
266 | | /// In other words, remove all pairs `(k, v)` for which `f(&k, &mut v)` returns `false`. |
267 | | /// The elements are visited in ascending key order. |
268 | | #[inline] |
269 | 0 | pub fn retain<F>(&mut self, f: F) |
270 | 0 | where |
271 | 0 | F: FnMut(&K, &mut V) -> bool, |
272 | | { |
273 | 0 | self.inner.retain(f) |
274 | 0 | } |
275 | | } |
276 | | |
277 | | impl<K, V> PartialEq for Map<K, V> |
278 | | where |
279 | | K: Eq + Hash, |
280 | | V: Eq, |
281 | | { |
282 | | #[inline] |
283 | 0 | fn eq(&self, other: &Self) -> bool { |
284 | 0 | self.inner == other.inner |
285 | 0 | } |
286 | | } |
287 | | |
288 | | impl<K, V> Eq for Map<K, V> |
289 | | where |
290 | | K: Eq + Hash, |
291 | | V: Eq, |
292 | | { |
293 | | } |
294 | | |
295 | | impl<K, Q, V> Index<&Q> for Map<K, V> |
296 | | where |
297 | | K: Borrow<Q> + Hash + Eq + Ord, |
298 | | Q: ?Sized + Hash + Eq + Ord, |
299 | | { |
300 | | type Output = V; |
301 | | |
302 | | #[inline] |
303 | 0 | fn index(&self, key: &Q) -> &V { |
304 | 0 | &self.inner[key] |
305 | 0 | } |
306 | | } |
307 | | |
308 | | impl<'a, K, V> Extend<(&'a K, &'a V)> for Map<K, V> |
309 | | where |
310 | | K: Eq + Hash + Ord + Copy, |
311 | | V: Copy, |
312 | | { |
313 | | #[inline] |
314 | 0 | fn extend<Iter: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: Iter) { |
315 | 0 | self.inner.extend(iter) |
316 | 0 | } |
317 | | } |
318 | | |
319 | | impl<K, V> Extend<(K, V)> for Map<K, V> |
320 | | where |
321 | | K: Eq + Hash + Ord, |
322 | | { |
323 | | #[inline] |
324 | 0 | fn extend<Iter: IntoIterator<Item = (K, V)>>(&mut self, iter: Iter) { |
325 | 0 | self.inner.extend(iter) |
326 | 0 | } |
327 | | } |
328 | | |
329 | | /// A view into a single entry in a [`Map`], which may either be vacant or occupied. |
330 | | /// |
331 | | /// This enum is constructed from the entry method on [`Map`]. |
332 | | #[derive(Debug)] |
333 | | pub enum Entry<'a, K: Ord, V> { |
334 | | /// An occupied entry. |
335 | | Occupied(OccupiedEntry<'a, K, V>), |
336 | | /// A vacant entry. |
337 | | Vacant(VacantEntry<'a, K, V>), |
338 | | } |
339 | | |
340 | | impl<'a, K, V> Entry<'a, K, V> |
341 | | where |
342 | | K: Hash + Ord, |
343 | | { |
344 | | /// Ensures a value is in the entry by inserting the default if empty, and returns |
345 | | /// a mutable reference to the value in the entry. |
346 | | #[inline] |
347 | 0 | pub fn or_insert(self, default: V) -> &'a mut V { |
348 | 0 | match self { |
349 | 0 | Self::Occupied(entry) => entry.into_mut(), |
350 | 0 | Self::Vacant(entry) => entry.insert(default), |
351 | | } |
352 | 0 | } |
353 | | |
354 | | /// Ensures a value is in the [`Entry`] by inserting the result of the default function if empty, |
355 | | /// and returns a mutable reference to the value in the entry. |
356 | | #[inline] |
357 | 0 | pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V { |
358 | 0 | match self { |
359 | 0 | Self::Occupied(entry) => entry.into_mut(), |
360 | 0 | Self::Vacant(entry) => entry.insert(default()), |
361 | | } |
362 | 0 | } |
363 | | |
364 | | /// Ensures a value is in the [`Entry`] by inserting, if empty, the result of the default function. |
365 | | /// This method allows for generating key-derived values for insertion by providing the default |
366 | | /// function a reference to the key that was moved during the `.entry(key)` method call. |
367 | | /// |
368 | | /// The reference to the moved key is provided so that cloning or copying the key is |
369 | | /// unnecessary, unlike with `.or_insert_with(|| ... )`. |
370 | | #[inline] |
371 | 0 | pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V { |
372 | 0 | match self { |
373 | 0 | Self::Occupied(entry) => entry.into_mut(), |
374 | 0 | Self::Vacant(entry) => { |
375 | 0 | let value = default(entry.key()); |
376 | 0 | entry.insert(value) |
377 | | } |
378 | | } |
379 | 0 | } |
380 | | |
381 | | /// Returns a reference to this [`Entry`]'s key. |
382 | | #[inline] |
383 | 0 | pub fn key(&self) -> &K { |
384 | 0 | match *self { |
385 | 0 | Self::Occupied(ref entry) => entry.key(), |
386 | 0 | Self::Vacant(ref entry) => entry.key(), |
387 | | } |
388 | 0 | } |
389 | | |
390 | | /// Provides in-place mutable access to an occupied [`Entry`] before any |
391 | | /// potential inserts into the map. |
392 | | #[inline] |
393 | 0 | pub fn and_modify<F>(self, f: F) -> Self |
394 | 0 | where |
395 | 0 | F: FnOnce(&mut V), |
396 | | { |
397 | 0 | match self { |
398 | 0 | Self::Occupied(mut entry) => { |
399 | 0 | f(entry.get_mut()); |
400 | 0 | Self::Occupied(entry) |
401 | | } |
402 | 0 | Self::Vacant(entry) => Self::Vacant(entry), |
403 | | } |
404 | 0 | } |
405 | | } |
406 | | |
407 | | impl<'a, K, V> Entry<'a, K, V> |
408 | | where |
409 | | K: Hash + Ord, |
410 | | V: Default, |
411 | | { |
412 | | /// Ensures a value is in the [`Entry`] by inserting the default value if empty, |
413 | | /// and returns a mutable reference to the value in the entry. |
414 | | #[inline] |
415 | 0 | pub fn or_default(self) -> &'a mut V { |
416 | 0 | match self { |
417 | 0 | Self::Occupied(entry) => entry.into_mut(), |
418 | 0 | Self::Vacant(entry) => entry.insert(Default::default()), |
419 | | } |
420 | 0 | } |
421 | | } |
422 | | |
423 | | /// A view into an occupied entry in a [`Map`]. |
424 | | /// |
425 | | /// It is part of the [`Entry`] enum. |
426 | | pub struct OccupiedEntry<'a, K, V> { |
427 | | inner: detail::OccupiedEntryImpl<'a, K, V>, |
428 | | } |
429 | | |
430 | | impl<'a, K, V> Debug for OccupiedEntry<'a, K, V> |
431 | | where |
432 | | K: Debug + Ord + 'a, |
433 | | V: Debug + 'a, |
434 | | { |
435 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
436 | 0 | self.inner.fmt(f) |
437 | 0 | } |
438 | | } |
439 | | |
440 | | impl<'a, K, V> OccupiedEntry<'a, K, V> |
441 | | where |
442 | | K: Ord + 'a, |
443 | | V: 'a, |
444 | | { |
445 | | /// Gets a reference to the key in the entry. |
446 | | #[inline] |
447 | 0 | pub fn key(&self) -> &K { |
448 | 0 | self.inner.key() |
449 | 0 | } |
450 | | |
451 | | /// Gets a reference to the value in the entry. |
452 | | #[inline] |
453 | 187k | pub fn get(&self) -> &V { |
454 | 187k | self.inner.get() |
455 | 187k | } |
456 | | |
457 | | /// Gets a mutable reference to the value in the entry. |
458 | | #[inline] |
459 | 0 | pub fn get_mut(&mut self) -> &mut V { |
460 | 0 | self.inner.get_mut() |
461 | 0 | } |
462 | | |
463 | | /// Sets the value of the entry with the [`OccupiedEntry`]'s key, and returns the entry's old value. |
464 | | #[inline] |
465 | 0 | pub fn insert(&mut self, value: V) -> V { |
466 | 0 | self.inner.insert(value) |
467 | 0 | } |
468 | | |
469 | | /// Converts the [`OccupiedEntry`] into a mutable reference to the value in the entry |
470 | | /// with a lifetime bound to the map itself. |
471 | | #[inline] |
472 | 0 | pub fn into_mut(self) -> &'a mut V { |
473 | 0 | self.inner.into_mut() |
474 | 0 | } |
475 | | |
476 | | /// Take ownership of the key and value from the [`Map`]. |
477 | | #[inline] |
478 | 0 | pub fn remove_entry(self) -> (K, V) { |
479 | 0 | self.inner.remove_entry() |
480 | 0 | } |
481 | | |
482 | | /// Takes the value of the entry out of the [`Map`], and returns it. |
483 | | #[inline] |
484 | 0 | pub fn remove(self) -> V { |
485 | 0 | self.inner.remove() |
486 | 0 | } |
487 | | } |
488 | | |
489 | | /// A view into a vacant entry in a [`Map`]. |
490 | | /// |
491 | | /// It is part of the [`Entry`] enum. |
492 | | pub struct VacantEntry<'a, K, V> { |
493 | | inner: detail::VacantEntryImpl<'a, K, V>, |
494 | | } |
495 | | |
496 | | impl<'a, K, V> Debug for VacantEntry<'a, K, V> |
497 | | where |
498 | | K: Debug + Ord + 'a, |
499 | | V: Debug + 'a, |
500 | | { |
501 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
502 | 0 | self.inner.fmt(f) |
503 | 0 | } |
504 | | } |
505 | | |
506 | | impl<'a, K, V> VacantEntry<'a, K, V> |
507 | | where |
508 | | K: Ord + 'a, |
509 | | V: 'a, |
510 | | { |
511 | | /// Gets a reference to the key in the entry. |
512 | | #[inline] |
513 | 91.4k | pub fn key(&self) -> &K { |
514 | 91.4k | self.inner.key() |
515 | 91.4k | } |
516 | | |
517 | | /// Take ownership of the key. |
518 | | #[inline] |
519 | 0 | pub fn into_key(self) -> K { |
520 | 0 | self.inner.into_key() |
521 | 0 | } |
522 | | |
523 | | /// Sets the value of the entry with the [`VacantEntry`]'s key, and returns a mutable reference to it. |
524 | | #[inline] |
525 | 91.4k | pub fn insert(self, value: V) -> &'a mut V |
526 | 91.4k | where |
527 | 91.4k | K: Hash, |
528 | | { |
529 | 91.4k | self.inner.insert(value) |
530 | 91.4k | } |
531 | | } |
532 | | |
533 | | impl<K, V> FromIterator<(K, V)> for Map<K, V> |
534 | | where |
535 | | K: Hash + Eq + Ord, |
536 | | { |
537 | | #[inline] |
538 | 0 | fn from_iter<I>(iter: I) -> Self |
539 | 0 | where |
540 | 0 | I: IntoIterator<Item = (K, V)>, |
541 | | { |
542 | 0 | Self { |
543 | 0 | inner: <detail::MapImpl<K, V>>::from_iter(iter), |
544 | 0 | } |
545 | 0 | } |
546 | | } |
547 | | |
548 | | impl<'a, K, V> IntoIterator for &'a Map<K, V> { |
549 | | type Item = (&'a K, &'a V); |
550 | | type IntoIter = Iter<'a, K, V>; |
551 | | |
552 | | #[inline] |
553 | 0 | fn into_iter(self) -> Self::IntoIter { |
554 | 0 | self.iter() |
555 | 0 | } |
556 | | } |
557 | | |
558 | | /// An iterator over the items of a [`Map`]. |
559 | | #[derive(Debug, Clone)] |
560 | | pub struct Iter<'a, K, V> { |
561 | | inner: detail::IterImpl<'a, K, V>, |
562 | | } |
563 | | |
564 | | impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> { |
565 | | type Item = (&'a K, &'a V); |
566 | | |
567 | | #[inline] |
568 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
569 | 0 | self.inner.size_hint() |
570 | 0 | } |
571 | | |
572 | | #[inline] |
573 | 0 | fn next(&mut self) -> Option<Self::Item> { |
574 | 0 | self.inner.next() |
575 | 0 | } |
576 | | } |
577 | | |
578 | | impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> { |
579 | | #[inline] |
580 | 0 | fn len(&self) -> usize { |
581 | 0 | self.inner.len() |
582 | 0 | } |
583 | | } |
584 | | |
585 | | impl<'a, K: 'a, V: 'a> FusedIterator for Iter<'a, K, V> where |
586 | | detail::IterImpl<'a, K, V>: FusedIterator |
587 | | { |
588 | | } |
589 | | |
590 | | impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut Map<K, V> { |
591 | | type Item = (&'a K, &'a mut V); |
592 | | type IntoIter = IterMut<'a, K, V>; |
593 | | |
594 | | #[inline] |
595 | 0 | fn into_iter(self) -> Self::IntoIter { |
596 | 0 | self.iter_mut() |
597 | 0 | } |
598 | | } |
599 | | |
600 | | /// An iterator over the mutable items of a [`Map`]. |
601 | | #[derive(Debug)] |
602 | | pub struct IterMut<'a, K, V> { |
603 | | inner: detail::IterMutImpl<'a, K, V>, |
604 | | } |
605 | | |
606 | | impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> { |
607 | | type Item = (&'a K, &'a mut V); |
608 | | |
609 | | #[inline] |
610 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
611 | 0 | self.inner.size_hint() |
612 | 0 | } |
613 | | |
614 | | #[inline] |
615 | 0 | fn next(&mut self) -> Option<Self::Item> { |
616 | 0 | self.inner.next() |
617 | 0 | } |
618 | | } |
619 | | |
620 | | impl<'a, K: 'a, V: 'a> ExactSizeIterator for IterMut<'a, K, V> { |
621 | | #[inline] |
622 | 0 | fn len(&self) -> usize { |
623 | 0 | self.inner.len() |
624 | 0 | } |
625 | | } |
626 | | |
627 | | impl<'a, K: 'a, V: 'a> FusedIterator for IterMut<'a, K, V> where |
628 | | detail::IterMutImpl<'a, K, V>: FusedIterator |
629 | | { |
630 | | } |
631 | | |
632 | | impl<K, V> IntoIterator for Map<K, V> { |
633 | | type Item = (K, V); |
634 | | type IntoIter = IntoIter<K, V>; |
635 | | |
636 | | #[inline] |
637 | 0 | fn into_iter(self) -> Self::IntoIter { |
638 | 0 | IntoIter { |
639 | 0 | inner: self.inner.into_iter(), |
640 | 0 | } |
641 | 0 | } |
642 | | } |
643 | | |
644 | | /// An iterator over the owned items of an [`Map`]. |
645 | | #[derive(Debug)] |
646 | | pub struct IntoIter<K, V> { |
647 | | inner: detail::IntoIterImpl<K, V>, |
648 | | } |
649 | | |
650 | | impl<K, V> Iterator for IntoIter<K, V> { |
651 | | type Item = (K, V); |
652 | | |
653 | | #[inline] |
654 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
655 | 0 | self.inner.size_hint() |
656 | 0 | } |
657 | | |
658 | | #[inline] |
659 | 0 | fn next(&mut self) -> Option<Self::Item> { |
660 | 0 | self.inner.next() |
661 | 0 | } |
662 | | } |
663 | | |
664 | | impl<K, V> ExactSizeIterator for IntoIter<K, V> { |
665 | | #[inline] |
666 | 0 | fn len(&self) -> usize { |
667 | 0 | self.inner.len() |
668 | 0 | } |
669 | | } |
670 | | |
671 | | impl<K, V> FusedIterator for IntoIter<K, V> where detail::IntoIterImpl<K, V>: FusedIterator {} |
672 | | |
673 | | /// An iterator over the keys of a [`Map`]. |
674 | | #[derive(Debug, Clone)] |
675 | | pub struct Keys<'a, K, V> { |
676 | | inner: detail::KeysImpl<'a, K, V>, |
677 | | } |
678 | | |
679 | | impl<'a, K: 'a, V> Iterator for Keys<'a, K, V> { |
680 | | type Item = &'a K; |
681 | | |
682 | | #[inline] |
683 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
684 | 0 | self.inner.size_hint() |
685 | 0 | } |
686 | | |
687 | | #[inline] |
688 | 0 | fn next(&mut self) -> Option<Self::Item> { |
689 | 0 | self.inner.next() |
690 | 0 | } |
691 | | } |
692 | | |
693 | | impl<'a, K: 'a, V> ExactSizeIterator for Keys<'a, K, V> { |
694 | | #[inline] |
695 | 0 | fn len(&self) -> usize { |
696 | 0 | self.inner.len() |
697 | 0 | } |
698 | | } |
699 | | |
700 | | impl<'a, K: 'a, V> FusedIterator for Keys<'a, K, V> where detail::KeysImpl<'a, K, V>: FusedIterator {} |
701 | | |
702 | | /// An iterator over the values of a [`Map`]. |
703 | | #[derive(Debug, Clone)] |
704 | | pub struct Values<'a, K, V> { |
705 | | inner: detail::ValuesImpl<'a, K, V>, |
706 | | } |
707 | | |
708 | | impl<'a, K, V: 'a> Iterator for Values<'a, K, V> { |
709 | | type Item = &'a V; |
710 | | |
711 | | #[inline] |
712 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
713 | 0 | self.inner.size_hint() |
714 | 0 | } |
715 | | |
716 | | #[inline] |
717 | 0 | fn next(&mut self) -> Option<Self::Item> { |
718 | 0 | self.inner.next() |
719 | 0 | } |
720 | | } |
721 | | |
722 | | impl<'a, K, V: 'a> ExactSizeIterator for Values<'a, K, V> { |
723 | | #[inline] |
724 | 0 | fn len(&self) -> usize { |
725 | 0 | self.inner.len() |
726 | 0 | } |
727 | | } |
728 | | |
729 | | impl<'a, K, V: 'a> FusedIterator for Values<'a, K, V> where |
730 | | detail::ValuesImpl<'a, K, V>: FusedIterator |
731 | | { |
732 | | } |
733 | | |
734 | | /// An mutable iterator over the values of a [`Map`]. |
735 | | #[derive(Debug)] |
736 | | pub struct ValuesMut<'a, K, V> { |
737 | | inner: detail::ValuesMutImpl<'a, K, V>, |
738 | | } |
739 | | |
740 | | impl<'a, K, V: 'a> Iterator for ValuesMut<'a, K, V> { |
741 | | type Item = &'a mut V; |
742 | | |
743 | | #[inline] |
744 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
745 | 0 | self.inner.size_hint() |
746 | 0 | } |
747 | | |
748 | | #[inline] |
749 | 0 | fn next(&mut self) -> Option<Self::Item> { |
750 | 0 | self.inner.next() |
751 | 0 | } |
752 | | } |
753 | | |
754 | | impl<'a, K, V: 'a> ExactSizeIterator for ValuesMut<'a, K, V> { |
755 | | #[inline] |
756 | 0 | fn len(&self) -> usize { |
757 | 0 | self.inner.len() |
758 | 0 | } |
759 | | } |
760 | | |
761 | | impl<'a, K, V: 'a> FusedIterator for ValuesMut<'a, K, V> where |
762 | | detail::ValuesMutImpl<'a, K, V>: FusedIterator |
763 | | { |
764 | | } |
765 | | |
766 | | /// An iterator over the owned keys of a [`Map`]. |
767 | | #[derive(Debug)] |
768 | | pub struct IntoKeys<K, V> { |
769 | | inner: detail::IntoKeysImpl<K, V>, |
770 | | } |
771 | | |
772 | | impl<K, V> Iterator for IntoKeys<K, V> { |
773 | | type Item = K; |
774 | | |
775 | | #[inline] |
776 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
777 | 0 | self.inner.size_hint() |
778 | 0 | } |
779 | | |
780 | | #[inline] |
781 | 0 | fn next(&mut self) -> Option<Self::Item> { |
782 | 0 | self.inner.next() |
783 | 0 | } |
784 | | } |
785 | | |
786 | | impl<K, V> ExactSizeIterator for IntoKeys<K, V> { |
787 | | #[inline] |
788 | 0 | fn len(&self) -> usize { |
789 | 0 | self.inner.len() |
790 | 0 | } |
791 | | } |
792 | | |
793 | | impl<K, V> FusedIterator for IntoKeys<K, V> where detail::IntoKeysImpl<K, V>: FusedIterator {} |
794 | | |
795 | | /// An iterator over the owned values of a [`Map`]. |
796 | | #[derive(Debug)] |
797 | | pub struct IntoValues<K, V> { |
798 | | inner: detail::IntoValuesImpl<K, V>, |
799 | | } |
800 | | |
801 | | impl<K, V> Iterator for IntoValues<K, V> { |
802 | | type Item = V; |
803 | | |
804 | | #[inline] |
805 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
806 | 0 | self.inner.size_hint() |
807 | 0 | } |
808 | | |
809 | | #[inline] |
810 | 0 | fn next(&mut self) -> Option<Self::Item> { |
811 | 0 | self.inner.next() |
812 | 0 | } |
813 | | } |
814 | | |
815 | | impl<K, V> ExactSizeIterator for IntoValues<K, V> { |
816 | | #[inline] |
817 | 0 | fn len(&self) -> usize { |
818 | 0 | self.inner.len() |
819 | 0 | } |
820 | | } |
821 | | |
822 | | impl<K, V> FusedIterator for IntoValues<K, V> where detail::IntoValuesImpl<K, V>: FusedIterator {} |
823 | | |
824 | | #[cfg(feature = "serde")] |
825 | | impl<K, V> serde::Serialize for Map<K, V> |
826 | | where |
827 | | K: serde::Serialize + Eq + Hash + Ord, |
828 | | V: serde::Serialize, |
829 | | { |
830 | | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
831 | | where |
832 | | S: serde::ser::Serializer, |
833 | | { |
834 | | serde::Serialize::serialize(&self.inner, serializer) |
835 | | } |
836 | | } |
837 | | |
838 | | #[cfg(feature = "serde")] |
839 | | impl<'a, K, V> serde::Deserialize<'a> for Map<K, V> |
840 | | where |
841 | | K: serde::Deserialize<'a> + Eq + Hash + Ord, |
842 | | V: serde::Deserialize<'a>, |
843 | | { |
844 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
845 | | where |
846 | | D: serde::de::Deserializer<'a>, |
847 | | { |
848 | | Ok(Map { |
849 | | inner: serde::Deserialize::deserialize(deserializer)?, |
850 | | }) |
851 | | } |
852 | | } |