Skip to main content

acktor_ipc/double_map/
iter.rs

1//! Iterator types returned by [`DoubleMap`][super::DoubleMap]'s iteration methods
2//! and its [`IntoIterator`] impls.
3
4use std::collections::hash_map;
5use std::iter::FusedIterator;
6
7/// An owning iterator over the entries of a [`DoubleMap`][super::DoubleMap], yielding
8/// `(K1, K2, V)` triples.
9pub struct IntoIter<K1, K2, V> {
10    inner: hash_map::IntoIter<K1, (K2, V)>,
11}
12
13impl<K1, K2, V> IntoIter<K1, K2, V> {
14    pub(super) fn new(inner: hash_map::IntoIter<K1, (K2, V)>) -> Self {
15        Self { inner }
16    }
17}
18
19impl<K1, K2, V> Iterator for IntoIter<K1, K2, V> {
20    type Item = (K1, K2, V);
21
22    fn next(&mut self) -> Option<Self::Item> {
23        self.inner.next().map(|(k1, (k2, v))| (k1, k2, v))
24    }
25
26    fn size_hint(&self) -> (usize, Option<usize>) {
27        self.inner.size_hint()
28    }
29}
30
31impl<K1, K2, V> ExactSizeIterator for IntoIter<K1, K2, V> {
32    fn len(&self) -> usize {
33        self.inner.len()
34    }
35}
36
37impl<K1, K2, V> FusedIterator for IntoIter<K1, K2, V> {}
38
39/// A borrowing iterator over the entries of a [`DoubleMap`][super::DoubleMap], yielding
40/// `(&'a K1, &'a K2, &'a V)` triples.
41pub struct Iter<'a, K1, K2, V> {
42    inner: hash_map::Iter<'a, K1, (K2, V)>,
43}
44
45impl<'a, K1, K2, V> Iter<'a, K1, K2, V> {
46    pub(super) fn new(inner: hash_map::Iter<'a, K1, (K2, V)>) -> Self {
47        Self { inner }
48    }
49}
50
51impl<'a, K1, K2, V> Iterator for Iter<'a, K1, K2, V> {
52    type Item = (&'a K1, &'a K2, &'a V);
53
54    fn next(&mut self) -> Option<Self::Item> {
55        self.inner.next().map(|(k1, (k2, v))| (k1, k2, v))
56    }
57
58    fn size_hint(&self) -> (usize, Option<usize>) {
59        self.inner.size_hint()
60    }
61}
62
63impl<K1, K2, V> ExactSizeIterator for Iter<'_, K1, K2, V> {
64    fn len(&self) -> usize {
65        self.inner.len()
66    }
67}
68
69impl<K1, K2, V> FusedIterator for Iter<'_, K1, K2, V> {}
70
71impl<K1, K2, V> Clone for Iter<'_, K1, K2, V> {
72    fn clone(&self) -> Self {
73        Self {
74            inner: self.inner.clone(),
75        }
76    }
77}
78
79/// A mutably borrowing iterator over the entries of a [`DoubleMap`][super::DoubleMap],
80/// yielding `(&'a K1, &'a K2, &'a mut V)` triples.
81pub struct IterMut<'a, K1, K2, V> {
82    inner: hash_map::IterMut<'a, K1, (K2, V)>,
83}
84
85impl<'a, K1, K2, V> IterMut<'a, K1, K2, V> {
86    pub(super) fn new(inner: hash_map::IterMut<'a, K1, (K2, V)>) -> Self {
87        Self { inner }
88    }
89}
90
91impl<'a, K1, K2, V> Iterator for IterMut<'a, K1, K2, V> {
92    type Item = (&'a K1, &'a K2, &'a mut V);
93
94    fn next(&mut self) -> Option<Self::Item> {
95        self.inner.next().map(|(k1, (k2, v))| (k1, &*k2, v))
96    }
97
98    fn size_hint(&self) -> (usize, Option<usize>) {
99        self.inner.size_hint()
100    }
101}
102
103impl<K1, K2, V> ExactSizeIterator for IterMut<'_, K1, K2, V> {
104    fn len(&self) -> usize {
105        self.inner.len()
106    }
107}
108
109impl<K1, K2, V> FusedIterator for IterMut<'_, K1, K2, V> {}
110
111/// A draining iterator over the entries of a [`DoubleMap`][super::DoubleMap], yielding
112/// `(K1, K2, V)` triples while emptying the map in place.
113pub struct Drain<'a, K1, K2, V> {
114    inner: hash_map::Drain<'a, K1, (K2, V)>,
115}
116
117impl<'a, K1, K2, V> Drain<'a, K1, K2, V> {
118    pub(super) fn new(inner: hash_map::Drain<'a, K1, (K2, V)>) -> Self {
119        Self { inner }
120    }
121}
122
123impl<K1, K2, V> Iterator for Drain<'_, K1, K2, V> {
124    type Item = (K1, K2, V);
125
126    fn next(&mut self) -> Option<Self::Item> {
127        self.inner.next().map(|(k1, (k2, v))| (k1, k2, v))
128    }
129
130    fn size_hint(&self) -> (usize, Option<usize>) {
131        self.inner.size_hint()
132    }
133}
134
135impl<K1, K2, V> ExactSizeIterator for Drain<'_, K1, K2, V> {
136    fn len(&self) -> usize {
137        self.inner.len()
138    }
139}
140
141impl<K1, K2, V> FusedIterator for Drain<'_, K1, K2, V> {}
142
143/// A borrowing iterator over the keys of a [`DoubleMap`][super::DoubleMap], yielding
144/// `(&'a K1, &'a K2)` tuples.
145pub struct Keys<'a, K1, K2, V> {
146    inner: hash_map::Iter<'a, K1, (K2, V)>,
147}
148
149impl<'a, K1, K2, V> Keys<'a, K1, K2, V> {
150    pub(super) fn new(inner: hash_map::Iter<'a, K1, (K2, V)>) -> Self {
151        Self { inner }
152    }
153}
154
155impl<'a, K1, K2, V> Iterator for Keys<'a, K1, K2, V> {
156    type Item = (&'a K1, &'a K2);
157
158    fn next(&mut self) -> Option<Self::Item> {
159        self.inner.next().map(|(k1, (k2, _))| (k1, k2))
160    }
161
162    fn size_hint(&self) -> (usize, Option<usize>) {
163        self.inner.size_hint()
164    }
165}
166
167impl<K1, K2, V> ExactSizeIterator for Keys<'_, K1, K2, V> {
168    fn len(&self) -> usize {
169        self.inner.len()
170    }
171}
172
173impl<K1, K2, V> FusedIterator for Keys<'_, K1, K2, V> {}
174
175impl<K1, K2, V> Clone for Keys<'_, K1, K2, V> {
176    fn clone(&self) -> Self {
177        Self {
178            inner: self.inner.clone(),
179        }
180    }
181}
182
183/// A consuming iterator over the keys of a [`DoubleMap`][super::DoubleMap], yielding
184/// owned `(K1, K2)` tuples.
185pub struct IntoKeys<K1, K2, V> {
186    inner: hash_map::IntoIter<K1, (K2, V)>,
187}
188
189impl<K1, K2, V> IntoKeys<K1, K2, V> {
190    pub(super) fn new(inner: hash_map::IntoIter<K1, (K2, V)>) -> Self {
191        Self { inner }
192    }
193}
194
195impl<K1, K2, V> Iterator for IntoKeys<K1, K2, V> {
196    type Item = (K1, K2);
197
198    fn next(&mut self) -> Option<Self::Item> {
199        self.inner.next().map(|(k1, (k2, _))| (k1, k2))
200    }
201
202    fn size_hint(&self) -> (usize, Option<usize>) {
203        self.inner.size_hint()
204    }
205}
206
207impl<K1, K2, V> ExactSizeIterator for IntoKeys<K1, K2, V> {
208    fn len(&self) -> usize {
209        self.inner.len()
210    }
211}
212
213impl<K1, K2, V> FusedIterator for IntoKeys<K1, K2, V> {}
214
215/// A borrowing iterator over the values of a [`DoubleMap`][super::DoubleMap], yielding
216/// `&'a V`.
217pub struct Values<'a, K1, K2, V> {
218    inner: hash_map::Values<'a, K1, (K2, V)>,
219}
220
221impl<'a, K1, K2, V> Values<'a, K1, K2, V> {
222    pub(super) fn new(inner: hash_map::Values<'a, K1, (K2, V)>) -> Self {
223        Self { inner }
224    }
225}
226
227impl<'a, K1, K2, V> Iterator for Values<'a, K1, K2, V> {
228    type Item = &'a V;
229
230    fn next(&mut self) -> Option<Self::Item> {
231        self.inner.next().map(|(_, v)| v)
232    }
233
234    fn size_hint(&self) -> (usize, Option<usize>) {
235        self.inner.size_hint()
236    }
237}
238
239impl<K1, K2, V> ExactSizeIterator for Values<'_, K1, K2, V> {
240    fn len(&self) -> usize {
241        self.inner.len()
242    }
243}
244
245impl<K1, K2, V> FusedIterator for Values<'_, K1, K2, V> {}
246
247impl<K1, K2, V> Clone for Values<'_, K1, K2, V> {
248    fn clone(&self) -> Self {
249        Self {
250            inner: self.inner.clone(),
251        }
252    }
253}
254
255/// A mutably borrowing iterator over the values of a [`DoubleMap`][super::DoubleMap],
256/// yielding `&'a mut V`.
257pub struct ValuesMut<'a, K1, K2, V> {
258    inner: hash_map::ValuesMut<'a, K1, (K2, V)>,
259}
260
261impl<'a, K1, K2, V> ValuesMut<'a, K1, K2, V> {
262    pub(super) fn new(inner: hash_map::ValuesMut<'a, K1, (K2, V)>) -> Self {
263        Self { inner }
264    }
265}
266
267impl<'a, K1, K2, V> Iterator for ValuesMut<'a, K1, K2, V> {
268    type Item = &'a mut V;
269
270    fn next(&mut self) -> Option<Self::Item> {
271        self.inner.next().map(|(_, v)| v)
272    }
273
274    fn size_hint(&self) -> (usize, Option<usize>) {
275        self.inner.size_hint()
276    }
277}
278
279impl<K1, K2, V> ExactSizeIterator for ValuesMut<'_, K1, K2, V> {
280    fn len(&self) -> usize {
281        self.inner.len()
282    }
283}
284
285impl<K1, K2, V> FusedIterator for ValuesMut<'_, K1, K2, V> {}
286
287/// A consuming iterator over the values of a [`DoubleMap`][super::DoubleMap], yielding
288/// owned `V`.
289pub struct IntoValues<K1, K2, V> {
290    inner: hash_map::IntoValues<K1, (K2, V)>,
291}
292
293impl<K1, K2, V> IntoValues<K1, K2, V> {
294    pub(super) fn new(inner: hash_map::IntoValues<K1, (K2, V)>) -> Self {
295        Self { inner }
296    }
297}
298
299impl<K1, K2, V> Iterator for IntoValues<K1, K2, V> {
300    type Item = V;
301
302    fn next(&mut self) -> Option<Self::Item> {
303        self.inner.next().map(|(_, v)| v)
304    }
305
306    fn size_hint(&self) -> (usize, Option<usize>) {
307        self.inner.size_hint()
308    }
309}
310
311impl<K1, K2, V> ExactSizeIterator for IntoValues<K1, K2, V> {
312    fn len(&self) -> usize {
313        self.inner.len()
314    }
315}
316
317impl<K1, K2, V> FusedIterator for IntoValues<K1, K2, V> {}