iterable/lazy/
lazy_copied.rs

1use crate::{Consumer, Iterable, IterableSeq};
2
3#[must_use = "iterable adaptors are lazy and do nothing unless consumed"]
4#[derive(Debug, Clone)]
5pub struct LazyCopied<I> {
6    pub(crate) iterable: I,
7}
8
9impl<'a, I, T> Iterable for LazyCopied<I>
10where
11    T: 'a + Copy,
12    I: Iterable<Item = &'a T>,
13{
14    type C = I::CC<T>;
15    type CC<U> = I::CC<U>;
16    type F = I::CF<T>;
17    type CF<U> = I::CF<U>;
18}
19
20impl<'a, I, T> IterableSeq for LazyCopied<I>
21where
22    T: 'a + Copy,
23    I: IterableSeq<Item = &'a T>,
24{
25}
26
27impl<'a, I, T> Consumer for LazyCopied<I>
28where
29    T: 'a + Copy,
30    I: Consumer<Item = &'a T>,
31{
32    type Item = T;
33    type IntoIter = std::iter::Copied<I::IntoIter>;
34    fn consume(self) -> Self::IntoIter {
35        self.iterable.consume().copied()
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::assert_type;
43    use crate::lazy::collect;
44
45    #[test]
46    fn smoke() {
47        let v = vec![&1, &2, &3];
48        let res = collect(v.lazy_copied());
49        assert_eq!(res, vec![1, 2, 3]);
50    }
51
52    #[test]
53    fn test_f() {
54        let v = [&1, &2, &3];
55        let res = v.lazy_copied().rev();
56        assert_type::<[i32; 3]>(res);
57    }
58
59    #[test]
60    fn test_cf() {
61        let v = [&1, &2, &3];
62        let res = v.lazy_copied().map(|x| x.to_string());
63        assert_type::<[String; 3]>(res);
64    }
65}