Coverage Report

Created: 2025-09-04 06:37

/rust/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.5.0/src/set/iter.rs
Line
Count
Source (jump to first uncovered line)
1
use super::{Bucket, Entries, IndexSet, Slice};
2
3
use alloc::vec::{self, Vec};
4
use core::fmt;
5
use core::hash::{BuildHasher, Hash};
6
use core::iter::{Chain, FusedIterator};
7
use core::ops::RangeBounds;
8
use core::slice::Iter as SliceIter;
9
10
impl<'a, T, S> IntoIterator for &'a IndexSet<T, S> {
11
    type Item = &'a T;
12
    type IntoIter = Iter<'a, T>;
13
14
0
    fn into_iter(self) -> Self::IntoIter {
15
0
        self.iter()
16
0
    }
17
}
18
19
impl<T, S> IntoIterator for IndexSet<T, S> {
20
    type Item = T;
21
    type IntoIter = IntoIter<T>;
22
23
0
    fn into_iter(self) -> Self::IntoIter {
24
0
        IntoIter::new(self.into_entries())
25
0
    }
26
}
27
28
/// An iterator over the items of an [`IndexSet`].
29
///
30
/// This `struct` is created by the [`IndexSet::iter`] method.
31
/// See its documentation for more.
32
pub struct Iter<'a, T> {
33
    iter: SliceIter<'a, Bucket<T>>,
34
}
35
36
impl<'a, T> Iter<'a, T> {
37
0
    pub(super) fn new(entries: &'a [Bucket<T>]) -> Self {
38
0
        Self {
39
0
            iter: entries.iter(),
40
0
        }
41
0
    }
42
43
    /// Returns a slice of the remaining entries in the iterator.
44
0
    pub fn as_slice(&self) -> &'a Slice<T> {
45
0
        Slice::from_slice(self.iter.as_slice())
46
0
    }
47
}
48
49
impl<'a, T> Iterator for Iter<'a, T> {
50
    type Item = &'a T;
51
52
    iterator_methods!(Bucket::key_ref);
53
}
54
55
impl<T> DoubleEndedIterator for Iter<'_, T> {
56
    double_ended_iterator_methods!(Bucket::key_ref);
57
}
58
59
impl<T> ExactSizeIterator for Iter<'_, T> {
60
0
    fn len(&self) -> usize {
61
0
        self.iter.len()
62
0
    }
63
}
64
65
impl<T> FusedIterator for Iter<'_, T> {}
66
67
impl<T> Clone for Iter<'_, T> {
68
0
    fn clone(&self) -> Self {
69
0
        Iter {
70
0
            iter: self.iter.clone(),
71
0
        }
72
0
    }
73
}
74
75
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
76
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77
0
        f.debug_list().entries(self.clone()).finish()
78
0
    }
79
}
80
81
impl<T> Default for Iter<'_, T> {
82
0
    fn default() -> Self {
83
0
        Self { iter: [].iter() }
84
0
    }
85
}
86
87
/// An owning iterator over the items of an [`IndexSet`].
88
///
89
/// This `struct` is created by the [`IndexSet::into_iter`] method
90
/// (provided by the [`IntoIterator`] trait). See its documentation for more.
91
pub struct IntoIter<T> {
92
    iter: vec::IntoIter<Bucket<T>>,
93
}
94
95
impl<T> IntoIter<T> {
96
0
    pub(super) fn new(entries: Vec<Bucket<T>>) -> Self {
97
0
        Self {
98
0
            iter: entries.into_iter(),
99
0
        }
100
0
    }
101
102
    /// Returns a slice of the remaining entries in the iterator.
103
0
    pub fn as_slice(&self) -> &Slice<T> {
104
0
        Slice::from_slice(self.iter.as_slice())
105
0
    }
106
}
107
108
impl<T> Iterator for IntoIter<T> {
109
    type Item = T;
110
111
    iterator_methods!(Bucket::key);
112
}
113
114
impl<T> DoubleEndedIterator for IntoIter<T> {
115
    double_ended_iterator_methods!(Bucket::key);
116
}
117
118
impl<T> ExactSizeIterator for IntoIter<T> {
119
0
    fn len(&self) -> usize {
120
0
        self.iter.len()
121
0
    }
122
}
123
124
impl<T> FusedIterator for IntoIter<T> {}
125
126
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
127
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128
0
        let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
