iter_scan/
scan_state_clone.rs

1use crate::{Iter, PseudoFunc};
2use core::marker::PhantomData;
3
4/// Pseudo-function to duplicate states by [cloning](Clone).
5#[derive(Debug, Clone, Copy)]
6struct DuplicateX0ByCloning;
7
8impl<X0: Clone, X1> PseudoFunc<(X0, X1), (X0, (X0, X1))> for DuplicateX0ByCloning {
9    fn exec(x: (X0, X1)) -> (X0, (X0, X1)) {
10        (x.0.clone(), x)
11    }
12}
13
14/// An iterator created by [`scan_state_clone`](crate::IterScan::scan_state_clone).
15#[derive(Debug, Clone, Copy)]
16#[must_use = "iterators are lazy and do nothing unless consumed"]
17pub struct ScanStateClone<Source, Compute, State, Value> {
18    internal: Iter<Source, DuplicateX0ByCloning, Compute, State, (State, Value)>,
19}
20
21impl<Source, Compute, State, Value> ScanStateClone<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        ScanStateClone { internal }
30    }
31}
32
33impl<Source, Compute, State, Value> Iterator for ScanStateClone<Source, Compute, State, Value>
34where
35    Source: Iterator,
36    Compute: FnMut(State, Source::Item) -> (State, Value),
37    State: Clone,
38{
39    type Item = (State, Value);
40
41    fn next(&mut self) -> Option<Self::Item> {
42        self.internal.next()
43    }
44
45    fn size_hint(&self) -> (usize, Option<usize>) {
46        self.internal.size_hint()
47    }
48}