|
10 | 10 | C: CountItem<I::Item>,
|
11 | 11 | {
|
12 | 12 | iter: I,
|
13 |
| - last: Option<C::CItem>, |
| 13 | + last: Option<Option<C::CItem>>, |
14 | 14 | f: F,
|
15 | 15 | }
|
16 | 16 |
|
@@ -45,36 +45,47 @@ where
|
45 | 45 | type Item = C::CItem;
|
46 | 46 |
|
47 | 47 | fn next(&mut self) -> Option<Self::Item> {
|
| 48 | + let Self { iter, last, f } = self; |
48 | 49 | // this fuses the iterator
|
49 |
| - let last = self.last.take()?; |
| 50 | + let init = match last { |
| 51 | + Some(elt) => elt.take(), |
| 52 | + None => { |
| 53 | + *last = Some(None); |
| 54 | + iter.next().map(C::new) |
| 55 | + } |
| 56 | + }?; |
50 | 57 |
|
51 |
| - let self_last = &mut self.last; |
52 |
| - let self_f = &mut self.f; |
53 | 58 | Some(
|
54 |
| - self.iter |
55 |
| - .try_fold(last, |last, next| match self_f.coalesce_pair(last, next) { |
56 |
| - Ok(joined) => Ok(joined), |
57 |
| - Err((last_, next_)) => { |
58 |
| - *self_last = Some(next_); |
59 |
| - Err(last_) |
60 |
| - } |
61 |
| - }) |
62 |
| - .unwrap_or_else(|x| x), |
| 59 | + iter.try_fold(init, |accum, next| match f.coalesce_pair(accum, next) { |
| 60 | + Ok(joined) => Ok(joined), |
| 61 | + Err((last_, next_)) => { |
| 62 | + *last = Some(Some(next_)); |
| 63 | + Err(last_) |
| 64 | + } |
| 65 | + }) |
| 66 | + .unwrap_or_else(|x| x), |
63 | 67 | )
|
64 | 68 | }
|
65 | 69 |
|
66 | 70 | fn size_hint(&self) -> (usize, Option<usize>) {
|
67 |
| - let (low, hi) = size_hint::add_scalar(self.iter.size_hint(), self.last.is_some() as usize); |
| 71 | + let (low, hi) = size_hint::add_scalar( |
| 72 | + self.iter.size_hint(), |
| 73 | + matches!(self.last, Some(Some(_))) as usize, |
| 74 | + ); |
68 | 75 | ((low > 0) as usize, hi)
|
69 | 76 | }
|
70 | 77 |
|
71 | 78 | fn fold<Acc, FnAcc>(self, acc: Acc, mut fn_acc: FnAcc) -> Acc
|
72 | 79 | where
|
73 | 80 | FnAcc: FnMut(Acc, Self::Item) -> Acc,
|
74 | 81 | {
|
75 |
| - if let Some(last) = self.last { |
76 |
| - let mut f = self.f; |
77 |
| - let (last, acc) = self.iter.fold((last, acc), |(last, acc), elt| { |
| 82 | + let Self { |
| 83 | + mut iter, |
| 84 | + last, |
| 85 | + mut f, |
| 86 | + } = self; |
| 87 | + if let Some(last) = last.unwrap_or_else(|| iter.next().map(C::new)) { |
| 88 | + let (last, acc) = iter.fold((last, acc), |(last, acc), elt| { |
78 | 89 | match f.coalesce_pair(last, elt) {
|
79 | 90 | Ok(joined) => (joined, acc),
|
80 | 91 | Err((last_, next_)) => (next_, fn_acc(acc, last_)),
|
@@ -135,12 +146,12 @@ where
|
135 | 146 | }
|
136 | 147 |
|
137 | 148 | /// Create a new `Coalesce`.
|
138 |
| -pub fn coalesce<I, F>(mut iter: I, f: F) -> Coalesce<I, F> |
| 149 | +pub fn coalesce<I, F>(iter: I, f: F) -> Coalesce<I, F> |
139 | 150 | where
|
140 | 151 | I: Iterator,
|
141 | 152 | {
|
142 | 153 | Coalesce {
|
143 |
| - last: iter.next(), |
| 154 | + last: None, |
144 | 155 | iter,
|
145 | 156 | f,
|
146 | 157 | }
|
@@ -192,12 +203,12 @@ impl<T, F: FnMut(&T, &T) -> bool> DedupPredicate<T> for F {
|
192 | 203 | }
|
193 | 204 |
|
194 | 205 | /// Create a new `DedupBy`.
|
195 |
| -pub fn dedup_by<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupBy<I, Pred> |
| 206 | +pub fn dedup_by<I, Pred>(iter: I, dedup_pred: Pred) -> DedupBy<I, Pred> |
196 | 207 | where
|
197 | 208 | I: Iterator,
|
198 | 209 | {
|
199 | 210 | DedupBy {
|
200 |
| - last: iter.next(), |
| 211 | + last: None, |
201 | 212 | iter,
|
202 | 213 | f: DedupPred2CoalescePred(dedup_pred),
|
203 | 214 | }
|
@@ -251,12 +262,12 @@ where
|
251 | 262 | pub type DedupWithCount<I> = DedupByWithCount<I, DedupEq>;
|
252 | 263 |
|
253 | 264 | /// Create a new `DedupByWithCount`.
|
254 |
| -pub fn dedup_by_with_count<I, Pred>(mut iter: I, dedup_pred: Pred) -> DedupByWithCount<I, Pred> |
| 265 | +pub fn dedup_by_with_count<I, Pred>(iter: I, dedup_pred: Pred) -> DedupByWithCount<I, Pred> |
255 | 266 | where
|
256 | 267 | I: Iterator,
|
257 | 268 | {
|
258 | 269 | DedupByWithCount {
|
259 |
| - last: iter.next().map(WithCount::new), |
| 270 | + last: None, |
260 | 271 | iter,
|
261 | 272 | f: DedupPredWithCount2CoalescePred(dedup_pred),
|
262 | 273 | }
|
|
0 commit comments