129
0
        f.debug_list().entries(iter).finish()
130
0
    }
131
}
132
133
impl<T> Default for IntoIter<T> {
134
0
    fn default() -> Self {
135
0
        Self {
136
0
            iter: Vec::new().into_iter(),
137
0
        }
138
0
    }
139
}
140
141
/// A draining iterator over the items of an [`IndexSet`].
142
///
143
/// This `struct` is created by the [`IndexSet::drain`] method.
144
/// See its documentation for more.
145
pub struct Drain<'a, T> {
146
    iter: vec::Drain<'a, Bucket<T>>,
147
}
148
149
impl<'a, T> Drain<'a, T> {
150
0
    pub(super) fn new(iter: vec::Drain<'a, Bucket<T>>) -> Self {
151
0
        Self { iter }
152
0
    }
153
154
    /// Returns a slice of the remaining entries in the iterator.
155
0
    pub fn as_slice(&self) -> &Slice<T> {
156
0
        Slice::from_slice(self.iter.as_slice())
157
0
    }
158
}
159
160
impl<T> Iterator for Drain<'_, T> {
161
    type Item = T;
162
163
    iterator_methods!(Bucket::key);
164
}
165
166
impl<T> DoubleEndedIterator for Drain<'_, T> {
167
    double_ended_iterator_methods!(Bucket::key);
168
}
169
170
impl<T> ExactSizeIterator for Drain<'_, T> {
171
0
    fn len(&self) -> usize {
172
0
        self.iter.len()
173
0
    }
174
}
175
176
impl<T> FusedIterator for Drain<'_, T> {}
177
178
impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
179
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180
0
        let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
181
0
        f.debug_list().entries(iter).finish()
182
0
    }
183
}
184
185
/// A lazy iterator producing elements in the difference of [`IndexSet`]s.
186
///
187
/// This `struct` is created by the [`IndexSet::difference`] method.
188
/// See its documentation for more.
189
pub struct Difference<'a, T, S> {
190
    iter: Iter<'a, T>,
191
    other: &'a IndexSet<T, S>,
192
}
193
194
impl<'a, T, S> Difference<'a, T, S> {
195
0
    pub(super) fn new<S1>(set: &'a IndexSet<T, S1>, other: &'a IndexSet<T, S>) -> Self {
196
0
        Self {
197
0
            iter: set.iter(),
198
0
            other,
199
0
        }
200
0
    }
201
}
202
203
impl<'a, T, S> Iterator for Difference<'a, T, S>
204
where
205
    T: Eq + Hash,
206
    S: BuildHasher,
207
{
208
    type Item = &'a T;
209
210
0
    fn next(&mut self) -> Option<Self::Item> {
211
0
        while let Some(item) = self.iter.next() {
212
0
            if !self.other.contains(item) {
213
0
                return Some(item);
214
0
            }
215
        }
216
0
        None
217
0
    }
218
219
0
    fn size_hint(&self) -> (usize, Option<usize>) {
220
0
        (0, self.iter.size_hint().1)
221
0
    }
222
}
223
224
impl<T, S> DoubleEndedIterator for Difference<'_, T, S>
225
where
226
    T: Eq + Hash,
227
    S: BuildHasher,
228
{
229
0
    fn next_back(&mut self) -> Option<Self::Item> {
230
0
        while let Some(item) = self.iter.next_back() {
231
0
            if !self.other.contains(item) {
232
0
                return Some(item);
233
0
            }
234
        }
235
0
        None
236
0
    }
237
}
238
239
impl<T, S> FusedIterator for Difference<'_, T, S>
240
where
241
    T: Eq + Hash,
242
    S: BuildHasher,
243
{
244
}
245
246
impl<T, S> Clone for Difference<'_, T, S> {
247
0
    fn clone(&self) -> Self {
248
0
        Difference {
249
0
            iter: self.iter.clone(),
250
0
            ..*self
251
0
        }
252
0
    }
