Skip to content

Commit ed607dd

Browse files
author
blake2-ppc
committed
std::iterator: Implement .next_back() for Zip
Let Zip be double-ended when both its children have the ExactSizeHint trait.
1 parent 4b2cc22 commit ed607dd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libstd/iterator.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,29 @@ impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
936936
}
937937
}
938938

939+
impl<A, B,
940+
T: DoubleEndedIterator<A> + ExactSizeHint,
941+
U: DoubleEndedIterator<B> + ExactSizeHint> DoubleEndedIterator<(A, B)>
942+
for Zip<T, U> {
943+
#[inline]
944+
fn next_back(&mut self) -> Option<(A, B)> {
945+
let (a_sz, _) = self.a.size_hint();
946+
let (b_sz, _) = self.b.size_hint();
947+
if a_sz < b_sz {
948+
for _ in range(0, b_sz - a_sz) { self.b.next_back(); }
949+
} else if a_sz > b_sz {
950+
for _ in range(0, a_sz - b_sz) { self.a.next_back(); }
951+
}
952+
let (a_sz, _) = self.a.size_hint();
953+
let (b_sz, _) = self.b.size_hint();
954+
assert!(a_sz == b_sz);
955+
match (self.a.next_back(), self.b.next_back()) {
956+
(Some(x), Some(y)) => Some((x, y)),
957+
_ => None
958+
}
959+
}
960+
}
961+
939962
impl<A, B, T: RandomAccessIterator<A>, U: RandomAccessIterator<B>>
940963
RandomAccessIterator<(A, B)> for Zip<T, U> {
941964
#[inline]

0 commit comments

Comments
 (0)