Skip to content

Commit 80cd4e9

Browse files
committed
Use internal iteration in coalesce
1 parent 1b81921 commit 80cd4e9

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/adaptors/coalesce.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@ where
4040

4141
fn next(&mut self) -> Option<Self::Item> {
4242
// this fuses the iterator
43-
let mut last = match self.last.take() {
44-
None => return None,
45-
Some(x) => x,
46-
};
47-
for next in &mut self.iter {
48-
match self.f.coalesce_pair(last, next) {
49-
Ok(joined) => last = joined,
50-
Err((last_, next_)) => {
51-
self.last = Some(next_);
52-
return Some(last_);
53-
}
54-
}
55-
}
56-
Some(last)
43+
let last = self.last.take()?;
44+
45+
let self_last = &mut self.last;
46+
let self_f = &mut self.f;
47+
Some(
48+
self.iter
49+
.try_fold(last, |last, next| match self_f.coalesce_pair(last, next) {
50+
Ok(joined) => Ok(joined),
51+
Err((last_, next_)) => {
52+
*self_last = Some(next_);
53+
Err(last_)
54+
}
55+
})
56+
.unwrap_or_else(|x| x),
57+
)
5758
}
5859

5960
fn size_hint(&self) -> (usize, Option<usize>) {

0 commit comments

Comments
 (0)