From cb71c58b428b109c1ce631d6c0f6e03405130c91 Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Sat, 28 Sep 2019 19:38:40 +0200 Subject: [PATCH] Specialize `count`, `last` and `nth` PutBack iterator for performance They were missing and did hurt the MergeJoinBy performance improvement on the `nth` method because we couldn't borrow iter without exposing new interfaces or using something like the `take_mut` crate. --- src/adaptors/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 4d0f5e129..35d721a51 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -243,6 +243,26 @@ impl Iterator for PutBack size_hint::add_scalar(self.iter.size_hint(), self.top.is_some() as usize) } + fn count(self) -> usize { + self.iter.count() + (self.top.is_some() as usize) + } + + fn last(self) -> Option { + self.iter.last().or(self.top) + } + + fn nth(&mut self, n: usize) -> Option { + if let Some(top) = self.top.take() { + if n == 0 { + Some(top) + } else { + self.iter.nth(n - 1) + } + } else { + self.iter.nth(n) + } + } + fn all(&mut self, mut f: G) -> bool where G: FnMut(Self::Item) -> bool {