wasmparser/collections/
map.rs

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