1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::{Consumer, Iterable, IterableSeq};

#[must_use = "iterable adaptors are lazy and do nothing unless consumed"]
#[derive(Debug, Clone)]
pub struct LazyCopied<I> {
    pub(crate) iterable: I,
}

impl<'a, I, T> Iterable for LazyCopied<I>
where
    T: 'a + Copy,
    I: Iterable<Item = &'a T>,
{
    type C = I::CC<T>;
    type CC<U> = I::CC<U>;
    type F = I::CF<T>;
    type CF<U> = I::CF<U>;
}

impl<'a, I, T> IterableSeq for LazyCopied<I>
where
    T: 'a + Copy,
    I: IterableSeq<Item = &'a T>,
{
}

impl<'a, I, T> Consumer for LazyCopied<I>
where
    T: 'a + Copy,
    I: Consumer<Item = &'a T>,
{
    type Item = T;
    type IntoIter = std::iter::Copied<I::IntoIter>;
    fn consume(self) -> Self::IntoIter {
        self.iterable.consume().copied()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assert_type;
    use crate::lazy::collect;

    #[test]
    fn smoke() {
        let v = vec![&1, &2, &3];
        let res = collect(v.lazy_copied());
        assert_eq!(res, vec![1, 2, 3]);
    }

    #[test]
    fn test_f() {
        let v = [&1, &2, &3];
        let res = v.lazy_copied().rev();
        assert_type::<[i32; 3]>(res);
    }

    #[test]
    fn test_cf() {
        let v = [&1, &2, &3];
        let res = v.lazy_copied().map(|x| x.to_string());
        assert_type::<[String; 3]>(res);
    }
}