253
}
254
255
impl<T, S> fmt::Debug for Difference<'_, T, S>
256
where
257
    T: fmt::Debug + Eq + Hash,
258
    S: BuildHasher,
259
{
260
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261
0
        f.debug_list().entries(self.clone()).finish()
262
0
    }
263
}
264
265
/// A lazy iterator producing elements in the intersection of [`IndexSet`]s.
266
///
267
/// This `struct` is created by the [`IndexSet::intersection`] method.
268
/// See its documentation for more.
269
pub struct Intersection<'a, T, S> {
270
    iter: Iter<'a, T>,
271
    other: &'a IndexSet<T, S>,
272
}
273
274
impl<'a, T, S> Intersection<'a, T, S> {
275
0
    pub(super) fn new<S1>(set: &'a IndexSet<T, S1>, other: &'a IndexSet<T, S>) -> Self {
276
0
        Self {
277
0
            iter: set.iter(),
278
0
            other,
279
0
        }
280
0
    }
281
}
282
283
impl<'a, T, S> Iterator for Intersection<'a, T, S>
284
where
285
    T: Eq + Hash,
286
    S: BuildHasher,
287
{
288
    type Item = &'a T;
289
290
0
    fn next(&mut self) -> Option<Self::Item> {
291
0
        while let Some(item) = self.iter.next() {
292
0
            if self.other.contains(item) {
293
0
                return Some(item);
294
0
            }
295
        }
296
0
        None
297
0
    }
298
299
0
    fn size_hint(&self) -> (usize, Option<usize>) {
300
0
        (0, self.iter.size_hint().1)
301
0
    }
302
}
303
304
impl<T, S> DoubleEndedIterator for Intersection<'_, T, S>
305
where
306
    T: Eq + Hash,
307
    S: BuildHasher,
308
{
309
0
    fn next_back(&mut self) -> Option<Self::Item> {
310
0
        while let Some(item) = self.iter.next_back() {
311
0
            if self.other.contains(item) {
312
0
                return Some(item);
313
0
            }
314
        }
315
0
        None
316
0
    }
317
}
318
319
impl<T, S> FusedIterator for Intersection<'_, T, S>
320
where
321
    T: Eq + Hash,
322
    S: BuildHasher,
323
{
324
}
325
326
impl<T, S> Clone for Intersection<'_, T, S> {
327
0
    fn clone(&self) -> Self {
328
0
        Intersection {
329
0
            iter: self.iter.clone(),
330
0
            ..*self
331
0
        }
332
0
    }
333
}
334
335
impl<T, S> fmt::Debug for Intersection<'_, T, S>
336
where
337
    T: fmt::Debug + Eq + Hash,
338
    S: BuildHasher,
339
{
340
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341
0
        f.debug_list().entries(self.clone()).finish()
342
0
    }
343
}
344
345
/// A lazy iterator producing elements in the symmetric difference of [`IndexSet`]s.
346
///
347
/// This `struct` is created by the [`IndexSet::symmetric_difference`] method.
348
/// See its documentation for more.
349
pub struct SymmetricDifference<'a, T, S1, S2> {
350
    iter: Chain<Difference<'a, T, S2>, Difference<'a, T, S1>>,
351
}
352
353
impl<'a, T, S1, S2> SymmetricDifference<'a, T, S1, S2>
354
where
355
    T: Eq + Hash,
356
    S1: BuildHasher,
357
    S2: BuildHasher,
358
{
359
0
    pub(super) fn new(set1: &'a IndexSet<T, S1>, set2: &'a IndexSet<T, S2>) -> Self {
360
0
        let diff1 = set1.difference(set2);
361
0
        let diff2 = set2.difference(set1);
362
0
        Self {
363
0
            iter: diff1.chain(diff2),
364
0
        }
365
0
    }
366
}
367
368
impl<'a, T, S1, S2> Iterator for SymmetricDifference<'a, T, S1, S2>
369
where
370
    T: Eq + Hash,
371
    S1: BuildHasher,
