From 2a246b94c6f80635e3256d5e00e7649d493976f7 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 19 May 2020 19:17:43 +0200 Subject: [PATCH] Inline CoalesceCore into CoalesceBy (2) impl --- src/adaptors/mod.rs | 51 ++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index b7d377d28..d2738984a 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -605,37 +605,6 @@ impl Iterator for MergeBy } } -impl CoalesceBy - where I: Iterator -{ - fn next_with(&mut self) -> Option - where F: CoalescePredicate - { - // this fuses the iterator - let mut last = match self.last.take() { - None => return None, - Some(x) => x, - }; - for next in &mut self.iter { - match self.f.coalesce_pair(last, next) { - Ok(joined) => last = joined, - Err((last_, next_)) => { - self.last = Some(next_); - return Some(last_); - } - } - } - - Some(last) - } - - fn size_hint_internal(&self) -> (usize, Option) { - let (low, hi) = size_hint::add_scalar(self.iter.size_hint(), - self.last.is_some() as usize); - ((low > 0) as usize, hi) - } -} - /// An iterator adaptor that may join together adjacent elements. /// /// See [`.coalesce()`](../trait.Itertools.html#method.coalesce) for more information. @@ -693,11 +662,27 @@ impl Iterator for CoalesceBy type Item = T; fn next(&mut self) -> Option { - self.next_with() + // this fuses the iterator + let mut last = match self.last.take() { + None => return None, + Some(x) => x, + }; + for next in &mut self.iter { + match self.f.coalesce_pair(last, next) { + Ok(joined) => last = joined, + Err((last_, next_)) => { + self.last = Some(next_); + return Some(last_); + } + } + } + Some(last) } fn size_hint(&self) -> (usize, Option) { - self.size_hint_internal() + let (low, hi) = size_hint::add_scalar(self.iter.size_hint(), + self.last.is_some() as usize); + ((low > 0) as usize, hi) } fn fold(self, acc: Acc, mut fn_acc: FnAcc) -> Acc