Skip to content

Commit f127423

Browse files
committed
Specialize methods on iter::Cloned<I> where I::Item: Copy.
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.
1 parent 27b9e6d commit f127423

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcore/iter/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,23 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
422422
}
423423
}
424424

425+
#[stable(feature = "iter_cloned_copy", since = "1.13.0")]
426+
impl<'a, I, T: 'a> Iterator for Cloned<I>
427+
where I: Iterator<Item=&'a T>, T: Copy
428+
{
429+
fn nth(&mut self, n: usize) -> Option<T> {
430+
self.it.nth(n).cloned()
431+
}
432+
433+
fn last(self) -> Option<T> {
434+
self.it.last().cloned()
435+
}
436+
437+
fn count(self) -> usize {
438+
self.it.count()
439+
}
440+
}
441+
425442
#[stable(feature = "iter_cloned", since = "1.1.0")]
426443
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
427444
where I: DoubleEndedIterator<Item=&'a T>, T: Clone

0 commit comments

Comments
 (0)