2 use std::iter::ExactSizeIterator;
3 use std::iter::FusedIterator;
4 use std::iter::Iterator;
6 /// Iterator which may contain instance of
7 /// one of two specific implementations.
9 /// Note: For most methods providing custom
10 /// implementation may marginally
11 /// improve performance by avoiding
12 /// doing Left/Right match on every step
13 /// and doing it only once instead.
15 pub enum EitherIter<L, R> {
20 impl<L, R> Iterator for EitherIter<L, R>
23 R: Iterator<Item = L::Item>,
27 fn next(&mut self) -> Option<Self::Item> {
29 EitherIter::Left(l) => l.next(),
30 EitherIter::Right(r) => r.next(),
34 fn size_hint(&self) -> (usize, Option<usize>) {
36 EitherIter::Left(l) => l.size_hint(),
37 EitherIter::Right(r) => r.size_hint(),
42 impl<L, R> ExactSizeIterator for EitherIter<L, R>
46 EitherIter<L, R>: Iterator,
48 fn len(&self) -> usize {
50 EitherIter::Left(l) => l.len(),
51 EitherIter::Right(r) => r.len(),
56 impl<L, R> FusedIterator for EitherIter<L, R>
60 EitherIter<L, R>: Iterator,
64 impl<L, R> fmt::Debug for EitherIter<L, R>
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 EitherIter::Left(l) => l.fmt(f),
72 EitherIter::Right(r) => r.fmt(f),