1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use std::cmp::Ordering::{Equal, Greater, Less};
#[cfg(feature = "unstable")]
use std::cmp;
#[cfg(feature = "unstable")]
use std::iter::RandomAccessIterator;
use super::size_hint;
use self::EitherOrBoth::{Right, Left, Both};
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct ZipLongest<T, U> {
    a: T,
    b: U
}
impl<T, U> ZipLongest<T, U>
{
    
    pub fn new(a: T, b: U) -> ZipLongest<T, U>
    {
        ZipLongest{a: a, b: b}
    }
}
impl<A, B, T, U> Iterator for ZipLongest<T, U> where
    T: Iterator<Item=A>,
    U: Iterator<Item=B>,
{
    type Item = EitherOrBoth<A, B>;
    #[inline]
    fn next(&mut self) -> Option<EitherOrBoth<A, B>> {
        match (self.a.next(), self.b.next()) {
            (None, None) => None,
            (Some(a), None) => Some(Left(a)),
            (None, Some(b)) => Some(Right(b)),
            (Some(a), Some(b)) => Some(Both(a, b)),
        }
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        size_hint::max(self.a.size_hint(), self.b.size_hint())
    }
}
impl<A, B, T, U> DoubleEndedIterator for ZipLongest<T, U> where
    T: DoubleEndedIterator<Item=A> + ExactSizeIterator,
    U: DoubleEndedIterator<Item=B> + ExactSizeIterator,
{
    #[inline]
    fn next_back(&mut self) -> Option<EitherOrBoth<A, B>> {
        match self.a.len().cmp(&self.b.len()) {
            Equal => match (self.a.next_back(), self.b.next_back()) {
                (None, None) => None,
                (Some(a), Some(b)) => Some(Both(a, b)),
                
                (Some(a), None) => Some(Left(a)),
                (None, Some(b)) => Some(Right(b)),
            },
            Greater => self.a.next_back().map(Left),
            Less => self.b.next_back().map(Right),
        }
    }
}
#[cfg(feature = "unstable")]
impl<A, B, T, U> RandomAccessIterator for ZipLongest<T, U> where
    T: RandomAccessIterator<Item=A>,
    U: RandomAccessIterator<Item=B>,
{
    #[inline]
    fn indexable(&self) -> usize {
        cmp::max(self.a.indexable(), self.b.indexable())
    }
    #[inline]
    fn idx(&mut self, index: usize) -> Option<EitherOrBoth<A, B>> {
        match (self.a.idx(index), self.b.idx(index)) {
            (None, None) => None,
            (Some(a), None) => Some(Left(a)),
            (None, Some(b)) => Some(Right(b)),
            (Some(a), Some(b)) => Some(Both(a, b)),
        }
    }
}
impl<T, U> ExactSizeIterator for ZipLongest<T, U> where
    T: ExactSizeIterator,
    U: ExactSizeIterator,
{}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum EitherOrBoth<A, B> {
    
    Both(A, B),
    
    
    Left(A),
    
    
    Right(B),
}