Skip to content

Commit 8a05e9e

Browse files
committed
Inline CoalesceCore into Coalesce (1)
1 parent 5f08cfd commit 8a05e9e

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

src/adaptors/mod.rs

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

608-
#[derive(Clone, Debug)]
609-
pub struct CoalesceCore<I, T>
610-
where I: Iterator
611-
{
612-
iter: I,
613-
last: Option<T>,
614-
}
615-
616-
impl<I, T> CoalesceCore<I, T>
617-
where I: Iterator
608+
impl<I, F, T> Coalesce<I, F, T>
609+
where I: Iterator,
610+
F: CoalescePredicate<I::Item, T>,
618611
{
619-
fn next_with<F>(&mut self, mut f: F) -> Option<T>
620-
where F: FnMut(T, I::Item) -> Result<T, (T, T)>
621-
{
612+
fn next_with(&mut self) -> Option<T> {
622613
// this fuses the iterator
623614
let mut last = match self.last.take() {
624615
None => return None,
625616
Some(x) => x,
626617
};
627618
for next in &mut self.iter {
628-
match f(last, next) {
619+
match self.f.coalesce_pair(last, next) {
629620
Ok(joined) => last = joined,
630621
Err((last_, next_)) => {
631622
self.last = Some(next_);
@@ -637,11 +628,6 @@ impl<I, T> CoalesceCore<I, T>
637628
Some(last)
638629
}
639630

640-
fn size_hint(&self) -> (usize, Option<usize>) {
641-
let (low, hi) = size_hint::add_scalar(self.iter.size_hint(),
642-
self.last.is_some() as usize);
643-
((low > 0) as usize, hi)
644-
}
645631
}
646632

647633
/// An iterator adaptor that may join together adjacent elements.
@@ -651,7 +637,8 @@ impl<I, T> CoalesceCore<I, T>
651637
pub struct Coalesce<I, F, T>
652638
where I: Iterator
653639
{
654-
iter: CoalesceCore<I, T>,
640+
iter: I,
641+
last: Option<T>,
655642
f: F,
656643
}
657644

@@ -670,25 +657,23 @@ impl<F, Item, T> CoalescePredicate<Item, T> for F
670657
impl<I: Clone, F: Clone, T: Clone> Clone for Coalesce<I, F, T>
671658
where I: Iterator,
672659
{
673-
clone_fields!(iter, f);
660+
clone_fields!(iter, last, f);
674661
}
675662

676663
impl<I, F, T> fmt::Debug for Coalesce<I, F, T>
677664
where I: Iterator + fmt::Debug,
678665
T: fmt::Debug,
679666
{
680-
debug_fmt_fields!(Coalesce, iter);
667+
debug_fmt_fields!(Coalesce, iter, last);
681668
}
682669

683670
/// Create a new `Coalesce`.
684671
pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F, I::Item>
685672
where I: Iterator
686673
{
687674
Coalesce {
688-
iter: CoalesceCore {
689-
last: iter.next(),
690-
iter,
691-
},
675+
last: iter.next(),
676+
iter,
692677
f,
693678
}
694679
}
@@ -700,20 +685,21 @@ impl<I, F, T> Iterator for Coalesce<I, F, T>
700685
type Item = T;
701686

702687
fn next(&mut self) -> Option<Self::Item> {
703-
let f = &mut self.f;
704-
self.iter.next_with(|last, item| f.coalesce_pair(last, item))
688+
self.next_with()
705689
}
706690

707691
fn size_hint(&self) -> (usize, Option<usize>) {
708-
self.iter.size_hint()
692+
let (low, hi) = size_hint::add_scalar(self.iter.size_hint(),
693+
self.last.is_some() as usize);
694+
((low > 0) as usize, hi)
709695
}
710696

711697
fn fold<Acc, FnAcc>(self, acc: Acc, mut fn_acc: FnAcc) -> Acc
712698
where FnAcc: FnMut(Acc, Self::Item) -> Acc,
713699
{
714-
if let Some(last) = self.iter.last {
700+
if let Some(last) = self.last {
715701
let mut f = self.f;
716-
let (last, acc) = self.iter.iter.fold((last, acc), |(last, acc), elt| {
702+
let (last, acc) = self.iter.fold((last, acc), |(last, acc), elt| {
717703
match f.coalesce_pair(last, elt) {
718704
Ok(joined) => (joined, acc),
719705
Err((last_, next_)) => (next_, fn_acc(acc, last_)),
@@ -776,10 +762,8 @@ pub fn dedup_by<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupBy<I, Pred>
776762
where I: Iterator,
777763
{
778764
Coalesce {
779-
iter: CoalesceCore {
780-
last: iter.next(),
781-
iter,
782-
},
765+
last: iter.next(),
766+
iter,
783767
f: DedupPred2CoalescePred(dedup_pred),
784768
}
785769
}

0 commit comments

Comments
 (0)