Skip to content

Commit a36ed86

Browse files
committed
Inline CoalesceCore into CoalesceBy (1) fields
1 parent 5d7f32e commit a36ed86

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

src/adaptors/mod.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -605,27 +605,19 @@ 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>
608+
impl<I, F, T> CoalesceBy<I, F, T>
617609
where I: Iterator
618610
{
619-
fn next_with<F>(&mut self, mut f: F) -> Option<T>
620-
where F: FnMut(T, I::Item) -> Result<T, (T, T)>
611+
fn next_with(&mut self) -> Option<T>
612+
where F: CoalescePredicate<I::Item, T>
621613
{
622614
// this fuses the iterator
623615
let mut last = match self.last.take() {
624616
None => return None,
625617
Some(x) => x,
626618
};
627619
for next in &mut self.iter {
628-
match f(last, next) {
620+
match self.f.coalesce_pair(last, next) {
629621
Ok(joined) => last = joined,
630622
Err((last_, next_)) => {
631623
self.last = Some(next_);
@@ -637,7 +629,7 @@ impl<I, T> CoalesceCore<I, T>
637629
Some(last)
638630
}
639631

640-
fn size_hint(&self) -> (usize, Option<usize>) {
632+
fn size_hint_internal(&self) -> (usize, Option<usize>) {
641633
let (low, hi) = size_hint::add_scalar(self.iter.size_hint(),
642634
self.last.is_some() as usize);
643635
((low > 0) as usize, hi)
@@ -653,7 +645,8 @@ pub type Coalesce<I, F> = CoalesceBy<I, F, <I as Iterator>::Item>;
653645
pub struct CoalesceBy<I, F, T>
654646
where I: Iterator
655647
{
656-
iter: CoalesceCore<I, T>,
648+
iter: I,
649+
last: Option<T>,
657650
f: F,
658651
}
659652

@@ -672,7 +665,7 @@ impl<F, Item, T> CoalescePredicate<Item, T> for F
672665
impl<I: Clone, F: Clone, T: Clone> Clone for CoalesceBy<I, F, T>
673666
where I: Iterator,
674667
{
675-
clone_fields!(iter, f);
668+
clone_fields!(last, iter, f);
676669
}
677670

678671
impl<I, F, T> fmt::Debug for CoalesceBy<I, F, T>
@@ -687,10 +680,8 @@ pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F>
687680
where I: Iterator
688681
{
689682
Coalesce {
690-
iter: CoalesceCore {
691-
last: iter.next(),
692-
iter,
693-
},
683+
last: iter.next(),
684+
iter,
694685
f,
695686
}
696687
}
@@ -702,20 +693,19 @@ impl<I, F, T> Iterator for CoalesceBy<I, F, T>
702693
type Item = T;
703694

704695
fn next(&mut self) -> Option<Self::Item> {
705-
let f = &mut self.f;
706-
self.iter.next_with(|last, item| f.coalesce_pair(last, item))
696+
self.next_with()
707697
}
708698

709699
fn size_hint(&self) -> (usize, Option<usize>) {
710-
self.iter.size_hint()
700+
self.size_hint_internal()
711701
}
712702

713703
fn fold<Acc, FnAcc>(self, acc: Acc, mut fn_acc: FnAcc) -> Acc
714704
where FnAcc: FnMut(Acc, Self::Item) -> Acc,
715705
{
716-
if let Some(last) = self.iter.last {
706+
if let Some(last) = self.last {
717707
let mut f = self.f;
718-
let (last, acc) = self.iter.iter.fold((last, acc), |(last, acc), elt| {
708+
let (last, acc) = self.iter.fold((last, acc), |(last, acc), elt| {
719709
match f.coalesce_pair(last, elt) {
720710
Ok(joined) => (joined, acc),
721711
Err((last_, next_)) => (next_, fn_acc(acc, last_)),
@@ -778,10 +768,8 @@ pub fn dedup_by<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupBy<I, Pred>
778768
where I: Iterator,
779769
{
780770
DedupBy {
781-
iter: CoalesceCore {
782-
last: iter.next(),
783-
iter,
784-
},
771+
last: iter.next(),
772+
iter,
785773
f: DedupPred2CoalescePred(dedup_pred),
786774
}
787775
}
@@ -827,10 +815,8 @@ pub fn dedup_by_with_count<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupByWit
827815
where I: Iterator,
828816
{
829817
DedupByWithCount {
830-
iter: CoalesceCore {
831-
last: iter.next().map(|v| (1, v)),
832-
iter,
833-
},
818+
last: iter.next().map(|v| (1, v)),
819+
iter,
834820
f: DedupPredWithCount2CoalescePred(dedup_pred),
835821
}
836822
}

0 commit comments

Comments
 (0)