Skip to content

Implement Clone for iterator adaptors using functions #19837

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

Closed
wants to merge 1 commit into from
Closed
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
106 changes: 106 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,15 @@ impl<A, B, I, F> RandomAccessIterator<B> for Map<A, B, I, F> where
}
}

impl<A, B, I, F> Clone for Map<A, B, I, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(A) -> B,
{
fn clone(&self) -> Map<A, B, I, F> {
Map{iter: self.iter.clone(), f: self.f.clone()}
}
}

/// An iterator which filters the elements of `iter` with `predicate`
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
Expand Down Expand Up @@ -1486,6 +1495,15 @@ impl<A, I, P> DoubleEndedIterator<A> for Filter<A, I, P> where
}
}

impl<A, I, P> Clone for Filter<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> Filter<A, I, P> {
Filter{iter: self.iter.clone(), predicate: self.predicate.clone()}
}
}

/// An iterator which uses `f` to both filter and map elements from `iter`
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
Expand Down Expand Up @@ -1534,6 +1552,15 @@ impl<A, B, I, F> DoubleEndedIterator<B> for FilterMap<A, B, I, F> where
}
}

impl<A, B, I, F> Clone for FilterMap<A, B, I, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(A) -> Option<B>,
{
fn clone(&self) -> FilterMap<A, B, I, F> {
FilterMap{iter: self.iter.clone(), f: self.f.clone()}
}
}

/// An iterator which yields the current count and the element during iteration
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
Expand Down Expand Up @@ -1677,6 +1704,19 @@ impl<A, I, P> Iterator<A> for SkipWhile<A, I, P> where I: Iterator<A>, P: FnMut(
}
}

impl<A, I, P> Clone for SkipWhile<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> SkipWhile<A, I, P> {
SkipWhile {
iter: self.iter.clone(),
flag: self.flag,
predicate: self.predicate.clone()
}
}
}

/// An iterator which only accepts elements while `predicate` is true
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
Expand Down Expand Up @@ -1714,6 +1754,19 @@ impl<A, I, P> Iterator<A> for TakeWhile<A, I, P> where I: Iterator<A>, P: FnMut(
}
}

impl<A, I, P> Clone for TakeWhile<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> TakeWhile<A, I, P> {
TakeWhile {
iter: self.iter.clone(),
flag: self.flag,
predicate: self.predicate.clone()
}
}
}

/// An iterator which skips over `n` elements of `iter`.
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
Expand Down Expand Up @@ -1864,6 +1917,20 @@ impl<A, B, I, St, F> Iterator<B> for Scan<A, B, I, St, F> where
}
}

impl<A, B, I, St, F> Clone for Scan<A, B, I, St, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(&mut St, A) -> Option<B>,
St: Clone
{
fn clone(&self) -> Scan<A, B, I, St, F> {
Scan {
iter: self.iter.clone(),
f: self.f.clone(),
state: self.state.clone()
}
}
}

/// An iterator that maps each element to an iterator,
/// and yields the elements of the produced iterators
///
Expand Down Expand Up @@ -1932,6 +1999,21 @@ impl<A, B, I, U, F> DoubleEndedIterator<B> for FlatMap<A, B, I, U, F> where
}
}

impl<A, B, I, U, F> Clone for FlatMap<A, B, I, U, F> where
I: Clone + Iterator<A>,
U: Clone + Iterator<B>,
F: Clone + FnMut(A) -> U,
{
fn clone(&self) -> FlatMap<A, B, I, U, F> {
FlatMap {
iter: self.iter.clone(),
f: self.f.clone(),
frontiter: self.frontiter.clone(),
backiter: self.backiter.clone(),
}
}
}

/// An iterator that yields `None` forever after the underlying iterator
/// yields `None` once.
#[deriving(Clone)]
Expand Down Expand Up @@ -2075,6 +2157,18 @@ impl<A, I, F> RandomAccessIterator<A> for Inspect<A, I, F> where
}
}

impl<A, I, F> Clone for Inspect<A, I, F> where
I: Iterator<A> + Clone,
F: FnMut(&A) + Clone,
{
fn clone(&self) -> Inspect<A, I, F> {
Inspect {
iter: self.iter.clone(),
f: self.f.clone()
}
}
}

/// An iterator which passes mutable state to a closure and yields the result.
///
/// # Example: The Fibonacci Sequence
Expand Down Expand Up @@ -2141,6 +2235,18 @@ impl<A, St, F> Iterator<A> for Unfold<A, St, F> where F: FnMut(&mut St) -> Optio
}
}

impl<A, St, F> Clone for Unfold<A, St, F> where
St: Clone,
F: Clone + FnMut(&mut St) -> Option<A>
{
fn clone(&self) -> Unfold<A, St, F> {
Unfold {
f: self.f.clone(),
state: self.state.clone()
}
}
}

/// An infinite iterator starting at `start` and advancing by `step` with each
/// iteration
#[deriving(Clone)]
Expand Down
9 changes: 3 additions & 6 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,9 @@ fn test_random_access_inspect() {
#[test]
fn test_random_access_map() {
let xs = [1i, 2, 3, 4, 5];

let mut it = xs.iter().map(|x| *x);
assert_eq!(xs.len(), it.indexable());
for (i, elt) in xs.iter().enumerate() {
assert_eq!(Some(*elt), it.idx(i));
}
fn negate(x: &int) -> int { -*x }
let mut it = xs.iter().map(negate);
check_randacc_iter(it, xs.len());
}

#[test]
Expand Down