1use super::{Bucket, Entries, IndexMap, Slice};
2
3use alloc::vec::{self, Vec};
4use core::fmt;
5use core::iter::FusedIterator;
6use core::slice;
7
8impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
9 type Item = (&'a K, &'a V);
10 type IntoIter = Iter<'a, K, V>;
11
12 fn into_iter(self) -> Self::IntoIter {
13 self.iter()
14 }
15}
16
17impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S> {
18 type Item = (&'a K, &'a mut V);
19 type IntoIter = IterMut<'a, K, V>;
20
21 fn into_iter(self) -> Self::IntoIter {
22 self.iter_mut()
23 }
24}
25
26impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
27 type Item = (K, V);
28 type IntoIter = IntoIter<K, V>;
29
30 fn into_iter(self) -> Self::IntoIter {
31 IntoIter::new(self.into_entries())
32 }
33}
34
35/// An iterator over the entries of a `IndexMap`.
36///
37/// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its
38/// documentation for more.
39///
40/// [`iter`]: struct.IndexMap.html#method.iter
41/// [`IndexMap`]: struct.IndexMap.html
42pub struct Iter<'a, K, V> {
43 iter: slice::Iter<'a, Bucket<K, V>>,
44}
45
46impl<'a, K, V> Iter<'a, K, V> {
47 pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self {
48 Self {
49 iter: entries.iter(),
50 }
51 }
52
53 /// Returns a slice of the remaining entries in the iterator.
54 pub fn as_slice(&self) -> &'a Slice<K, V> {
55 Slice::from_slice(self.iter.as_slice())
56 }
57}
58
59impl<'a, K, V> Iterator for Iter<'a, K, V> {
60 type Item = (&'a K, &'a V);
61
62 iterator_methods!(Bucket::refs);
63}
64
65impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
66 double_ended_iterator_methods!(Bucket::refs);
67}
68
69impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
70 fn len(&self) -> usize {
71 self.iter.len()
72 }
73}
74
75impl<K, V> FusedIterator for Iter<'_, K, V> {}
76
77// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
78impl<K, V> Clone for Iter<'_, K, V> {
79 fn clone(&self) -> Self {
80 Iter {
81 iter: self.iter.clone(),
82 }
83 }
84}
85
86impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 f.debug_list().entries(self.clone()).finish()
89 }
90}
91
92impl<K, V> Default for Iter<'_, K, V> {
93 fn default() -> Self {
94 Self { iter: [].iter() }
95 }
96}
97
98/// A mutable iterator over the entries of a `IndexMap`.
99///
100/// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its
101/// documentation for more.
102///
103/// [`iter_mut`]: struct.IndexMap.html#method.iter_mut
104/// [`IndexMap`]: struct.IndexMap.html
105pub struct IterMut<'a, K, V> {
106 iter: slice::IterMut<'a, Bucket<K, V>>,
107}
108
109impl<'a, K, V> IterMut<'a, K, V> {
110 pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self {
111 Self {
112 iter: entries.iter_mut(),
113 }
114 }
115
116 /// Returns a slice of the remaining entries in the iterator.
117 pub fn as_slice(&self) -> &Slice<K, V> {
118 Slice::from_slice(self.iter.as_slice())
119 }
120
121 /// Returns a mutable slice of the remaining entries in the iterator.
122 ///
123 /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
124 pub fn into_slice(self) -> &'a mut Slice<K, V> {
125 Slice::from_mut_slice(self.iter.into_slice())
126 }
127}
128
129impl<'a, K, V> Iterator for IterMut<'a, K, V> {
130 type Item = (&'a K, &'a mut V);
131
132 iterator_methods!(Bucket::ref_mut);
133}
134
135impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
136 double_ended_iterator_methods!(Bucket::ref_mut);
137}
138
139impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
140 fn len(&self) -> usize {
141 self.iter.len()
142 }
143}
144
145impl<K, V> FusedIterator for IterMut<'_, K, V> {}
146
147impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs);
150 f.debug_list().entries(iter).finish()
151 }
152}
153
154impl<K, V> Default for IterMut<'_, K, V> {
155 fn default() -> Self {
156 Self {
157 iter: [].iter_mut(),
158 }
159 }
160}
161
162/// An owning iterator over the entries of a `IndexMap`.
163///
164/// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
165/// (provided by the `IntoIterator` trait). See its documentation for more.
166///
167/// [`into_iter`]: struct.IndexMap.html#method.into_iter
168/// [`IndexMap`]: struct.IndexMap.html
169pub struct IntoIter<K, V> {
170 iter: vec::IntoIter<Bucket<K, V>>,
171}
172
173impl<K, V> IntoIter<K, V> {
174 pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
175 Self {
176 iter: entries.into_iter(),
177 }
178 }
179
180 /// Returns a slice of the remaining entries in the iterator.
181 pub fn as_slice(&self) -> &Slice<K, V> {
182 Slice::from_slice(self.iter.as_slice())
183 }
184
185 /// Returns a mutable slice of the remaining entries in the iterator.
186 pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
187 Slice::from_mut_slice(self.iter.as_mut_slice())
188 }
189}
190
191impl<K, V> Iterator for IntoIter<K, V> {
192 type Item = (K, V);
193
194 iterator_methods!(Bucket::key_value);
195}
196
197impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
198 double_ended_iterator_methods!(Bucket::key_value);
199}
200
201impl<K, V> ExactSizeIterator for IntoIter<K, V> {
202 fn len(&self) -> usize {
203 self.iter.len()
204 }
205}
206
207impl<K, V> FusedIterator for IntoIter<K, V> {}
208
209impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs);
212 f.debug_list().entries(iter).finish()
213 }
214}
215
216impl<K, V> Default for IntoIter<K, V> {
217 fn default() -> Self {
218 Self {
219 iter: Vec::new().into_iter(),
220 }
221 }
222}
223
224/// A draining iterator over the entries of a `IndexMap`.
225///
226/// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its
227/// documentation for more.
228///
229/// [`drain`]: struct.IndexMap.html#method.drain
230/// [`IndexMap`]: struct.IndexMap.html
231pub struct Drain<'a, K, V> {
232 iter: vec::Drain<'a, Bucket<K, V>>,
233}
234
235impl<'a, K, V> Drain<'a, K, V> {
236 pub(super) fn new(iter: vec::Drain<'a, Bucket<K, V>>) -> Self {
237 Self { iter }
238 }
239
240 /// Returns a slice of the remaining entries in the iterator.
241 pub fn as_slice(&self) -> &Slice<K, V> {
242 Slice::from_slice(self.iter.as_slice())
243 }
244}
245
246impl<K, V> Iterator for Drain<'_, K, V> {
247 type Item = (K, V);
248
249 iterator_methods!(Bucket::key_value);
250}
251
252impl<K, V> DoubleEndedIterator for Drain<'_, K, V> {
253 double_ended_iterator_methods!(Bucket::key_value);
254}
255
256impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
257 fn len(&self) -> usize {
258 self.iter.len()
259 }
260}
261
262impl<K, V> FusedIterator for Drain<'_, K, V> {}
263
264impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Drain<'_, K, V> {
265 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs);
267 f.debug_list().entries(iter).finish()
268 }
269}
270
271/// An iterator over the keys of a `IndexMap`.
272///
273/// This `struct` is created by the [`keys`] method on [`IndexMap`]. See its
274/// documentation for more.
275///
276/// [`keys`]: struct.IndexMap.html#method.keys
277/// [`IndexMap`]: struct.IndexMap.html
278pub struct Keys<'a, K, V> {
279 iter: slice::Iter<'a, Bucket<K, V>>,
280}
281
282impl<'a, K, V> Keys<'a, K, V> {
283 pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self {
284 Self {
285 iter: entries.iter(),
286 }
287 }
288}
289
290impl<'a, K, V> Iterator for Keys<'a, K, V> {
291 type Item = &'a K;
292
293 iterator_methods!(Bucket::key_ref);
294}
295
296impl<K, V> DoubleEndedIterator for Keys<'_, K, V> {
297 double_ended_iterator_methods!(Bucket::key_ref);
298}
299
300impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
301 fn len(&self) -> usize {
302 self.iter.len()
303 }
304}
305
306impl<K, V> FusedIterator for Keys<'_, K, V> {}
307
308// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
309impl<K, V> Clone for Keys<'_, K, V> {
310 fn clone(&self) -> Self {
311 Keys {
312 iter: self.iter.clone(),
313 }
314 }
315}
316
317impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
318 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319 f.debug_list().entries(self.clone()).finish()
320 }
321}
322
323impl<K, V> Default for Keys<'_, K, V> {
324 fn default() -> Self {
325 Self { iter: [].iter() }
326 }
327}
328
329/// An owning iterator over the keys of a `IndexMap`.
330///
331/// This `struct` is created by the [`into_keys`] method on [`IndexMap`].
332/// See its documentation for more.
333///
334/// [`IndexMap`]: struct.IndexMap.html
335/// [`into_keys`]: struct.IndexMap.html#method.into_keys
336pub struct IntoKeys<K, V> {
337 iter: vec::IntoIter<Bucket<K, V>>,
338}
339
340impl<K, V> IntoKeys<K, V> {
341 pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
342 Self {
343 iter: entries.into_iter(),
344 }
345 }
346}
347
348impl<K, V> Iterator for IntoKeys<K, V> {
349 type Item = K;
350
351 iterator_methods!(Bucket::key);
352}
353
354impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
355 double_ended_iterator_methods!(Bucket::key);
356}
357
358impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
359 fn len(&self) -> usize {
360 self.iter.len()
361 }
362}
363
364impl<K, V> FusedIterator for IntoKeys<K, V> {}
365
366impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
367 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
368 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::key_ref);
369 f.debug_list().entries(iter).finish()
370 }
371}
372
373impl<K, V> Default for IntoKeys<K, V> {
374 fn default() -> Self {
375 Self {
376 iter: Vec::new().into_iter(),
377 }
378 }
379}
380
381/// An iterator over the values of a `IndexMap`.
382///
383/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
384/// documentation for more.
385///
386/// [`values`]: struct.IndexMap.html#method.values
387/// [`IndexMap`]: struct.IndexMap.html
388pub struct Values<'a, K, V> {
389 iter: slice::Iter<'a, Bucket<K, V>>,
390}
391
392impl<'a, K, V> Values<'a, K, V> {
393 pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self {
394 Self {
395 iter: entries.iter(),
396 }
397 }
398}
399
400impl<'a, K, V> Iterator for Values<'a, K, V> {
401 type Item = &'a V;
402
403 iterator_methods!(Bucket::value_ref);
404}
405
406impl<K, V> DoubleEndedIterator for Values<'_, K, V> {
407 double_ended_iterator_methods!(Bucket::value_ref);
408}
409
410impl<K, V> ExactSizeIterator for Values<'_, K, V> {
411 fn len(&self) -> usize {
412 self.iter.len()
413 }
414}
415
416impl<K, V> FusedIterator for Values<'_, K, V> {}
417
418// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
419impl<K, V> Clone for Values<'_, K, V> {
420 fn clone(&self) -> Self {
421 Values {
422 iter: self.iter.clone(),
423 }
424 }
425}
426
427impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
428 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
429 f.debug_list().entries(self.clone()).finish()
430 }
431}
432
433impl<K, V> Default for Values<'_, K, V> {
434 fn default() -> Self {
435 Self { iter: [].iter() }
436 }
437}
438
439/// A mutable iterator over the values of a `IndexMap`.
440///
441/// This `struct` is created by the [`values_mut`] method on [`IndexMap`]. See its
442/// documentation for more.
443///
444/// [`values_mut`]: struct.IndexMap.html#method.values_mut
445/// [`IndexMap`]: struct.IndexMap.html
446pub struct ValuesMut<'a, K, V> {
447 iter: slice::IterMut<'a, Bucket<K, V>>,
448}
449
450impl<'a, K, V> ValuesMut<'a, K, V> {
451 pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self {
452 Self {
453 iter: entries.iter_mut(),
454 }
455 }
456}
457
458impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
459 type Item = &'a mut V;
460
461 iterator_methods!(Bucket::value_mut);
462}
463
464impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> {
465 double_ended_iterator_methods!(Bucket::value_mut);
466}
467
468impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
469 fn len(&self) -> usize {
470 self.iter.len()
471 }
472}
473
474impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
475
476impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
477 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
478 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::value_ref);
479 f.debug_list().entries(iter).finish()
480 }
481}
482
483impl<K, V> Default for ValuesMut<'_, K, V> {
484 fn default() -> Self {
485 Self {
486 iter: [].iter_mut(),
487 }
488 }
489}
490
491/// An owning iterator over the values of a `IndexMap`.
492///
493/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
494/// See its documentation for more.
495///
496/// [`IndexMap`]: struct.IndexMap.html
497/// [`into_values`]: struct.IndexMap.html#method.into_values
498pub struct IntoValues<K, V> {
499 iter: vec::IntoIter<Bucket<K, V>>,
500}
501
502impl<K, V> IntoValues<K, V> {
503 pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
504 Self {
505 iter: entries.into_iter(),
506 }
507 }
508}
509
510impl<K, V> Iterator for IntoValues<K, V> {
511 type Item = V;
512
513 iterator_methods!(Bucket::value);
514}
515
516impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
517 double_ended_iterator_methods!(Bucket::value);
518}
519
520impl<K, V> ExactSizeIterator for IntoValues<K, V> {
521 fn len(&self) -> usize {
522 self.iter.len()
523 }
524}
525
526impl<K, V> FusedIterator for IntoValues<K, V> {}
527
528impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
529 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
530 let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::value_ref);
531 f.debug_list().entries(iter).finish()
532 }
533}
534
535impl<K, V> Default for IntoValues<K, V> {
536 fn default() -> Self {
537 Self {
538 iter: Vec::new().into_iter(),
539 }
540 }
541}
542