output_iter/adapters/
map_output.rs

1use crate::{OutputIterator, OutputIteratorVariant};
2
3/// An OutputIterator that transforms the intermediate output using a provided function.
4pub struct MapOutput<I, F> {
5    iter: I,
6    f: F,
7}
8
9impl<I, F> MapOutput<I, F> {
10    pub(crate) fn new(iter: I, f: F) -> Self {
11        MapOutput { iter, f }
12    }
13}
14
15impl<B, I: OutputIterator, F> OutputIterator for MapOutput<I, F>
16where
17    F: FnOnce(I::Output) -> B,
18{
19    type Item = I::Item;
20    type Output = B;
21    type AfterOutput = I::AfterOutput;
22
23    fn next(self) -> OutputIteratorVariant<Self> {
24        use OutputIteratorVariant::*;
25
26        let MapOutput { iter, f } = self;
27        match iter.next() {
28            Next(next_iter, item) => Next(MapOutput::new(next_iter, f), item),
29            Output(next_iter, value) => Output(next_iter, f(value)),
30        }
31    }
32}