From a19d61a3ab44b78a1cb991b10b14f85c01b9d478 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 dd57fd1b5190f..1c3b4572e08fc 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -401,6 +401,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 = "rust1", since = "1.0.0")] impl<'a, I, T: 'a> DoubleEndedIterator for Cloned where I: DoubleEndedIterator, T: Clone