output_iter/adapters/
map.rs

1use crate::{OutputIterator, OutputIteratorVariant};
2
3/// An adapter for mapping items in an `OutputIterator`.
4pub struct Map<I, F> {
5    iter: I,
6    f: F,
7}
8
9impl<I, F> Map<I, F> {
10    pub(crate) fn new(iter: I, f: F) -> Self {
11        Map { iter, f }
12    }
13}
14
15impl<I, F, B> OutputIterator for Map<I, F>
16where
17    I: OutputIterator,
18    F: FnMut(I::Item) -> B,
19{
20    type Item = B;
21    type Output = I::Output;
22    type AfterOutput = std::iter::Map<I::AfterOutput, F>;
23
24    fn next(self) -> OutputIteratorVariant<Self> {
25        use OutputIteratorVariant::*;
26        let Map { iter, mut f } = self;
27
28        match iter.next() {
29            Next(next_iter, item) => {
30                let mapped_item = f(item);
31                Next(Map::new(next_iter, f), mapped_item)
32            }
33            Output(after_output, output) => Output(after_output.map(f), output),
34        }
35    }
36}