Make Zip iterator short-circuit

Python's zip() short-circuits by not even querying its right-hand
iterator if the left-hand one is done. Match that behavior here by not
calling .next() on the right iterator if the left one returns None.
This commit is contained in:
Kevin Ballard 2013-08-03 15:41:29 -07:00
parent fb0b388804
commit a3d18bc95b

View file

@ -929,9 +929,12 @@ pub struct Zip<T, U> {
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
#[inline]
fn next(&mut self) -> Option<(A, B)> {
match (self.a.next(), self.b.next()) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
match self.a.next() {
None => None,
Some(x) => match self.b.next() {
None => None,
Some(y) => Some((x, y))
}
}
}
@ -962,9 +965,12 @@ RandomAccessIterator<(A, B)> for Zip<T, U> {
#[inline]
fn idx(&self, index: uint) -> Option<(A, B)> {
match (self.a.idx(index), self.b.idx(index)) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
match self.a.idx(index) {
None => None,
Some(x) => match self.b.idx(index) {
None => None,
Some(y) => Some((x, y))
}
}
}
}