vec_btree_map/
iter.rs

1use core::fmt::{Debug, Formatter, Result};
2use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator};
3use core::slice;
4
5#[must_use = "iterators are lazy and do nothing unless consumed"]
6#[derive(Default)]
7pub struct Iter<'a, K, V> {
8    base: slice::Iter<'a, (K, V)>,
9}
10
11impl<'a, K, V> Iter<'a, K, V> {
12    #[inline]
13    pub(super) fn new(base: slice::Iter<'a, (K, V)>) -> Self {
14        Self { base }
15    }
16}
17
18impl<K, V> Clone for Iter<'_, K, V> {
19    #[inline]
20    fn clone(&self) -> Self {
21        Self::new(self.base.clone())
22    }
23}
24
25impl<'a, K: Debug, V: Debug> Debug for Iter<'a, K, V> {
26    #[inline]
27    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
28        f.debug_list().entries(self.clone()).finish()
29    }
30}
31
32impl<'a, K, V> Iterator for Iter<'a, K, V> {
33    type Item = (&'a K, &'a V);
34
35    #[inline]
36    fn next(&mut self) -> Option<Self::Item> {
37        self.base.next().map(|e| (&e.0, &e.1))
38    }
39
40    #[inline]
41    fn size_hint(&self) -> (usize, Option<usize>) {
42        self.base.size_hint()
43    }
44
45    #[inline]
46    fn count(self) -> usize {
47        self.base.len()
48    }
49
50    #[inline]
51    fn nth(&mut self, n: usize) -> Option<Self::Item> {
52        self.base.nth(n).map(|e| (&e.0, &e.1))
53    }
54
55    #[inline]
56    fn last(self) -> Option<Self::Item> {
57        self.base.last().map(|e| (&e.0, &e.1))
58    }
59
60    #[inline]
61    fn fold<B, F>(self, init: B, mut f: F) -> B
62    where
63        F: FnMut(B, Self::Item) -> B,
64    {
65        self.base.fold(init, |b, (k, v)| f(b, (k, v)))
66    }
67}
68
69impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
70    #[inline]
71    fn next_back(&mut self) -> Option<Self::Item> {
72        self.base.next_back().map(|e| (&e.0, &e.1))
73    }
74
75    #[inline]
76    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
77        self.base.nth_back(n).map(|e| (&e.0, &e.1))
78    }
79}
80
81impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
82    #[inline]
83    fn len(&self) -> usize {
84        self.base.len()
85    }
86}
87
88impl<K, V> FusedIterator for Iter<'_, K, V> {}
89
90#[must_use = "iterators are lazy and do nothing unless consumed"]
91#[derive(Default)]
92pub struct IterMut<'a, K, V> {
93    base: slice::IterMut<'a, (K, V)>,
94}
95
96impl<'a, K, V> IterMut<'a, K, V> {
97    #[inline]
98    pub(super) fn new(base: slice::IterMut<'a, (K, V)>) -> Self {
99        Self { base }
100    }
101}
102
103impl<'a, K: Debug, V: Debug> Debug for IterMut<'a, K, V> {
104    #[inline]
105    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
106        f.debug_list()
107            .entries(Iter::new(self.base.as_slice().iter()))
108            .finish()
109    }
110}
111
112impl<'a, K, V> Iterator for IterMut<'a, K, V> {
113    type Item = (&'a K, &'a mut V);
114
115    #[inline]
116    fn next(&mut self) -> Option<Self::Item> {
117        self.base.next().map(|e| (&e.0, &mut e.1))
118    }
119
120    #[inline]
121    fn size_hint(&self) -> (usize, Option<usize>) {
122        self.base.size_hint()
123    }
124
125    #[inline]
126    fn count(self) -> usize {
127        self.base.len()
128    }
129
130    #[inline]
131    fn nth(&mut self, n: usize) -> Option<Self::Item> {
132        self.base.nth(n).map(|e| (&e.0, &mut e.1))
133    }
134
135    #[inline]
136    fn last(self) -> Option<Self::Item> {
137        self.base.last().map(|e| (&e.0, &mut e.1))
138    }
139
140    #[inline]
141    fn fold<B, F>(self, init: B, mut f: F) -> B
142    where
143        F: FnMut(B, Self::Item) -> B,
144    {
145        self.base.fold(init, |b, (k, v)| f(b, (k, v)))
146    }
147}
148
149impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
150    #[inline]
151    fn next_back(&mut self) -> Option<Self::Item> {
152        self.base.next_back().map(|e| (&e.0, &mut e.1))
153    }
154
155    #[inline]
156    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
157        self.base.nth_back(n).map(|e| (&e.0, &mut e.1))
158    }
159}
160
161impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
162    #[inline]
163    fn len(&self) -> usize {
164        self.base.len()
165    }
166}
167
168impl<K, V> FusedIterator for IterMut<'_, K, V> {}
169
170#[must_use = "iterators are lazy and do nothing unless consumed"]
171#[derive(Default)]
172pub struct Keys<'a, K, V> {
173    base: slice::Iter<'a, (K, V)>,
174}
175
176impl<'a, K, V> Keys<'a, K, V> {
177    #[inline]
178    pub(super) const fn new(base: slice::Iter<'a, (K, V)>) -> Self {
179        Self { base }
180    }
181}
182
183impl<K, V> Clone for Keys<'_, K, V> {
184    #[inline]
185    fn clone(&self) -> Self {
186        Self::new(self.base.clone())
187    }
188}
189
190impl<'a, K: Debug, V: Debug> Debug for Keys<'a, K, V> {
191    #[inline]
192    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
193        f.debug_list().entries(self.clone()).finish()
194    }
195}
196
197impl<'a, K, V> Iterator for Keys<'a, K, V> {
198    type Item = &'a K;
199
200    #[inline]
201    fn next(&mut self) -> Option<Self::Item> {
202        self.base.next().map(|e| &e.0)
203    }
204
205    #[inline]
206    fn size_hint(&self) -> (usize, Option<usize>) {
207        self.base.size_hint()
208    }
209
210    #[inline]
211    fn count(self) -> usize {
212        self.base.len()
213    }
214
215    #[inline]
216    fn nth(&mut self, n: usize) -> Option<Self::Item> {
217        self.base.nth(n).map(|e| &e.0)
218    }
219
220    #[inline]
221    fn last(self) -> Option<Self::Item> {
222        self.base.last().map(|e| &e.0)
223    }
224
225    #[inline]
226    fn fold<B, F>(self, init: B, mut f: F) -> B
227    where
228        F: FnMut(B, Self::Item) -> B,
229    {
230        self.base.fold(init, |b, e| f(b, &e.0))
231    }
232}
233
234impl<K, V> DoubleEndedIterator for Keys<'_, K, V> {
235    #[inline]
236    fn next_back(&mut self) -> Option<Self::Item> {
237        self.base.next_back().map(|e| &e.0)
238    }
239
240    #[inline]
241    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
242        self.base.nth_back(n).map(|e| &e.0)
243    }
244}
245
246impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
247    #[inline]
248    fn len(&self) -> usize {
249        self.base.len()
250    }
251}
252
253impl<K, V> FusedIterator for Keys<'_, K, V> {}
254
255#[must_use = "iterators are lazy and do nothing unless consumed"]
256#[derive(Default)]
257pub struct Values<'a, K, V> {
258    base: slice::Iter<'a, (K, V)>,
259}
260
261impl<'a, K, V> Values<'a, K, V> {
262    #[inline]
263    pub(super) const fn new(base: slice::Iter<'a, (K, V)>) -> Self {
264        Self { base }
265    }
266}
267
268impl<K, V> Clone for Values<'_, K, V> {
269    #[inline]
270    fn clone(&self) -> Self {
271        Self::new(self.base.clone())
272    }
273}
274
275impl<'a, K: Debug, V: Debug> Debug for Values<'a, K, V> {
276    #[inline]
277    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
278        f.debug_list().entries(self.clone()).finish()
279    }
280}
281
282impl<'a, K, V> Iterator for Values<'a, K, V> {
283    type Item = &'a V;
284
285    #[inline]
286    fn next(&mut self) -> Option<Self::Item> {
287        self.base.next().map(|e| &e.1)
288    }
289
290    #[inline]
291    fn size_hint(&self) -> (usize, Option<usize>) {
292        self.base.size_hint()
293    }
294
295    #[inline]
296    fn count(self) -> usize {
297        self.base.len()
298    }
299
300    #[inline]
301    fn nth(&mut self, n: usize) -> Option<Self::Item> {
302        self.base.nth(n).map(|e| &e.1)
303    }
304
305    #[inline]
306    fn last(self) -> Option<Self::Item> {
307        self.base.last().map(|e| &e.1)
308    }
309
310    #[inline]
311    fn fold<B, F>(self, init: B, mut f: F) -> B
312    where
313        F: FnMut(B, Self::Item) -> B,
314    {
315        self.base.fold(init, |b, e| f(b, &e.1))
316    }
317}
318
319impl<K, V> DoubleEndedIterator for Values<'_, K, V> {
320    #[inline]
321    fn next_back(&mut self) -> Option<Self::Item> {
322        self.base.next_back().map(|e| &e.1)
323    }
324
325    #[inline]
326    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
327        self.base.nth_back(n).map(|e| &e.1)
328    }
329}
330
331impl<K, V> ExactSizeIterator for Values<'_, K, V> {
332    #[inline]
333    fn len(&self) -> usize {
334        self.base.len()
335    }
336}
337
338impl<K, V> FusedIterator for Values<'_, K, V> {}
339
340#[must_use = "iterators are lazy and do nothing unless consumed"]
341#[derive(Default)]
342pub struct ValuesMut<'a, K, V> {
343    base: slice::IterMut<'a, (K, V)>,
344}
345
346impl<'a, K, V> ValuesMut<'a, K, V> {
347    #[inline]
348    pub(super) const fn new(base: slice::IterMut<'a, (K, V)>) -> Self {
349        Self { base }
350    }
351}
352
353impl<'a, K: Debug, V: Debug> Debug for ValuesMut<'a, K, V> {
354    #[inline]
355    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
356        f.debug_list()
357            .entries(Values::new(self.base.as_slice().iter()))
358            .finish()
359    }
360}
361
362impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
363    type Item = &'a mut V;
364
365    #[inline]
366    fn next(&mut self) -> Option<Self::Item> {
367        self.base.next().map(|e| &mut e.1)
368    }
369
370    #[inline]
371    fn size_hint(&self) -> (usize, Option<usize>) {
372        self.base.size_hint()
373    }
374
375    #[inline]
376    fn count(self) -> usize {
377        self.base.len()
378    }
379
380    #[inline]
381    fn nth(&mut self, n: usize) -> Option<Self::Item> {
382        self.base.nth(n).map(|e| &mut e.1)
383    }
384
385    #[inline]
386    fn last(self) -> Option<Self::Item> {
387        self.base.last().map(|e| &mut e.1)
388    }
389
390    #[inline]
391    fn fold<B, F>(self, init: B, mut f: F) -> B
392    where
393        F: FnMut(B, Self::Item) -> B,
394    {
395        self.base.fold(init, |b, e| f(b, &mut e.1))
396    }
397}
398
399impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> {
400    #[inline]
401    fn next_back(&mut self) -> Option<Self::Item> {
402        self.base.next_back().map(|e| &mut e.1)
403    }
404
405    #[inline]
406    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
407        self.base.nth_back(n).map(|e| &mut e.1)
408    }
409}
410
411impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
412    #[inline]
413    fn len(&self) -> usize {
414        self.base.len()
415    }
416}
417
418impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}