Skip to content

Commit 2a246b9

Browse files
committed
Inline CoalesceCore into CoalesceBy (2) impl
1 parent a36ed86 commit 2a246b9

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

src/adaptors/mod.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -605,37 +605,6 @@ impl<I, J, F> Iterator for MergeBy<I, J, F>
605605
}
606606
}
607607

608-
impl<I, F, T> CoalesceBy<I, F, T>
609-
where I: Iterator
610-
{
611-
fn next_with(&mut self) -> Option<T>
612-
where F: CoalescePredicate<I::Item, T>
613-
{
614-
// this fuses the iterator
615-
let mut last = match self.last.take() {
616-
None => return None,
617-
Some(x) => x,
618-
};
619-
for next in &mut self.iter {
620-
match self.f.coalesce_pair(last, next) {
621-
Ok(joined) => last = joined,
622-
Err((last_, next_)) => {
623-
self.last = Some(next_);
624-
return Some(last_);
625-
}
626-
}
627-
}
628-
629-
Some(last)
630-
}
631-
632-
fn size_hint_internal(&self) -> (usize, Option<usize>) {
633-
let (low, hi) = size_hint::add_scalar(self.iter.size_hint(),
634-
self.last.is_some() as usize);
635-
((low > 0) as usize, hi)
636-
}
637-
}
638-
639608
/// An iterator adaptor that may join together adjacent elements.
640609
///
641610
/// See [`.coalesce()`](../trait.Itertools.html#method.coalesce) for more information.
@@ -693,11 +662,27 @@ impl<I, F, T> Iterator for CoalesceBy<I, F, T>
693662
type Item = T;
694663

695664
fn next(&mut self) -> Option<Self::Item> {
696-
self.next_with()
665+
// this fuses the iterator
666+
let mut last = match self.last.take() {
667+
None => return None,
668+
Some(x) => x,
669+
};
670+
for next in &mut self.iter {
671+
match self.f.coalesce_pair(last, next) {
672+
Ok(joined) => last = joined,
673+
Err((last_, next_)) => {
674+
self.last = Some(next_);
675+
return Some(last_);
676+
}
677+
}
678+
}
679+
Some(last)
697680
}
698681

699682
fn size_hint(&self) -> (usize, Option<usize>) {
700-
self.size_hint_internal()
683+
let (low, hi) = size_hint::add_scalar(self.iter.size_hint(),
684+
self.last.is_some() as usize);
685+
((low > 0) as usize, hi)
701686
}
702687

703688
fn fold<Acc, FnAcc>(self, acc: Acc, mut fn_acc: FnAcc) -> Acc

0 commit comments

Comments
 (0)