1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use super::{Entries, Slot, VecMap};
use alloc::vec::{self, Vec};
use core::fmt;
use core::iter::FusedIterator;
use core::ops::RangeBounds;
use core::slice;

macro_rules! impl_iterator {
    ($ty:ident<$($lt:lifetime,)*$($gen:ident),+>, $item:ty, $map:expr) => {
        impl<$($lt,)*$($gen),+> Iterator for $ty<$($lt,)*$($gen),+> {
            type Item = $item;

            fn next(&mut self) -> Option<Self::Item> {
                self.iter.next().map($map)
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                self.iter.size_hint()
            }
        }

        impl<$($lt,)*$($gen),+> DoubleEndedIterator for $ty<$($lt,)*$($gen),+> {
            fn next_back(&mut self) -> Option<Self::Item> {
                self.iter.next_back().map($map)
            }
        }

        impl<$($lt,)*$($gen),+> ExactSizeIterator for $ty<$($lt,)*$($gen),+> {
            fn len(&self) -> usize {
                self.iter.len()
            }
        }

        impl<$($lt,)*$($gen),+> FusedIterator for $ty<$($lt,)*$($gen),+> {}

        impl<$($lt,)*$($gen),+> fmt::Debug for $ty<$($lt,)*$($gen),+>
        where
            K: fmt::Debug,
            V: fmt::Debug,
        {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let iter = self.iter.as_slice().iter().map(Slot::refs);
                f.debug_list().entries(iter).finish()
            }
        }
    };
}

impl<K, V> IntoIterator for VecMap<K, V> {
    type Item = (K, V);

    type IntoIter = IntoIter<K, V>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self.into_entries())
    }
}

impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
    type Item = (&'a K, &'a V);

    type IntoIter = Iter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V> {
    type Item = (&'a K, &'a mut V);

    type IntoIter = IterMut<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// An iterator over the entries of a `VecMap`.
///
/// This `struct` is created by the [`iter`] method on [`VecMap`]. See its documentation for more.
///
/// [`iter`]: VecMap::iter
pub struct Iter<'a, K, V> {
    iter: slice::Iter<'a, Slot<K, V>>,
}