372
    S2: BuildHasher,
373
{
374
    type Item = &'a T;
375
376
0
    fn next(&mut self) -> Option<Self::Item> {
377
0
        self.iter.next()
378
0
    }
379
380
0
    fn size_hint(&self) -> (usize, Option<usize>) {
381
0
        self.iter.size_hint()
382
0
    }
383
384
0
    fn fold<B, F>(self, init: B, f: F) -> B
385
0
    where
386
0
        F: FnMut(B, Self::Item) -> B,
387
0
    {
388
0
        self.iter.fold(init, f)
389
0
    }
390
}
391
392
impl<T, S1, S2> DoubleEndedIterator for SymmetricDifference<'_, T, S1, S2>
393
where
394
    T: Eq + Hash,
395
    S1: BuildHasher,
396
    S2: BuildHasher,
397
{
398
0
    fn next_back(&mut self) -> Option<Self::Item> {
399
0
        self.iter.next_back()
400
0
    }
401
402
0
    fn rfold<B, F>(self, init: B, f: F) -> B
403
0
    where
404
0
        F: FnMut(B, Self::Item) -> B,
405
0
    {
406
0
        self.iter.rfold(init, f)
407
0
    }
408
}
409
410
impl<T, S1, S2> FusedIterator for SymmetricDifference<'_, T, S1, S2>
411
where
412
    T: Eq + Hash,
413
    S1: BuildHasher,
414
    S2: BuildHasher,
415
{
416
}
417
418
impl<T, S1, S2> Clone for SymmetricDifference<'_, T, S1, S2> {
419
0
    fn clone(&self) -> Self {
420
0
        SymmetricDifference {
421
0
            iter: self.iter.clone(),
422
0
        }
423
0
    }
424
}
425
426
impl<T, S1, S2> fmt::Debug for SymmetricDifference<'_, T, S1, S2>
427
where
428
    T: fmt::Debug + Eq + Hash,
429
    S1: BuildHasher,
430
    S2: BuildHasher,
431
{
432
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
433
0
        f.debug_list().entries(self.clone()).finish()
434
0
    }
435
}
436
437
/// A lazy iterator producing elements in the union of [`IndexSet`]s.
438
///
439
/// This `struct` is created by the [`IndexSet::union`] method.
440
/// See its documentation for more.
441
pub struct Union<'a, T, S> {
442
    iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
443
}
444
445
impl<'a, T, S> Union<'a, T, S>
446
where
447
    T: Eq + Hash,
448
    S: BuildHasher,
449
{
450
0
    pub(super) fn new<S2>(set1: &'a IndexSet<T, S>, set2: &'a IndexSet<T, S2>) -> Self
451
0
    where
452
0
        S2: BuildHasher,
453
0
    {
454
0
        Self {
455
0
            iter: set1.iter().chain(set2.difference(set1)),
456
0
        }
457
0
    }
458
}
459
460
impl<'a, T, S> Iterator for Union<'a, T, S>
461
where
462
    T: Eq + Hash,
463
    S: BuildHasher,
464
{
465
    type Item = &'a T;
466
467
0
    fn next(&mut self) -> Option<Self::Item> {
468
0
        self.iter.next()
469
0
    }
470
471
0
    fn size_hint(&self) -> (usize, Option<usize>) {
472
0
        self.iter.size_hint()
473
0
    }
474
475
0
    fn fold<B, F>(self, init: B, f: F) -> B
476
0
    where
477
0
        F: FnMut(B, Self::Item) -> B,
478
0
    {
479
0
        self.iter.fold(init, f)
480
0
    }
481
}
482
483
impl<T, S> DoubleEndedIterator for Union<'_, T, S>
484
where
485
    T: Eq + Hash,
486
    S: BuildHasher,
487
{
488
0
    fn next_back(&mut self) -> Option<Self::Item> {
489
0
        self.iter.next_back()
490
0
    }
491
492
0
    fn rfold<B, F>(self, init: B, f: F) -> B
493
0
    where
494
0
        F: FnMut(B, Self::Item) -> B,
495
0
    {
496
0
        self.iter.rfold(init, f)
497
0
    }
498
}
499
500
impl<T, S> FusedIterator for Union<'_, T, S>
501
where
502
    T: Eq + Hash,
