Skip to content

Commit a19d61a

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 #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 289f3a4 commit a19d61a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcore/iter/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,23 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
401401
}
402402
}
403403

404+
#[stable(feature = "iter_cloned_copy", since = "1.13.0")]
405+
impl<'a, I, T: 'a> Iterator for Cloned<I>
406+
where I: Iterator<Item=&'a T>, T: Copy
407+
{
408+
fn nth(&mut self, n: usize) -> Option<T> {
409+
self.it.nth(n).cloned()
410+
}
411+
412+
fn last(self) -> Option<T> {
413+
self.it.last().cloned()
414+
}
415+
416+
fn count(self) -> usize {
417+
self.it.count()
418+
}
419+
}
420+
404421
#[stable(feature = "rust1", since = "1.0.0")]
405422
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
406423
where I: DoubleEndedIterator<Item=&'a T>, T: Clone

0 commit comments

Comments
 (0)