]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_data_structures/src/sso/either_iter.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_data_structures / src / sso / either_iter.rs
CommitLineData
29967ef6
XL
1use std::fmt;
2use std::iter::ExactSizeIterator;
3use std::iter::FusedIterator;
4use std::iter::Iterator;
5
6/// Iterator which may contain instance of
7/// one of two specific implementations.
8///
9/// Note: For most methods providing custom
5e7ed085 10/// implementation may marginally
29967ef6
XL
11/// improve performance by avoiding
12/// doing Left/Right match on every step
13/// and doing it only once instead.
14#[derive(Clone)]
15pub enum EitherIter<L, R> {
16 Left(L),
17 Right(R),
18}
19
20impl<L, R> Iterator for EitherIter<L, R>
21where
22 L: Iterator,
23 R: Iterator<Item = L::Item>,
24{
25 type Item = L::Item;
26
27 fn next(&mut self) -> Option<Self::Item> {
28 match self {
29 EitherIter::Left(l) => l.next(),
30 EitherIter::Right(r) => r.next(),
31 }
32 }
33
34 fn size_hint(&self) -> (usize, Option<usize>) {
35 match self {
36 EitherIter::Left(l) => l.size_hint(),
37 EitherIter::Right(r) => r.size_hint(),
38 }
39 }
40}
41
42impl<L, R> ExactSizeIterator for EitherIter<L, R>
43where
44 L: ExactSizeIterator,
45 R: ExactSizeIterator,
46 EitherIter<L, R>: Iterator,
47{
48 fn len(&self) -> usize {
49 match self {
50 EitherIter::Left(l) => l.len(),
51 EitherIter::Right(r) => r.len(),
52 }
53 }
54}
55
56impl<L, R> FusedIterator for EitherIter<L, R>
57where
58 L: FusedIterator,
59 R: FusedIterator,
60 EitherIter<L, R>: Iterator,
61{
62}
63
64impl<L, R> fmt::Debug for EitherIter<L, R>
65where
66 L: fmt::Debug,
67 R: fmt::Debug,
68{
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 EitherIter::Left(l) => l.fmt(f),
72 EitherIter::Right(r) => r.fmt(f),
73 }
74 }
75}