iter_scan/
scan_with_tuple.rs

1use crate::{Iter, PseudoFunc};
2use core::marker::PhantomData;
3
4/// Pseudo-function that returns the value it received as-is.
5#[derive(Debug, Clone, Copy)]
6pub struct Identity;
7
8impl<X> PseudoFunc<X, X> for Identity {
9    fn exec(x: X) -> X {
10        x
11    }
12}
13
14/// An iterator created by [`scan_with_tuple`](crate::IterScan::scan_with_tuple).
15#[derive(Debug, Clone, Copy)]
16#[must_use = "iterators are lazy and do nothing unless consumed"]
17pub struct ScanWithTuple<Source, Compute, State, Value> {
18    internal: Iter<Source, Identity, Compute, State, Value>,
19}
20
21impl<Source, Compute, State, Value> ScanWithTuple<Source, Compute, State, Value> {
22    pub(crate) fn new(source: Source, initial: State, compute: Compute) -> Self {
23        let internal = Iter {
24            source,
25            compute,
26            state: initial,
27            _phantom: PhantomData,
28        };
29        Self { internal }
30    }
31}
32
33impl<Source, Compute, State, Value> Iterator for ScanWithTuple<Source, Compute, State, Value>
34where
35    Source: Iterator,
36    Compute: FnMut(State, Source::Item) -> (State, Value),
37{
38    type Item = Value;
39
40    fn next(&mut self) -> Option<Self::Item> {
41        self.internal.next()
42    }
43
44    fn size_hint(&self) -> (usize, Option<usize>) {
45        self.internal.size_hint()
46    }
47}