1use super::{Entries, Slot, VecMap};
2use alloc::vec::{self, Vec};
3use core::fmt;
4use core::iter::FusedIterator;
5use core::ops::RangeBounds;
6use core::slice;
7
8macro_rules! impl_iterator {
9 ($ty:ident<$($lt:lifetime,)*$($gen:ident),+>, $item:ty, $map:expr) => {
10 impl_iterator!($ty<$($lt,)*$($gen),+>, $item, $map, $map);
11 };
12 ($ty:ident<$($lt:lifetime,)*$($gen:ident),+>, $item:ty, $map:expr, $debug_map:expr) => {
13 impl<$($lt,)*$($gen),+> Iterator for $ty<$($lt,)*$($gen),+> {
14 type Item = $item;
15
16 fn next(&mut self) -> Option<Self::Item> {
17 self.iter.next().map($map)
18 }
19
20 fn size_hint(&self) -> (usize, Option<usize>) {
21 self.iter.size_hint()
22 }
23 }
24
25 impl<$($lt,)*$($gen),+> DoubleEndedIterator for $ty<$($lt,)*$($gen),+> {
26 fn next_back(&mut self) -> Option<Self::Item> {
27 self.iter.next_back().map($map)
28 }
29 }
30
31 impl<$($lt,)*$($gen),+> ExactSizeIterator for $ty<$($lt,)*$($gen),+> {
32 fn len(&self) -> usize {
33 self.iter.len()
34 }
35 }
36
37 impl<$($lt,)*$($gen),+> FusedIterator for $ty<$($lt,)*$($gen),+> {}
38
39 impl<$($lt,)*$($gen),+> fmt::Debug for $ty<$($lt,)*$($gen),+>
40 where
41 K: fmt::Debug,
42 V: fmt::Debug,
43 {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let iter = self.iter.as_slice().iter().map($debug_map);
46 f.debug_list().entries(iter).finish()
47 }
48 }
49 };
50}
51
52impl<K, V> IntoIterator for VecMap<K, V> {
53 type Item = (K, V);
54
55 type IntoIter = IntoIter<K, V>;
56
57 fn into_iter(self) -> Self::IntoIter {
58 IntoIter::new(self.into_entries())
59 }
60}
61
62impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
63 type Item = (&'a K, &'a V);
64
65 type IntoIter = Iter<'a, K, V>;
66
67 fn into_iter(self) -> Self::IntoIter {
68 self.iter()
69 }
70}
71
72impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V> {
73 type Item = (&'a K, &'a mut V);
74
75 type IntoIter = IterMut<'a, K, V>;
76
77 fn into_iter(self) -> Self::IntoIter {
78 self.iter_mut()
79 }
80}
81
82pub struct Iter<'a, K, V> {
88 iter: slice::Iter<'a, Slot<K, V>>,
89}
90
91impl<'a, K, V> Iter<'a, K, V> {
92 pub(super) fn new(entries: &'a [Slot<K, V>]) -> Iter<'a, K, V> {
93 Iter {
94 iter: entries.iter(),
95 }
96 }
97}
98
99impl<K, V> Clone for Iter<'_, K, V> {
100 fn clone(&self) -> Self {
101 Iter {
102 iter: self.iter.clone(),
103 }
104 }
105}
106
107impl_iterator!(Iter<'a, K, V>, (&'a K, &'a V), Slot::refs);
108
109pub struct IterMut<'a, K, V> {
116 iter: slice::IterMut<'a, Slot<K, V>>,
117}
118
119impl<'a, K, V> IterMut<'a, K, V> {
120 pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> IterMut<'a, K, V> {
121 IterMut {
122 iter: entries.iter_mut(),
123 }
124 }
125}
126
127impl_iterator!(
128 IterMut<'a, K, V>,
129 (&'a K, &'a mut V),
130 Slot::ref_mut,
131 Slot::refs
132);
133
134pub struct IterMut2<'a, K, V> {
142 iter: slice::IterMut<'a, Slot<K, V>>,
143}
144
145impl<'a, K, V> IterMut2<'a, K, V> {
146 pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> IterMut2<'a, K, V> {
147 IterMut2 {
148 iter: entries.iter_mut(),
149 }
150 }
151}
152
153impl_iterator!(
154 IterMut2<'a, K, V>,
155 (&'a mut K, &'a mut V),
156 Slot::muts,
157 Slot::refs
158);
159
160pub struct IntoIter<K, V> {
168 iter: vec::IntoIter<Slot<K, V>>,
169}
170
171impl<K, V> IntoIter<K, V> {
172 pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoIter<K, V> {
173 IntoIter {
174 iter: entries.into_iter(),
175 }
176 }
177}
178
179impl<K, V> Clone for IntoIter<K, V>
180where
181 K: Clone,
182 V: Clone,
183{
184 fn clone(&self) -> Self {
185 IntoIter {
186 iter: self.iter.clone(),
187 }
188 }
189}
190
191impl_iterator!(IntoIter<K, V>, (K, V), Slot::into_key_value, Slot::refs);
192
193pub struct Keys<'a, K, V> {
199 iter: slice::Iter<'a, Slot<K, V>>,
200}
201
202impl<'a, K, V> Keys<'a, K, V> {
203 pub(super) fn new(entries: &'a [Slot<K, V>]) -> Keys<'a, K, V> {
204 Keys {
205 iter: entries.iter(),
206 }
207 }
208}
209
210impl<K, V> Clone for Keys<'_, K, V> {
211 fn clone(&self) -> Self {
212 Keys {
213 iter: self.iter.clone(),
214 }
215 }
216}
217
218impl_iterator!(Keys<'a, K, V>, &'a K, Slot::key);
219
220pub struct KeysMut<'a, K, V> {
228 iter: slice::IterMut<'a, Slot<K, V>>,
229}
230
231impl<'a, K, V> KeysMut<'a, K, V> {
232 pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> KeysMut<'a, K, V> {
233 KeysMut {
234 iter: entries.iter_mut(),
235 }
236 }
237}
238
239impl_iterator!(KeysMut<'a, K, V>, &'a mut K, Slot::key_mut, Slot::key);
240
241pub struct IntoKeys<K, V> {
248 iter: vec::IntoIter<Slot<K, V>>,
249}
250
251impl<K, V> IntoKeys<K, V> {
252 pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoKeys<K, V> {
253 IntoKeys {
254 iter: entries.into_iter(),
255 }
256 }
257}
258
259impl<K, V> Clone for IntoKeys<K, V>
260where
261 K: Clone,
262 V: Clone,
263{
264 fn clone(&self) -> Self {
265 IntoKeys {
266 iter: self.iter.clone(),
267 }
268 }
269}
270
271impl_iterator!(IntoKeys<K, V>, K, Slot::into_key, Slot::key);
272
273pub struct Values<'a, K, V> {
280 iter: slice::Iter<'a, Slot<K, V>>,
281}
282
283impl<'a, K, V> Values<'a, K, V> {
284 pub(super) fn new(entries: &'a [Slot<K, V>]) -> Values<'a, K, V> {
285 Values {
286 iter: entries.iter(),
287 }
288 }
289}
290
291impl<K, V> Clone for Values<'_, K, V> {
292 fn clone(&self) -> Self {
293 Values {
294 iter: self.iter.clone(),
295 }
296 }
297}
298
299impl_iterator!(Values<'a, K, V>, &'a V, Slot::value);
300
301pub struct ValuesMut<'a, K, V> {
308 iter: slice::IterMut<'a, Slot<K, V>>,
309}
310
311impl<'a, K, V> ValuesMut<'a, K, V> {
312 pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> ValuesMut<'a, K, V> {
313 ValuesMut {
314 iter: entries.iter_mut(),
315 }
316 }
317}
318
319impl_iterator!(ValuesMut<'a, K, V>, &'a mut V, Slot::value_mut, Slot::value);
320
321pub struct IntoValues<K, V> {
328 iter: vec::IntoIter<Slot<K, V>>,
329}
330
331impl<K, V> IntoValues<K, V> {
332 pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoValues<K, V> {
333 IntoValues {
334 iter: entries.into_iter(),
335 }
336 }
337}
338
339impl<K, V> Clone for IntoValues<K, V>
340where
341 K: Clone,
342 V: Clone,
343{
344 fn clone(&self) -> Self {
345 IntoValues {
346 iter: self.iter.clone(),
347 }
348 }
349}
350
351impl_iterator!(IntoValues<K, V>, V, Slot::into_value, Slot::value);
352
353pub struct Drain<'a, K, V> {
360 iter: vec::Drain<'a, Slot<K, V>>,
361}
362
363impl<'a, K, V> Drain<'a, K, V> {
364 pub(super) fn new<R>(map: &'a mut VecMap<K, V>, range: R) -> Drain<'a, K, V>
365 where
366 R: RangeBounds<usize>,
367 {
368 Drain {
369 iter: map.base.drain(range),
370 }
371 }
372
373 pub(crate) fn as_slice(&self) -> &[Slot<K, V>] {
374 self.iter.as_slice()
375 }
376}
377
378impl_iterator!(Drain<'a, K, V>, (K, V), Slot::into_key_value, Slot::refs);