Skip to content

Commit 33a2465

Browse files
committed
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.
1 parent d73460b commit 33a2465

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/adaptors/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,28 @@ impl<I> Iterator for PutBack<I>
243243
size_hint::add_scalar(self.iter.size_hint(), self.top.is_some() as usize)
244244
}
245245

246+
fn count(self) -> usize {
247+
self.iter.count() + (self.top.is_some() as usize)
248+
}
249+
250+
fn last(self) -> Option<Self::Item> {
251+
self.iter.last().or(self.top)
252+
}
253+
254+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
255+
match self.top {
256+
None => self.iter.nth(n),
257+
ref mut some => {
258+
if n == 0 {
259+
some.take()
260+
} else {
261+
*some = None;
262+
self.iter.nth(n - 1)
263+
}
264+
}
265+
}
266+
}
267+
246268
fn all<G>(&mut self, mut f: G) -> bool
247269
where G: FnMut(Self::Item) -> bool
248270
{

0 commit comments

Comments
 (0)