Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Skip a DoubleEndedIterator #31700

Merged
merged 1 commit into from
Mar 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down