503
    S: BuildHasher,
504
{
505
}
506
507
impl<T, S> Clone for Union<'_, T, S> {
508
0
    fn clone(&self) -> Self {
509
0
        Union {
510
0
            iter: self.iter.clone(),
511
0
        }
512
0
    }
513
}
514
515
impl<T, S> fmt::Debug for Union<'_, T, S>
516
where
517
    T: fmt::Debug + Eq + Hash,
518
    S: BuildHasher,
519
{
520
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
521
0
        f.debug_list().entries(self.clone()).finish()
522
0
    }
523
}
524
525
/// A splicing iterator for `IndexSet`.
526
///
527
/// This `struct` is created by [`IndexSet::splice()`].
528
/// See its documentation for more.
529
pub struct Splice<'a, I, T, S>
530
where
531
    I: Iterator<Item = T>,
532
    T: Hash + Eq,
533
    S: BuildHasher,
534
{
535
    iter: crate::map::Splice<'a, UnitValue<I>, T, (), S>,
536
}
537
538
impl<'a, I, T, S> Splice<'a, I, T, S>
539
where
540
    I: Iterator<Item = T>,
541
    T: Hash + Eq,
542
    S: BuildHasher,
543
{
544
0
    pub(super) fn new<R>(set: &'a mut IndexSet<T, S>, range: R, replace_with: I) -> Self
545
0
    where
546
0
        R: RangeBounds<usize>,
547
0
    {
548
0
        Self {
549
0
            iter: set.map.splice(range, UnitValue(replace_with)),
550
0
        }
551
0
    }
552
}
553
554
impl<I, T, S> Iterator for Splice<'_, I, T, S>
555
where
556
    I: Iterator<Item = T>,
557
    T: Hash + Eq,
558
    S: BuildHasher,
559
{
560
    type Item = T;
561
562
0
    fn next(&mut self) -> Option<Self::Item> {
563
0
        Some(self.iter.next()?.0)
564
0
    }
565
566
0
    fn size_hint(&self) -> (usize, Option<usize>) {
567
0
        self.iter.size_hint()
568
0
    }
569
}
570
571
impl<I, T, S> DoubleEndedIterator for Splice<'_, I, T, S>
572
where
573
    I: Iterator<Item = T>,
574
    T: Hash + Eq,
575
    S: BuildHasher,
576
{
577
0
    fn next_back(&mut self) -> Option<Self::Item> {
578
0
        Some(self.iter.next_back()?.0)
579
0
    }
580
}
581
582
impl<I, T, S> ExactSizeIterator for Splice<'_, I, T, S>
583
where
584
    I: Iterator<Item = T>,
585
    T: Hash + Eq,
586
    S: BuildHasher,
587
{
588
0
    fn len(&self) -> usize {
589
0
        self.iter.len()
590
0
    }
591
}
592
593
impl<I, T, S> FusedIterator for Splice<'_, I, T, S>
594
where
595
    I: Iterator<Item = T>,
596
    T: Hash + Eq,
597
    S: BuildHasher,
598
{
599
}
600
601
struct UnitValue<I>(I);
602
603
impl<I: Iterator> Iterator for UnitValue<I> {
604
    type Item = (I::Item, ());
605
606
0
    fn next(&mut self) -> Option<Self::Item> {
607
0
        self.0.next().map(|x| (x, ()))
608
0
    }
609
}
610
611
impl<'a, I, T, S> fmt::Debug for Splice<'a, I, T, S>
612
where
613
    I: fmt::Debug + Iterator<Item = T>,
614
    T: fmt::Debug + Hash + Eq,
615
    S: BuildHasher,
616
{
617
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
618
0
        fmt::Debug::fmt(&self.iter, f)
619
0
    }
620
}
621
622
impl<I: fmt::Debug> fmt::Debug for UnitValue<I> {
623
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
624
0
        fmt::Debug::fmt(&self.0, f)
625
0
    }
626
}