From f1274234a1f1fe1c168e34aaf96d6f2e06245ef5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 27 Sep 2016 15:39:30 -0700 Subject: [PATCH] Specialize methods on iter::Cloned 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. --- src/libcore/iter/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 3999db0d63c99..ca2c3ceb4a112 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -422,6 +422,23 @@ impl<'a, I, T: 'a> Iterator for Cloned } } +#[stable(feature = "iter_cloned_copy", since = "1.13.0")] +impl<'a, I, T: 'a> Iterator for Cloned + where I: Iterator, T: Copy +{ + fn nth(&mut self, n: usize) -> Option { + self.it.nth(n).cloned() + } + + fn last(self) -> Option { + self.it.last().cloned() + } + + fn count(self) -> usize { + self.it.count() + } +} + #[stable(feature = "iter_cloned", since = "1.1.0")] impl<'a, I, T: 'a> DoubleEndedIterator for Cloned where I: DoubleEndedIterator, T: Clone