reductor/
iter.rs

1use super::Reductor;
2
3/// Allow reducing an [`Iterator`] with a [`Reductor`].
4pub trait Reduce: Iterator + Sized {
5    /// Similar to [`Iterator::reduce`], but uses a generic implementation of [`Reductor`],
6    /// instead of a function parameter, to supply the reduction logic.
7    #[inline]
8    fn reduce_with<R>(self) -> R
9    where
10        R: Reductor<Self::Item>,
11        R::State: Default,
12    {
13        let state = R::State::default();
14        R::into_result(self.fold(state, R::reduce))
15    }
16
17    /// Similar to [`Iterator::fold`], but uses a generic implementation of [`Reductor`],
18    /// instead of a function parameter, to supply the reduction logic.
19    #[inline]
20    fn fold_with<R, I>(self, init: I) -> R
21    where
22        R: Reductor<Self::Item>,
23        R::State: From<I>,
24    {
25        R::into_result(self.fold(init.into(), R::reduce))
26    }
27}
28
29impl<I> Reduce for I where I: Iterator {}