iter_scan/
scan_clone.rs

1use crate::{Iter, PseudoFunc};
2use core::marker::PhantomData;
3
4/// Pseudo-function to duplicate values by [cloning](Clone).
5#[derive(Debug, Clone, Copy)]
6struct DuplicateByCloning;
7
8impl<X: Clone> PseudoFunc<X, (X, X)> for DuplicateByCloning {
9    fn exec(x: X) -> (X, X) {
10        (x.clone(), x)
11    }
12}
13
14/// An iterator created by [`scan_clone`](crate::IterScan::scan_clone).
15#[derive(Debug, Clone, Copy)]
16#[must_use = "iterators are lazy and do nothing unless consumed"]
17pub struct ScanClone<Source, Compute, Value> {
18    internal: Iter<Source, DuplicateByCloning, Compute, Value, Value>,
19}
20
21impl<Source, Compute, Value> ScanClone<Source, Compute, Value> {
22    pub(crate) fn new(source: Source, initial: Value, compute: Compute) -> Self {
23        let internal = Iter {
24            source,
25            compute,
26            state: initial,
27            _phantom: PhantomData,
28        };
29        ScanClone { internal }
30    }
31}
32
33impl<Source, Compute, Value> Iterator for ScanClone<Source, Compute, Value>
34where
35    Source: Iterator,
36    Compute: FnMut(Value, Source::Item) -> Value,
37    Value: Clone,
38{
39    type Item = 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}