Skip to main content

djs_collection/
iter.rs

1//! The iterators of a [`Collection`](crate::Collection), in insertion order (D-217).
2
3use core::{fmt, iter::FusedIterator};
4
5/// Defines an iterator wrapping one of `indexmap`'s, so that its type stays private.
6macro_rules! iterator {
7    (
8        $(#[$meta:meta])*
9        $name:ident<$($lifetime:lifetime,)? K, V> => $inner:ident, $item:ty $(,)?
10    ) => {
11        $(#[$meta])*
12        pub struct $name<$($lifetime,)? K, V>(pub(crate) indexmap::map::$inner<$($lifetime,)? K, V>);
13
14        impl<$($lifetime,)? K, V> Iterator for $name<$($lifetime,)? K, V> {
15            type Item = $item;
16
17            fn next(&mut self) -> Option<Self::Item> {
18                self.0.next()
19            }
20
21            fn size_hint(&self) -> (usize, Option<usize>) {
22                self.0.size_hint()
23            }
24
25            fn count(self) -> usize {
26                self.0.count()
27            }
28
29            fn nth(&mut self, n: usize) -> Option<Self::Item> {
30                self.0.nth(n)
31            }
32
33            fn last(self) -> Option<Self::Item> {
34                self.0.last()
35            }
36        }
37
38        impl<$($lifetime,)? K, V> DoubleEndedIterator for $name<$($lifetime,)? K, V> {
39            fn next_back(&mut self) -> Option<Self::Item> {
40                self.0.next_back()
41            }
42
43            fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
44                self.0.nth_back(n)
45            }
46        }
47
48        impl<$($lifetime,)? K, V> ExactSizeIterator for $name<$($lifetime,)? K, V> {
49            fn len(&self) -> usize {
50                self.0.len()
51            }
52        }
53
54        impl<$($lifetime,)? K, V> FusedIterator for $name<$($lifetime,)? K, V> {}
55
56        impl<$($lifetime,)? K: fmt::Debug, V: fmt::Debug> fmt::Debug for $name<$($lifetime,)? K, V> {
57            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58                f.debug_tuple(stringify!($name)).field(&self.0).finish()
59            }
60        }
61    };
62}
63
64/// Implements `Clone` for an iterator over borrowed entries (no bounds on `K` and `V`).
65macro_rules! clone {
66    ($($name:ident),*) => {
67        $(
68            impl<K, V> Clone for $name<'_, K, V> {
69                fn clone(&self) -> Self {
70                    Self(self.0.clone())
71                }
72            }
73        )*
74    };
75}
76
77iterator! {
78    /// The entries of a collection, borrowed, as `(&key, &value)`. Returned by
79    /// [`Collection::iter`](crate::Collection::iter).
80    Iter<'a, K, V> => Iter, (&'a K, &'a V),
81}
82
83iterator! {
84    /// The entries of a collection with mutable values, as `(&key, &mut value)`. Returned by
85    /// [`Collection::iter_mut`](crate::Collection::iter_mut).
86    IterMut<'a, K, V> => IterMut, (&'a K, &'a mut V),
87}
88
89iterator! {
90    /// The entries of a collection, moved out, as `(key, value)`. Returned by `into_iter` of a
91    /// [`Collection`](crate::Collection).
92    IntoIter<K, V> => IntoIter, (K, V),
93}
94
95iterator! {
96    /// The keys of a collection. Returned by [`Collection::keys`](crate::Collection::keys).
97    Keys<'a, K, V> => Keys, &'a K,
98}
99
100iterator! {
101    /// The values of a collection. Returned by [`Collection::values`](crate::Collection::values).
102    Values<'a, K, V> => Values, &'a V,
103}
104
105iterator! {
106    /// The values of a collection, mutable. Returned by
107    /// [`Collection::values_mut`](crate::Collection::values_mut).
108    ValuesMut<'a, K, V> => ValuesMut, &'a mut V,
109}
110
111iterator! {
112    /// The keys of a collection, moved out. Returned by
113    /// [`Collection::into_keys`](crate::Collection::into_keys).
114    IntoKeys<K, V> => IntoKeys, K,
115}
116
117iterator! {
118    /// The values of a collection, moved out. Returned by
119    /// [`Collection::into_values`](crate::Collection::into_values).
120    IntoValues<K, V> => IntoValues, V,
121}
122
123clone!(Iter, Keys, Values);