Skip to content

Commit

Permalink
Specialize methods on iter::Cloned<I> where I::Item: Copy.
Browse files Browse the repository at this point in the history
Instead of cloning a bunch of copyable types only to drop them (in `nth`,
`last`, and `count`), take advantage of rust-lang#1521 (Copy clone semantics) and don't
bother cloning them in the first place (directly call `nth`, `last`, and `count`
on the wrapped iterator). If the wrapped iterator optimizes these methods,
`Cloned` now inherits this optimization.
  • Loading branch information
Stebalien committed Sep 30, 2016
1 parent 289f3a4 commit a19d61a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,23 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
}
}

#[stable(feature = "iter_cloned_copy", since = "1.13.0")]
impl<'a, I, T: 'a> Iterator for Cloned<I>
where I: Iterator<Item=&'a T>, T: Copy
{
fn nth(&mut self, n: usize) -> Option<T> {
self.it.nth(n).cloned()
}

fn last(self) -> Option<T> {
self.it.last().cloned()
}

fn count(self) -> usize {
self.it.count()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
where I: DoubleEndedIterator<Item=&'a T>, T: Clone
Expand Down

0 comments on commit a19d61a

Please sign in to comment.