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
use crate::{Iterable, Consumer};

pub struct WithFilter<I, F> {
    pub(crate) iterable: I,
    pub(crate) f: F,
}

impl<I, F> Iterable for WithFilter<I, F>
where
    I: Iterable,
    F: Fn(&I::Item) -> bool,
{
    type C = I::C;
    type CC<U> = I::CC<U>;
    type CR<'a> = I::CR<'a>;
}

impl<I, F> Consumer for WithFilter<I, F>
where
    I: Consumer,
    F: Fn(&I::Item) -> bool,
{
    type Item = I::Item;
    type IntoIter = std::iter::Filter<I::IntoIter, F>;
    fn into_iter(self) -> Self::IntoIter {
        self.iterable.into_iter().filter(self.f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_c() {
        let v = vec![1, 2, 3];
        let res = v.with_filter(|i| i > &1).filter(|i| i > &2);
        assert_eq!(res, vec![3]);
    }

    #[test]
    fn test_cc() {
        let v = vec![1, 2, 3];
        let res = v.with_filter(|i| i > &1).map(|i| i.to_string());
        assert_eq!(res, vec!["2".to_string(), "3".to_string()]);
    }

    #[test]
    fn test_c_r() {
        let v = vec![1, 2, 3];
        let res = (&v).with_filter(|i| i > &&1).filter(|i| i > &&2);
        assert_eq!(res, vec![&3]);
    }

    #[test]
    fn test_cc_r() {
        let v = vec![1, 2, 3];
        let res = (&v).with_filter(|i| i > &&1).map(|i| i.to_string());
        assert_eq!(res, vec!["2".to_string(), "3".to_string()]);
    }
}