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 | fn default() -> Self { |
63 | Self { |
64 | inner: detail::MapImpl::default(), |
65 | } |
66 | } |
67 | } |
68 | |
69 | impl<K, V> Map<K, V> { |
70 | /// Creates a new empty [`Map`]. |
71 | #[inline ] |
72 | pub fn new() -> Self { |
73 | Self::default() |
74 | } |
75 | |
76 | /// Clears the [`Map`], removing all elements. |
77 | #[inline ] |
78 | pub fn clear(&mut self) { |
79 | self.inner.clear() |
80 | } |
81 | |
82 | /// Returns the number of elements in the [`Map`]. |
83 | #[inline ] |
84 | pub fn len(&self) -> usize { |
85 | self.inner.len() |
86 | } |
87 | |
88 | /// Returns `true` if the [`Map`] contains no elements. |
89 | #[inline ] |
90 | pub fn is_empty(&self) -> bool { |
91 | self.inner.is_empty() |
92 | } |
93 | |
94 | /// Returns an iterator that yields the items in the [`Map`]. |
95 | #[inline ] |
96 | pub fn iter(&self) -> Iter<'_, K, V> { |
97 | Iter { |
98 | inner: self.inner.iter(), |
99 | } |
100 | } |
101 | |
102 | /// Returns a mutable iterator that yields the items in the [`Map`]. |
103 | #[inline ] |
104 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { |
105 | IterMut { |
106 | inner: self.inner.iter_mut(), |
107 | } |
108 | } |
109 | |
110 | /// Returns an iterator that yields the keys in the [`Map`]. |
111 | #[inline ] |
112 | pub fn keys(&self) -> Keys<'_, K, V> { |
113 | Keys { |
114 | inner: self.inner.keys(), |
115 | } |
116 | } |
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 | pub fn into_keys(self) -> IntoKeys<K, V> { |
124 | IntoKeys { |
125 | inner: self.inner.into_keys(), |
126 | } |
127 | } |
128 | |
129 | /// Returns an iterator that yields the values in the [`Map`]. |
130 | #[inline ] |
131 | pub fn values(&self) -> Values<'_, K, V> { |
132 | Values { |
133 | inner: self.inner.values(), |
134 | } |
135 | } |
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 | pub fn into_values(self) -> IntoValues<K, V> { |
143 | IntoValues { |
144 | inner: self.inner.into_values(), |
145 | } |
146 | } |
147 | |
148 | /// Returns a mutable iterator that yields the values in the [`Map`]. |
149 | #[inline ] |
150 | pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { |
151 | ValuesMut { |
152 | inner: self.inner.values_mut(), |
153 | } |
154 | } |
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 | pub fn reserve(&mut self, additional: usize) { |
164 | #[cfg (all( |
165 | feature = "hash-collections" , |
166 | not(feature = "prefer-btree-collections" ) |
167 | ))] |
168 | self.inner.reserve(additional); |
169 | #[cfg (any( |
170 | not(feature = "hash-collections" ), |
171 | feature = "prefer-btree-collections" |
172 | ))] |
173 | let _ = additional; |
174 | } |
175 | |
176 | /// Returns true if `key` is contains in the [`Map`]. |
177 | #[inline ] |
178 | pub fn contains_key<Q>(&self, key: &Q) -> bool |
179 | where |
180 | K: Borrow<Q>, |
181 | Q: ?Sized + Hash + Eq + Ord, |
182 | { |
183 | self.inner.contains_key(key) |
184 | } |
185 | |
186 | /// Returns a reference to the value corresponding to the `key`. |
187 | #[inline ] |
188 | pub fn get<Q>(&self, key: &Q) -> Option<&V> |
189 | where |
190 | K: Borrow<Q>, |
191 | Q: ?Sized + Hash + Eq + Ord, |
192 | { |
193 | self.inner.get(key) |
194 | } |
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 | pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> |
202 | where |
203 | K: Borrow<Q>, |
204 | Q: ?Sized + Hash + Eq + Ord, |
205 | { |
206 | self.inner.get_key_value(key) |
207 | } |
208 | |
209 | /// Returns a mutable reference to the value corresponding to the key. |
210 | #[inline ] |
211 | pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> |
212 | where |
213 | K: Borrow<Q>, |
214 | Q: ?Sized + Hash + Eq + Ord, |
215 | { |
216 | self.inner.get_mut(key) |
217 | } |
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 | pub fn insert(&mut self, key: K, value: V) -> Option<V> { |
228 | self.inner.insert(key, value) |
229 | } |
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 | pub fn remove<Q>(&mut self, key: &Q) -> Option<V> |
234 | where |
235 | K: Borrow<Q>, |
236 | Q: ?Sized + Hash + Eq + Ord, |
237 | { |
238 | self.inner.remove(key) |
239 | } |
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 | pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> |
248 | where |
249 | K: Borrow<Q>, |
250 | Q: ?Sized + Hash + Ord, |
251 | { |
252 | self.inner.remove_entry(key) |
253 | } |
254 | |
255 | /// Gets the given key's corresponding entry in the [`Map`] for in-place manipulation. |
256 | #[inline ] |
257 | pub fn entry(&mut self, key: K) -> Entry<'_, K, V> { |
258 | match self.inner.entry(key) { |
259 | detail::EntryImpl::Occupied(entry) => Entry::Occupied(OccupiedEntry { inner: entry }), |
260 | detail::EntryImpl::Vacant(entry) => Entry::Vacant(VacantEntry { inner: entry }), |
261 | } |
262 | } |
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 | pub fn retain<F>(&mut self, f: F) |
270 | where |
271 | F: FnMut(&K, &mut V) -> bool, |
272 | { |
273 | self.inner.retain(f) |
274 | } |
275 | } |
276 | |
277 | impl<K, V> PartialEq for Map<K, V> |
278 | where |
279 | K: Eq + Hash, |
280 | V: Eq, |
281 | { |
282 | #[inline ] |
283 | fn eq(&self, other: &Self) -> bool { |
284 | self.inner == other.inner |
285 | } |
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 | fn index(&self, key: &Q) -> &V { |
304 | &self.inner[key] |
305 | } |
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 | fn extend<Iter: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: Iter) { |
315 | self.inner.extend(iter) |
316 | } |
317 | } |
318 | |
319 | impl<K, V> Extend<(K, V)> for Map<K, V> |
320 | where |
321 | K: Eq + Hash + Ord, |
322 | { |
323 | #[inline ] |
324 | fn extend<Iter: IntoIterator<Item = (K, V)>>(&mut self, iter: Iter) { |
325 | self.inner.extend(iter) |
326 | } |
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 | pub fn or_insert(self, default: V) -> &'a mut V { |
348 | match self { |
349 | Self::Occupied(entry) => entry.into_mut(), |
350 | Self::Vacant(entry) => entry.insert(default), |
351 | } |
352 | } |
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 | pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V { |
358 | match self { |
359 | Self::Occupied(entry) => entry.into_mut(), |
360 | Self::Vacant(entry) => entry.insert(default()), |
361 | } |
362 | } |
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 | pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V { |
372 | match self { |
373 | Self::Occupied(entry) => entry.into_mut(), |
374 | Self::Vacant(entry) => { |
375 | let value = default(entry.key()); |
376 | entry.insert(value) |
377 | } |
378 | } |
379 | } |
380 | |
381 | /// Returns a reference to this [`Entry`]'s key. |
382 | #[inline ] |
383 | pub fn key(&self) -> &K { |
384 | match *self { |
385 | Self::Occupied(ref entry) => entry.key(), |
386 | Self::Vacant(ref entry) => entry.key(), |
387 | } |
388 | } |
389 | |
390 | /// Provides in-place mutable access to an occupied [`Entry`] before any |
391 | /// potential inserts into the map. |
392 | #[inline ] |
393 | pub fn and_modify<F>(self, f: F) -> Self |
394 | where |
395 | F: FnOnce(&mut V), |
396 | { |
397 | match self { |
398 | Self::Occupied(mut entry) => { |
399 | f(entry.get_mut()); |
400 | Self::Occupied(entry) |
401 | } |
402 | Self::Vacant(entry) => Self::Vacant(entry), |
403 | } |
404 | } |
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 | pub fn or_default(self) -> &'a mut V { |
416 | match self { |
417 | Self::Occupied(entry: OccupiedEntry<'a, K, V>) => entry.into_mut(), |
418 | Self::Vacant(entry: VacantEntry<'a, K, V>) => entry.insert(Default::default()), |
419 | } |
420 | } |
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 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
436 | self.inner.fmt(f) |
437 | } |
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 | pub fn key(&self) -> &K { |
448 | self.inner.key() |
449 | } |
450 | |
451 | /// Gets a reference to the value in the entry. |
452 | #[inline ] |
453 | pub fn get(&self) -> &V { |
454 | self.inner.get() |
455 | } |
456 | |
457 | /// Gets a mutable reference to the value in the entry. |
458 | #[inline ] |
459 | pub fn get_mut(&mut self) -> &mut V { |
460 | self.inner.get_mut() |
461 | } |
462 | |
463 | /// Sets the value of the entry with the [`OccupiedEntry`]'s key, and returns the entry's old value. |
464 | #[inline ] |
465 | pub fn insert(&mut self, value: V) -> V { |
466 | self.inner.insert(value) |
467 | } |
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 | pub fn into_mut(self) -> &'a mut V { |
473 | self.inner.into_mut() |
474 | } |
475 | |
476 | /// Take ownership of the key and value from the [`Map`]. |
477 | #[inline ] |
478 | pub fn remove_entry(self) -> (K, V) { |
479 | self.inner.remove_entry() |
480 | } |
481 | |
482 | /// Takes the value of the entry out of the [`Map`], and returns it. |
483 | #[inline ] |
484 | pub fn remove(self) -> V { |
485 | self.inner.remove() |
486 | } |
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 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
502 | self.inner.fmt(f) |
503 | } |
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 | pub fn key(&self) -> &K { |
514 | self.inner.key() |
515 | } |
516 | |
517 | /// Take ownership of the key. |
518 | #[inline ] |
519 | pub fn into_key(self) -> K { |
520 | self.inner.into_key() |
521 | } |
522 | |
523 | /// Sets the value of the entry with the [`VacantEntry`]'s key, and returns a mutable reference to it. |
524 | #[inline ] |
525 | pub fn insert(self, value: V) -> &'a mut V |
526 | where |
527 | K: Hash, |
528 | { |
529 | self.inner.insert(value) |
530 | } |
531 | } |
532 | |
533 | impl<K, V> FromIterator<(K, V)> for Map<K, V> |
534 | where |
535 | K: Hash + Eq + Ord, |
536 | { |
537 | #[inline ] |
538 | fn from_iter<I>(iter: I) -> Self |
539 | where |
540 | I: IntoIterator<Item = (K, V)>, |
541 | { |
542 | Self { |
543 | inner: <detail::MapImpl<K, V>>::from_iter(iter), |
544 | } |
545 | } |
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 | fn into_iter(self) -> Self::IntoIter { |
554 | self.iter() |
555 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
569 | self.inner.size_hint() |
570 | } |
571 | |
572 | #[inline ] |
573 | fn next(&mut self) -> Option<Self::Item> { |
574 | self.inner.next() |
575 | } |
576 | } |
577 | |
578 | impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> { |
579 | #[inline ] |
580 | fn len(&self) -> usize { |
581 | self.inner.len() |
582 | } |
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 | fn into_iter(self) -> Self::IntoIter { |
596 | self.iter_mut() |
597 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
611 | self.inner.size_hint() |
612 | } |
613 | |
614 | #[inline ] |
615 | fn next(&mut self) -> Option<Self::Item> { |
616 | self.inner.next() |
617 | } |
618 | } |
619 | |
620 | impl<'a, K: 'a, V: 'a> ExactSizeIterator for IterMut<'a, K, V> { |
621 | #[inline ] |
622 | fn len(&self) -> usize { |
623 | self.inner.len() |
624 | } |
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 | fn into_iter(self) -> Self::IntoIter { |
638 | IntoIter { |
639 | inner: self.inner.into_iter(), |
640 | } |
641 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
655 | self.inner.size_hint() |
656 | } |
657 | |
658 | #[inline ] |
659 | fn next(&mut self) -> Option<Self::Item> { |
660 | self.inner.next() |
661 | } |
662 | } |
663 | |
664 | impl<K, V> ExactSizeIterator for IntoIter<K, V> { |
665 | #[inline ] |
666 | fn len(&self) -> usize { |
667 | self.inner.len() |
668 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
684 | self.inner.size_hint() |
685 | } |
686 | |
687 | #[inline ] |
688 | fn next(&mut self) -> Option<Self::Item> { |
689 | self.inner.next() |
690 | } |
691 | } |
692 | |
693 | impl<'a, K: 'a, V> ExactSizeIterator for Keys<'a, K, V> { |
694 | #[inline ] |
695 | fn len(&self) -> usize { |
696 | self.inner.len() |
697 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
713 | self.inner.size_hint() |
714 | } |
715 | |
716 | #[inline ] |
717 | fn next(&mut self) -> Option<Self::Item> { |
718 | self.inner.next() |
719 | } |
720 | } |
721 | |
722 | impl<'a, K, V: 'a> ExactSizeIterator for Values<'a, K, V> { |
723 | #[inline ] |
724 | fn len(&self) -> usize { |
725 | self.inner.len() |
726 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
745 | self.inner.size_hint() |
746 | } |
747 | |
748 | #[inline ] |
749 | fn next(&mut self) -> Option<Self::Item> { |
750 | self.inner.next() |
751 | } |
752 | } |
753 | |
754 | impl<'a, K, V: 'a> ExactSizeIterator for ValuesMut<'a, K, V> { |
755 | #[inline ] |
756 | fn len(&self) -> usize { |
757 | self.inner.len() |
758 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
777 | self.inner.size_hint() |
778 | } |
779 | |
780 | #[inline ] |
781 | fn next(&mut self) -> Option<Self::Item> { |
782 | self.inner.next() |
783 | } |
784 | } |
785 | |
786 | impl<K, V> ExactSizeIterator for IntoKeys<K, V> { |
787 | #[inline ] |
788 | fn len(&self) -> usize { |
789 | self.inner.len() |
790 | } |
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 | fn size_hint(&self) -> (usize, Option<usize>) { |
806 | self.inner.size_hint() |
807 | } |
808 | |
809 | #[inline ] |
810 | fn next(&mut self) -> Option<Self::Item> { |
811 | self.inner.next() |
812 | } |
813 | } |
814 | |
815 | impl<K, V> ExactSizeIterator for IntoValues<K, V> { |
816 | #[inline ] |
817 | fn len(&self) -> usize { |
818 | self.inner.len() |
819 | } |
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 | } |
853 | |