impl<'a, K, V> Iter<'a, K, V> {
    pub(super) fn new(entries: &'a [Slot<K, V>]) -> Iter<'a, K, V> {
        Iter {
            iter: entries.iter(),
        }
    }
}

impl<K, V> Clone for Iter<'_, K, V> {
    fn clone(&self) -> Self {
        Iter {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(Iter<'a, K, V>, (&'a K, &'a V), Slot::refs);

/// A mutable iterator over the entries of a `VecMap`.
///
/// This `struct` is created by the [`iter_mut`] method on [`VecMap`]. See its documentation for
/// more.
///
/// [`iter_mut`]: VecMap::iter_mut
pub struct IterMut<'a, K, V> {
    iter: slice::IterMut<'a, Slot<K, V>>,
}

impl<'a, K, V> IterMut<'a, K, V> {
    pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> IterMut<'a, K, V> {
        IterMut {
            iter: entries.iter_mut(),
        }
    }
}

impl_iterator!(IterMut<'a, K, V>, (&'a K, &'a mut V), Slot::ref_mut);

/// A mutable iterator over the entries of a `VecMap`.
///
/// This `struct` is created by the [`iter_mut2`] method on [`MutableKeys`]. See its documentation
/// for more.
///
/// [`iter_mut2`]: crate::map::MutableKeys::iter_mut2
/// [`MutableKeys`]: crate::map::MutableKeys
pub struct IterMut2<'a, K, V> {
    iter: slice::IterMut<'a, Slot<K, V>>,
}

impl<'a, K, V> IterMut2<'a, K, V> {
    pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> IterMut2<'a, K, V> {
        IterMut2 {
            iter: entries.iter_mut(),
        }
    }
}

impl_iterator!(IterMut2<'a, K, V>, (&'a mut K, &'a mut V), Slot::muts);

/// An owning iterator over the entries of a `VecMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`VecMap`] (provided by the
/// [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: IntoIterator::into_iter
/// [`IntoIterator`]: core::iter::IntoIterator
pub struct IntoIter<K, V> {
    iter: vec::IntoIter<Slot<K, V>>,
}

impl<K, V> IntoIter<K, V> {
    pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoIter<K, V> {
        IntoIter {
            iter: entries.into_iter(),
        }
    }
}

impl<K, V> Clone for IntoIter<K, V>
where
    K: Clone,
    V: Clone,
{
    fn clone(&self) -> Self {
        IntoIter {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(IntoIter<K, V>, (K, V), Slot::into_key_value);

/// An iterator over the keys of a `VecMap`.
///
/// This `struct` is created by the [`keys`] method on [`VecMap`]. See its documentation for more.
///
/// [`keys`]: VecMap::keys
pub struct Keys<'a, K, V> {
    iter: slice::Iter<'a, Slot<K, V>>,
}

impl<'a, K, V> Keys<'a, K, V> {
    pub(super) fn new(entries: &'a [Slot<K, V>]) -> Keys<'a, K, V> {
        Keys {
            iter: entries.iter(),
        }
    }
}

impl<K, V> Clone for Keys<'_, K, V> {
    fn clone(&self) -> Self {
        Keys {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(Keys<'a, K, V>, &'a K, Slot::key);

/// A mutable iterator over the keys of a `VecMap`.
///
/// This `struct` is created by the [`keys_mut`] method on [`MutableKeys`]. See its documentation
/// for more.
///
/// [`keys_mut`]: crate::map::MutableKeys::keys_mut
/// [`MutableKeys`]: crate::map::MutableKeys
pub struct KeysMut<'a, K, V> {
    iter: slice::IterMut<'a, Slot<K, V>>,
}

impl<'a, K, V> KeysMut<'a, K, V> {
    pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> KeysMut<'a, K, V> {
        KeysMut {
            iter: entries.iter_mut(),
        }
    }
}

impl_iterator!(KeysMut<'a, K, V>, &'a mut K, Slot::key_mut);

/// An owning iterator over the keys of a `VecMap`.
///
/// This `struct` is created by the [`into_keys`] method on [`VecMap`]. See its documentation for
/// more.
///
/// [`into_keys`]: VecMap::into_keys
pub struct IntoKeys<K, V> {
    iter: vec::IntoIter<Slot<K, V>>,
}

impl<K, V> IntoKeys<K, V> {
    pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoKeys<K, V> {
        IntoKeys {
            iter: entries.into_iter(),
        }
    }
}

impl<K, V> Clone for IntoKeys<K, V>
where
    K: Clone,
    V: Clone,
{
    fn clone(&self) -> Self {
        IntoKeys {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(IntoKeys<K, V>, K, Slot::into_key);

/// An iterator over the values of a `VecMap`.
///
/// This `struct` is created by the [`values`] method on [`VecMap`]. See its documentation for
/// more.
///
/// [`values`]: VecMap::values
pub struct Values<'a, K, V> {
    iter: slice::Iter<'a, Slot<K, V>>,
}

impl<'a, K, V> Values<'a, K, V> {
    pub(super) fn new(entries: &'a [Slot<K, V>]) -> Values<'a, K, V> {
        Values {
            iter: entries.iter(),
        }
    }
}

impl<K, V> Clone for Values<'_, K, V> {
    fn clone(&self) -> Self {
        Values {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(Values<'a, K, V>, &'a V, Slot::value);

/// A mutable iterator over the values of a `VecMap`.
///
/// This `struct` is created by the [`values_mut`] method on [`VecMap`]. See its documentation for
/// more.
///
/// [`values_mut`]: VecMap::values_mut
pub struct ValuesMut<'a, K, V> {
    iter: slice::IterMut<'a, Slot<K, V>>,
}

impl<'a, K, V> ValuesMut<'a, K, V> {
    pub(super) fn new(entries: &'a mut [Slot<K, V>]) -> ValuesMut<'a, K, V> {
        ValuesMut {
            iter: entries.iter_mut(),
        }
    }
}

impl_iterator!(ValuesMut<'a, K, V>, &'a mut V, Slot::value_mut);

/// An owning iterator over the values of a `VecMap`.
///
/// This `struct` is created by the [`into_values`] method on [`VecMap`]. See its documentation
/// for more.
///
/// [`into_values`]: VecMap::into_values
pub struct IntoValues<K, V> {
    iter: vec::IntoIter<Slot<K, V>>,
}

impl<K, V> IntoValues<K, V> {
    pub(super) fn new(entries: Vec<Slot<K, V>>) -> IntoValues<K, V> {
        IntoValues {
            iter: entries.into_iter(),
        }
    }
}

impl<K, V> Clone for IntoValues<K, V>
where
    K: Clone,
    V: Clone,
{
    fn clone(&self) -> Self {
        IntoValues {
            iter: self.iter.clone(),
        }
    }
}

impl_iterator!(IntoValues<K, V>, V, Slot::into_value);

/// A draining iterator for `VecMap`.
///
/// This `struct` is created by the [`drain`] method on [`VecMap`]. See its documentation for
/// more.
///
/// [`drain`]: VecMap::drain
pub struct Drain<'a, K, V> {
    iter: vec::Drain<'a, Slot<K, V>>,
}

impl<'a, K, V> Drain<'a, K, V> {
    pub(super) fn new<R>(map: &'a mut VecMap<K, V>, range: R) -> Drain<'a, K, V>
    where
        R: RangeBounds<usize>,
    {
        Drain {
            iter: map.base.drain(range),
        }
    }

    pub(crate) fn as_slice(&self) -> &[Slot<K, V>] {
        self.iter.as_slice()
    }
}

impl_iterator!(Drain<'a, K, V>, (K, V), Slot::into_key_value);