indexmap/map/
iter.rs

1use super::core::IndexMapCore;
2use super::{Bucket, Entries, IndexMap, Slice};
3
4use alloc::vec::{self, Vec};
5use core::fmt;
6use core::hash::{BuildHasher, Hash};
7use core::iter::FusedIterator;
8use core::ops::{Index, RangeBounds};
9use core::slice;
10
11impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
12    type Item = (&'a K, &'a V);
13    type IntoIter = Iter<'a, K, V>;
14
15    fn into_iter(self) -> Self::IntoIter {
16        self.iter()
17    }
18}
19
20impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S> {
21    type Item = (&'a K, &'a mut V);
22    type IntoIter = IterMut<'a, K, V>;
23
24    fn into_iter(self) -> Self::IntoIter {
25        self.iter_mut()
26    }
27}
28
29impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
30    type Item = (K, V);
31    type IntoIter = IntoIter<K, V>;
32
33    fn into_iter(self) -> Self::IntoIter {
34        IntoIter::new(self.into_entries())
35    }
36}
37
38/// An iterator over the entries of an [`IndexMap`].
39///
40/// This `struct` is created by the [`IndexMap::iter`] method.
41/// See its documentation for more.
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 an [`IndexMap`].
99///
100/// This `struct` is created by the [`IndexMap::iter_mut`] method.
101/// See its documentation for more.
102pub struct IterMut<'a, K, V> {
103    iter: slice::IterMut<'a, Bucket<K, V>>,
104}
105
106impl<'a, K, V> IterMut<'a, K, V> {
107    pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self {
108        Self {
109            iter: entries.iter_mut(),
110        }
111    }
112
113    /// Returns a slice of the remaining entries in the iterator.
114    pub fn as_slice(&self) -> &Slice<K, V> {
115        Slice::from_slice(self.iter.as_slice())
116    }
117
118    /// Returns a mutable slice of the remaining entries in the iterator.
119    ///
120    /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
121    pub fn into_slice(self) -> &'a mut Slice<K, V> {
122        Slice::from_mut_slice(self.iter.into_slice())
123    }
124}
125
126impl<'a, K, V> Iterator for IterMut<'a, K, V> {
127    type Item = (&'a K, &'a mut V);
128
129    iterator_methods!(Bucket::ref_mut);
130}
131
132impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
133    double_ended_iterator_methods!(Bucket::ref_mut);
134}
135
136impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
137    fn len(&self) -> usize {
138        self.iter.len()
139    }
140}
141
142impl<K, V> FusedIterator for IterMut<'_, K, V> {}
143
144impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
145    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146        let iter = self.iter.as_slice().iter().map(Bucket::refs);
147        f.debug_list().entries(iter).finish()
148    }
149}
150
151impl<K, V> Default for IterMut<'_, K, V> {
152    fn default() -> Self {
153        Self {
154            iter: [].iter_mut(),
155        }
156    }
157}
158
159/// A mutable iterator over the entries of an [`IndexMap`].
160///
161/// This `struct` is created by the [`MutableKeys::iter_mut2`][super::MutableKeys::iter_mut2] method.
162/// See its documentation for more.
163pub struct IterMut2<'a, K, V> {
164    iter: slice::IterMut<'a, Bucket<K, V>>,
165}
166
167impl<'a, K, V> IterMut2<'a, K, V> {
168    pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self {
169        Self {
170            iter: entries.iter_mut(),
171        }
172    }
173
174    /// Returns a slice of the remaining entries in the iterator.
175    pub fn as_slice(&self) -> &Slice<K, V> {
176        Slice::from_slice(self.iter.as_slice())
177    }
178
179    /// Returns a mutable slice of the remaining entries in the iterator.
180    ///
181    /// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
182    pub fn into_slice(self) -> &'a mut Slice<K, V> {
183        Slice::from_mut_slice(self.iter.into_slice())
184    }
185}
186
187impl<'a, K, V> Iterator for IterMut2<'a, K, V> {
188    type Item = (&'a mut K, &'a mut V);
189
190    iterator_methods!(Bucket::muts);
191}
192
193impl<K, V> DoubleEndedIterator for IterMut2<'_, K, V> {
194    double_ended_iterator_methods!(Bucket::muts);
195}
196
197impl<K, V> ExactSizeIterator for IterMut2<'_, K, V> {
198    fn len(&self) -> usize {
199        self.iter.len()
200    }
201}
202
203impl<K, V> FusedIterator for IterMut2<'_, K, V> {}
204
205impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut2<'_, K, V> {
206    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207        let iter = self.iter.as_slice().iter().map(Bucket::refs);
208        f.debug_list().entries(iter).finish()
209    }
210}
211
212impl<K, V> Default for IterMut2<'_, K, V> {
213    fn default() -> Self {
214        Self {
215            iter: [].iter_mut(),
216        }
217    }
218}
219
220/// An owning iterator over the entries of an [`IndexMap`].
221///
222/// This `struct` is created by the [`IndexMap::into_iter`] method
223/// (provided by the [`IntoIterator`] trait). See its documentation for more.
224pub struct IntoIter<K, V> {
225    iter: vec::IntoIter<Bucket<K, V>>,
226}
227
228impl<K, V> IntoIter<K, V> {
229    pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
230        Self {
231            iter: entries.into_iter(),
232        }
233    }
234
235    /// Returns a slice of the remaining entries in the iterator.
236    pub fn as_slice(&self) -> &Slice<K, V> {
237        Slice::from_slice(self.iter.as_slice())
238    }
239
240    /// Returns a mutable slice of the remaining entries in the iterator.
241    pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
242        Slice::from_mut_slice(self.iter.as_mut_slice())
243    }
244}
245
246impl<K, V> Iterator for IntoIter<K, V> {
247    type Item = (K, V);
248
249    iterator_methods!(Bucket::key_value);
250}
251
252impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
253    double_ended_iterator_methods!(Bucket::key_value);
254}
255
256impl<K, V> ExactSizeIterator for IntoIter<K, V> {
257    fn len(&self) -> usize {
258        self.iter.len()
259    }
260}
261
262impl<K, V> FusedIterator for IntoIter<K, V> {}
263
264impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
265    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266        let iter = self.iter.as_slice().iter().map(Bucket::refs);
267        f.debug_list().entries(iter).finish()
268    }
269}
270
271impl<K, V> Default for IntoIter<K, V> {
272    fn default() -> Self {
273        Self {
274            iter: Vec::new().into_iter(),
275        }
276    }
277}
278
279/// A draining iterator over the entries of an [`IndexMap`].
280///
281/// This `struct` is created by the [`IndexMap::drain`] method.
282/// See its documentation for more.
283pub struct Drain<'a, K, V> {
284    iter: vec::Drain<'a, Bucket<K, V>>,
285}
286
287impl<'a, K, V> Drain<'a, K, V> {
288    pub(super) fn new(iter: vec::Drain<'a, Bucket<K, V>>) -> Self {
289        Self { iter }
290    }
291
292    /// Returns a slice of the remaining entries in the iterator.
293    pub fn as_slice(&self) -> &Slice<K, V> {
294        Slice::from_slice(self.iter.as_slice())
295    }
296}
297
298impl<K, V> Iterator for Drain<'_, K, V> {
299    type Item = (K, V);
300
301    iterator_methods!(Bucket::key_value);
302}
303
304impl<K, V> DoubleEndedIterator for Drain<'_, K, V> {
305    double_ended_iterator_methods!(Bucket::key_value);
306}
307
308impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
309    fn len(&self) -> usize {
310        self.iter.len()
311    }
312}
313
314impl<K, V> FusedIterator for Drain<'_, K, V> {}
315
316impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Drain<'_, K, V> {
317    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
318        let iter = self.iter.as_slice().iter().map(Bucket::refs);
319        f.debug_list().entries(iter).finish()
320    }
321}
322
323/// An iterator over the keys of an [`IndexMap`].
324///
325/// This `struct` is created by the [`IndexMap::keys`] method.
326/// See its documentation for more.
327pub struct Keys<'a, K, V> {
328    iter: slice::Iter<'a, Bucket<K, V>>,
329}
330
331impl<'a, K, V> Keys<'a, K, V> {
332    pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self {
333        Self {
334            iter: entries.iter(),
335        }
336    }
337}
338
339impl<'a, K, V> Iterator for Keys<'a, K, V> {
340    type Item = &'a K;
341
342    iterator_methods!(Bucket::key_ref);
343}
344
345impl<K, V> DoubleEndedIterator for Keys<'_, K, V> {
346    double_ended_iterator_methods!(Bucket::key_ref);
347}
348
349impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
350    fn len(&self) -> usize {
351        self.iter.len()
352    }
353}
354
355impl<K, V> FusedIterator for Keys<'_, K, V> {}
356
357// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
358impl<K, V> Clone for Keys<'_, K, V> {
359    fn clone(&self) -> Self {
360        Keys {
361            iter: self.iter.clone(),
362        }
363    }
364}
365
366impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
367    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
368        f.debug_list().entries(self.clone()).finish()
369    }
370}
371
372impl<K, V> Default for Keys<'_, K, V> {
373    fn default() -> Self {
374        Self { iter: [].iter() }
375    }
376}
377
378/// Access [`IndexMap`] keys at indexed positions.
379///
380/// While [`Index<usize> for IndexMap`][values] accesses a map's values,
381/// indexing through [`IndexMap::keys`] offers an alternative to access a map's
382/// keys instead.
383///
384/// [values]: IndexMap#impl-Index<usize>-for-IndexMap<K,+V,+S>
385///
386/// Since `Keys` is also an iterator, consuming items from the iterator will
387/// offset the effective indexes. Similarly, if `Keys` is obtained from
388/// [`Slice::keys`], indexes will be interpreted relative to the position of
389/// that slice.
390///
391/// # Examples
392///
393/// ```
394/// use indexmap::IndexMap;
395///
396/// let mut map = IndexMap::new();
397/// for word in "Lorem ipsum dolor sit amet".split_whitespace() {
398///     map.insert(word.to_lowercase(), word.to_uppercase());
399/// }
400///
401/// assert_eq!(map[0], "LOREM");
402/// assert_eq!(map.keys()[0], "lorem");
403/// assert_eq!(map[1], "IPSUM");
404/// assert_eq!(map.keys()[1], "ipsum");
405///
406/// map.reverse();
407/// assert_eq!(map.keys()[0], "amet");
408/// assert_eq!(map.keys()[1], "sit");
409///
410/// map.sort_keys();
411/// assert_eq!(map.keys()[0], "amet");
412/// assert_eq!(map.keys()[1], "dolor");
413///
414/// // Advancing the iterator will offset the indexing
415/// let mut keys = map.keys();
416/// assert_eq!(keys[0], "amet");
417/// assert_eq!(keys.next().map(|s| &**s), Some("amet"));
418/// assert_eq!(keys[0], "dolor");
419/// assert_eq!(keys[1], "ipsum");
420///
421/// // Slices may have an offset as well
422/// let slice = &map[2..];
423/// assert_eq!(slice[0], "IPSUM");
424/// assert_eq!(slice.keys()[0], "ipsum");
425/// ```
426///
427/// ```should_panic
428/// use indexmap::IndexMap;
429///
430/// let mut map = IndexMap::new();
431/// map.insert("foo", 1);
432/// println!("{:?}", map.keys()[10]); // panics!
433/// ```
434impl<'a, K, V> Index<usize> for Keys<'a, K, V> {
435    type Output = K;
436
437    /// Returns a reference to the key at the supplied `index`.
438    ///
439    /// ***Panics*** if `index` is out of bounds.
440    fn index(&self, index: usize) -> &K {
441        &self.iter.as_slice()[index].key
442    }
443}
444
445/// An owning iterator over the keys of an [`IndexMap`].
446///
447/// This `struct` is created by the [`IndexMap::into_keys`] method.
448/// See its documentation for more.
449pub struct IntoKeys<K, V> {
450    iter: vec::IntoIter<Bucket<K, V>>,
451}
452
453impl<K, V> IntoKeys<K, V> {
454    pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
455        Self {
456            iter: entries.into_iter(),
457        }
458    }
459}
460
461impl<K, V> Iterator for IntoKeys<K, V> {
462    type Item = K;
463
464    iterator_methods!(Bucket::key);
465}
466
467impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
468    double_ended_iterator_methods!(Bucket::key);
469}
470
471impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
472    fn len(&self) -> usize {
473        self.iter.len()
474    }
475}
476
477impl<K, V> FusedIterator for IntoKeys<K, V> {}
478
479impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
480    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481        let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
482        f.debug_list().entries(iter).finish()
483    }
484}
485
486impl<K, V> Default for IntoKeys<K, V> {
487    fn default() -> Self {
488        Self {
489            iter: Vec::new().into_iter(),
490        }
491    }
492}
493
494/// An iterator over the values of an [`IndexMap`].
495///
496/// This `struct` is created by the [`IndexMap::values`] method.
497/// See its documentation for more.
498pub struct Values<'a, K, V> {
499    iter: slice::Iter<'a, Bucket<K, V>>,
500}
501
502impl<'a, K, V> Values<'a, K, V> {
503    pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self {
504        Self {
505            iter: entries.iter(),
506        }
507    }
508}
509
510impl<'a, K, V> Iterator for Values<'a, K, V> {
511    type Item = &'a V;
512
513    iterator_methods!(Bucket::value_ref);
514}
515
516impl<K, V> DoubleEndedIterator for Values<'_, K, V> {
517    double_ended_iterator_methods!(Bucket::value_ref);
518}
519
520impl<K, V> ExactSizeIterator for Values<'_, K, V> {
521    fn len(&self) -> usize {
522        self.iter.len()
523    }
524}
525
526impl<K, V> FusedIterator for Values<'_, K, V> {}
527
528// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
529impl<K, V> Clone for Values<'_, K, V> {
530    fn clone(&self) -> Self {
531        Values {
532            iter: self.iter.clone(),
533        }
534    }
535}
536
537impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
538    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
539        f.debug_list().entries(self.clone()).finish()
540    }
541}
542
543impl<K, V> Default for Values<'_, K, V> {
544    fn default() -> Self {
545        Self { iter: [].iter() }
546    }
547}
548
549/// A mutable iterator over the values of an [`IndexMap`].
550///
551/// This `struct` is created by the [`IndexMap::values_mut`] method.
552/// See its documentation for more.
553pub struct ValuesMut<'a, K, V> {
554    iter: slice::IterMut<'a, Bucket<K, V>>,
555}
556
557impl<'a, K, V> ValuesMut<'a, K, V> {
558    pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self {
559        Self {
560            iter: entries.iter_mut(),
561        }
562    }
563}
564
565impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
566    type Item = &'a mut V;
567
568    iterator_methods!(Bucket::value_mut);
569}
570
571impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> {
572    double_ended_iterator_methods!(Bucket::value_mut);
573}
574
575impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
576    fn len(&self) -> usize {
577        self.iter.len()
578    }
579}
580
581impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
582
583impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
584    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
585        let iter = self.iter.as_slice().iter().map(Bucket::value_ref);
586        f.debug_list().entries(iter).finish()
587    }
588}
589
590impl<K, V> Default for ValuesMut<'_, K, V> {
591    fn default() -> Self {
592        Self {
593            iter: [].iter_mut(),
594        }
595    }
596}
597
598/// An owning iterator over the values of an [`IndexMap`].
599///
600/// This `struct` is created by the [`IndexMap::into_values`] method.
601/// See its documentation for more.
602pub struct IntoValues<K, V> {
603    iter: vec::IntoIter<Bucket<K, V>>,
604}
605
606impl<K, V> IntoValues<K, V> {
607    pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self {
608        Self {
609            iter: entries.into_iter(),
610        }
611    }
612}
613
614impl<K, V> Iterator for IntoValues<K, V> {
615    type Item = V;
616
617    iterator_methods!(Bucket::value);
618}
619
620impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
621    double_ended_iterator_methods!(Bucket::value);
622}
623
624impl<K, V> ExactSizeIterator for IntoValues<K, V> {
625    fn len(&self) -> usize {
626        self.iter.len()
627    }
628}
629
630impl<K, V> FusedIterator for IntoValues<K, V> {}
631
632impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
633    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
634        let iter = self.iter.as_slice().iter().map(Bucket::value_ref);
635        f.debug_list().entries(iter).finish()
636    }
637}
638
639impl<K, V> Default for IntoValues<K, V> {
640    fn default() -> Self {
641        Self {
642            iter: Vec::new().into_iter(),
643        }
644    }
645}
646
647/// A splicing iterator for `IndexMap`.
648///
649/// This `struct` is created by [`IndexMap::splice()`].
650/// See its documentation for more.
651pub struct Splice<'a, I, K, V, S>
652where
653    I: Iterator<Item = (K, V)>,
654    K: Hash + Eq,
655    S: BuildHasher,
656{
657    map: &'a mut IndexMap<K, V, S>,
658    tail: IndexMapCore<K, V>,
659    drain: vec::IntoIter<Bucket<K, V>>,
660    replace_with: I,
661}
662
663impl<'a, I, K, V, S> Splice<'a, I, K, V, S>
664where
665    I: Iterator<Item = (K, V)>,
666    K: Hash + Eq,
667    S: BuildHasher,
668{
669    pub(super) fn new<R>(map: &'a mut IndexMap<K, V, S>, range: R, replace_with: I) -> Self
670    where
671        R: RangeBounds<usize>,
672    {
673        let (tail, drain) = map.core.split_splice(range);
674        Self {
675            map,
676            tail,
677            drain,
678            replace_with,
679        }
680    }
681}
682
683impl<I, K, V, S> Drop for Splice<'_, I, K, V, S>
684where
685    I: Iterator<Item = (K, V)>,
686    K: Hash + Eq,
687    S: BuildHasher,
688{
689    fn drop(&mut self) {
690        // Finish draining unconsumed items. We don't strictly *have* to do this
691        // manually, since we already split it into separate memory, but it will
692        // match the drop order of `vec::Splice` items this way.
693        let _ = self.drain.nth(usize::MAX);
694
695        // Now insert all the new items. If a key matches an existing entry, it
696        // keeps the original position and only replaces the value, like `insert`.
697        while let Some((key, value)) = self.replace_with.next() {
698            // Since the tail is disjoint, we can try to update it first,
699            // or else insert (update or append) the primary map.
700            let hash = self.map.hash(&key);
701            if let Some(i) = self.tail.get_index_of(hash, &key) {
702                self.tail.as_entries_mut()[i].value = value;
703            } else {
704                self.map.core.insert_full(hash, key, value);
705            }
706        }
707
708        // Finally, re-append the tail
709        self.map.core.append_unchecked(&mut self.tail);
710    }
711}
712
713impl<I, K, V, S> Iterator for Splice<'_, I, K, V, S>
714where
715    I: Iterator<Item = (K, V)>,
716    K: Hash + Eq,
717    S: BuildHasher,
718{
719    type Item = (K, V);
720
721    fn next(&mut self) -> Option<Self::Item> {
722        self.drain.next().map(Bucket::key_value)
723    }
724
725    fn size_hint(&self) -> (usize, Option<usize>) {
726        self.drain.size_hint()
727    }
728}
729
730impl<I, K, V, S> DoubleEndedIterator for Splice<'_, I, K, V, S>
731where
732    I: Iterator<Item = (K, V)>,
733    K: Hash + Eq,
734    S: BuildHasher,
735{
736    fn next_back(&mut self) -> Option<Self::Item> {
737        self.drain.next_back().map(Bucket::key_value)
738    }
739}
740
741impl<I, K, V, S> ExactSizeIterator for Splice<'_, I, K, V, S>
742where
743    I: Iterator<Item = (K, V)>,
744    K: Hash + Eq,
745    S: BuildHasher,
746{
747    fn len(&self) -> usize {
748        self.drain.len()
749    }
750}
751
752impl<I, K, V, S> FusedIterator for Splice<'_, I, K, V, S>
753where
754    I: Iterator<Item = (K, V)>,
755    K: Hash + Eq,
756    S: BuildHasher,
757{
758}
759
760impl<'a, I, K, V, S> fmt::Debug for Splice<'a, I, K, V, S>
761where
762    I: fmt::Debug + Iterator<Item = (K, V)>,
763    K: fmt::Debug + Hash + Eq,
764    V: fmt::Debug,
765    S: BuildHasher,
766{
767    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
768        // Follow `vec::Splice` in only printing the drain and replacement
769        f.debug_struct("Splice")
770            .field("drain", &self.drain)
771            .field("replace_with", &self.replace_with)
772            .finish()
773    }
774}