associated_list/
iter.rs

1//! Iterators for an [`AssocList`](crate::AssocList) where using the [`Vec`](alloc::vec::Vec)-Iterators directly was not possible.
2
3use core::{
4    marker::PhantomData,
5    slice::{self, Iter},
6};
7
8use alloc::vec::{self, IntoIter};
9
10use crate::allocator::Allocator;
11
12/// Draining Iterator for an [`AssocList`](crate::AssocList).
13/// It is created by the [`drain`](crate::AssocList::drain)-method.
14#[derive(Debug)]
15#[must_use]
16pub struct Drain<'a, K, V, A: Allocator> {
17    #[cfg(feature = "allocator_api")]
18    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
19    pub(crate) iter: vec::Drain<'a, (K, V), A>,
20    #[cfg(not(feature = "allocator_api"))]
21    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
22    pub(crate) iter: vec::Drain<'a, (K, V)>,
23    /// PhantomData
24    pub(crate) phantom: PhantomData<A>,
25}
26
27impl<K, V, A: Allocator> Iterator for Drain<'_, K, V, A> {
28    type Item = (K, V);
29
30    #[inline]
31    fn next(&mut self) -> Option<Self::Item> {
32        self.iter.next()
33    }
34}
35
36// following convention for Iterator-names
37#[allow(clippy::module_name_repetitions)]
38/// Mutable Iterator for an [`AssocList`](crate::AssocList).
39/// It is created by the [`iter_mut`](crate::AssocList::iter_mut)-method.
40#[derive(Debug)]
41#[must_use]
42pub struct IterMut<'a, K, V>(pub(crate) slice::IterMut<'a, (K, V)>);
43
44impl<'a, K, V> Iterator for IterMut<'a, K, V> {
45    type Item = (&'a K, &'a mut V);
46
47    #[inline]
48    fn next(&mut self) -> Option<Self::Item> {
49        self.0.next().map(|(key, value)| (&*key, value))
50    }
51}
52
53/// Iterator for the keys of an [`AssocList`](crate::AssocList).
54/// It is created by the [`keys`](crate::AssocList::keys)-method.
55#[derive(Debug)]
56#[must_use]
57pub struct Keys<'a, K, V>(pub(crate) Iter<'a, (K, V)>);
58
59impl<'a, K, V> Iterator for Keys<'a, K, V> {
60    type Item = &'a K;
61
62    #[inline]
63    fn next(&mut self) -> Option<Self::Item> {
64        self.0.next().map(|(key, _value)| key)
65    }
66}
67
68/// Consuming Iterator for the keys of an [`AssocList`](crate::AssocList).
69/// It is created by the [`into_keys`](crate::AssocList::into_keys)-method.
70#[derive(Debug)]
71#[must_use]
72pub struct IntoKeys<K, V, A: Allocator> {
73    #[cfg(feature = "allocator_api")]
74    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
75    pub(crate) iter: IntoIter<(K, V), A>,
76    #[cfg(not(feature = "allocator_api"))]
77    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
78    pub(crate) iter: IntoIter<(K, V)>,
79    /// PhantomData
80    pub(crate) phantom: PhantomData<A>,
81}
82
83impl<K, V, A: Allocator> Iterator for IntoKeys<K, V, A> {
84    type Item = K;
85
86    #[inline]
87    fn next(&mut self) -> Option<Self::Item> {
88        self.iter.next().map(|(key, _value)| key)
89    }
90}
91
92/// Iterator for the values of an [`AssocList`](crate::AssocList).
93/// It is created by the [`values`](crate::AssocList::values)-method.
94#[derive(Debug)]
95#[must_use]
96pub struct Values<'a, K, V>(pub(crate) Iter<'a, (K, V)>);
97
98impl<'a, K, V> Iterator for Values<'a, K, V> {
99    type Item = &'a V;
100
101    #[inline]
102    fn next(&mut self) -> Option<Self::Item> {
103        self.0.next().map(|(_key, value)| value)
104    }
105}
106
107/// Iterator for the mutable values of an [`AssocList`](crate::AssocList).
108/// It is created by the [`values_mut`](crate::AssocList::values_mut)-method.
109#[derive(Debug)]
110#[must_use]
111pub struct ValuesMut<'a, K, V>(pub(crate) slice::IterMut<'a, (K, V)>);
112
113impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
114    type Item = &'a mut V;
115
116    #[inline]
117    fn next(&mut self) -> Option<Self::Item> {
118        self.0.next().map(|(_key, value)| value)
119    }
120}
121
122/// Consuming Iterator for the values of an [`AssocList`](crate::AssocList).
123/// It is created by the [`into_values`](crate::AssocList::into_values)-method.
124#[derive(Debug)]
125#[must_use]
126pub struct IntoValues<K, V, A: Allocator> {
127    #[cfg(feature = "allocator_api")]
128    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
129    pub(crate) iter: IntoIter<(K, V), A>,
130    #[cfg(not(feature = "allocator_api"))]
131    /// The Iterator from a [`Vec`](alloc::vec::Vec) the implementation is based on.
132    pub(crate) iter: IntoIter<(K, V)>,
133    /// PhantomData
134    pub(crate) phantom: PhantomData<A>,
135}
136
137impl<K, V, A: Allocator> Iterator for IntoValues<K, V, A> {
138    type Item = V;
139
140    #[inline]
141    fn next(&mut self) -> Option<Self::Item> {
142        self.iter.next().map(|(_key, value)| value)
143    }
144}