Skip to content

Commit 7ea5e72

Browse files
committed
Reduce genericity in Scan
1 parent 9e58fcc commit 7ea5e72

File tree

1 file changed

+17
-8
lines changed
  • src/libcore/iter/adapters

1 file changed

+17
-8
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,8 @@ impl<B, I, St, F> Iterator for Scan<I, St, F> where
17831783

17841784
#[inline]
17851785
fn next(&mut self) -> Option<B> {
1786-
self.iter.next().and_then(|a| (self.f)(&mut self.state, a))
1786+
let a = self.iter.next()?;
1787+
(self.f)(&mut self.state, a)
17871788
}
17881789

17891790
#[inline]
@@ -1793,17 +1794,25 @@ impl<B, I, St, F> Iterator for Scan<I, St, F> where
17931794
}
17941795

17951796
#[inline]
1796-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1797+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
17971798
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
17981799
{
1800+
fn scan<'a, T, St, B, Acc, R: Try<Ok = Acc>>(
1801+
state: &'a mut St,
1802+
f: &'a mut impl FnMut(&mut St, T) -> Option<B>,
1803+
mut fold: impl FnMut(Acc, B) -> R + 'a,
1804+
) -> impl FnMut(Acc, T) -> LoopState<Acc, R> + 'a {
1805+
move |acc, x| {
1806+
match f(state, x) {
1807+
None => LoopState::Break(Try::from_ok(acc)),
1808+
Some(x) => LoopState::from_try(fold(acc, x)),
1809+
}
1810+
}
1811+
}
1812+
17991813
let state = &mut self.state;
18001814
let f = &mut self.f;
1801-
self.iter.try_fold(init, move |acc, x| {
1802-
match f(state, x) {
1803-
None => LoopState::Break(Try::from_ok(acc)),
1804-
Some(x) => LoopState::from_try(fold(acc, x)),
1805-
}
1806-
}).into_try()
1815+
self.iter.try_fold(init, scan(state, f, fold)).into_try()
18071816
}
18081817
}
18091818

0 commit comments

Comments
 (0)