make skip a double ended iterator

This commit is contained in:
Oliver Schneider 2016-03-04 10:22:55 +01:00
parent 8e261d12e3
commit 25e5de3f7e
2 changed files with 49 additions and 0 deletions

View File

@ -3851,6 +3851,17 @@ impl<I> Iterator for Skip<I> where I: Iterator {
#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}
#[stable(feature = "double_ended_skip_iterator", since = "1.8.0")]
impl<I> DoubleEndedIterator for Skip<I> where I: DoubleEndedIterator + ExactSizeIterator {
fn next_back(&mut self) -> Option<Self::Item> {
if self.len() > 0 {
self.iter.next_back()
} else {
None
}
}
}
/// An iterator that only iterates over the first `n` iterations of `iter`.
///
/// This `struct` is created by the [`take()`] method on [`Iterator`]. See its

View File

@ -303,6 +303,44 @@ fn test_iterator_skip() {
assert_eq!(it.len(), 0);
}
#[test]
fn test_iterator_skip_doubleended() {
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
let mut it = xs.iter().rev().skip(5);
assert_eq!(it.next(), Some(&15));
assert_eq!(it.by_ref().rev().next(), Some(&0));
assert_eq!(it.next(), Some(&13));
assert_eq!(it.by_ref().rev().next(), Some(&1));
assert_eq!(it.next(), Some(&5));
assert_eq!(it.by_ref().rev().next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), None);
let mut it = xs.iter().rev().skip(5).rev();
assert_eq!(it.next(), Some(&0));
assert_eq!(it.rev().next(), Some(&15));
let mut it_base = xs.iter();
{
let mut it = it_base.by_ref().skip(5).rev();
assert_eq!(it.next(), Some(&30));
assert_eq!(it.next(), Some(&20));
assert_eq!(it.next(), Some(&19));
assert_eq!(it.next(), Some(&17));
assert_eq!(it.next(), Some(&16));
assert_eq!(it.next(), Some(&15));
assert_eq!(it.next(), Some(&13));
assert_eq!(it.next(), None);
}
// make sure the skipped parts have not been consumed
assert_eq!(it_base.next(), Some(&0));
assert_eq!(it_base.next(), Some(&1));
assert_eq!(it_base.next(), Some(&2));
assert_eq!(it_base.next(), Some(&3));
assert_eq!(it_base.next(), Some(&5));
assert_eq!(it_base.next(), None);
let it = xs.iter().skip(5).rev();
assert_eq!(it.last(), Some(&13));
}
#[test]
fn test_iterator_skip_nth() {
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];