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 std::cell::UnsafeCell;
use std::rc::Rc;
use std::os::raw::c_void;
use std::marker::PhantomData;

use crate::Sender;
use crate::priv_traits::*;

pub struct Scan<'a, T1, T2, T3, F: FnMut(&mut T2,T1) -> Option<T3>>{
    func: F,
    scan: T2,
    sender: Sender<'a,T3>,
    _marker2: PhantomData<T1>,
    _marker3: PhantomData<T3>,
}

impl<'a,T1, T2, T3, F: FnMut(&mut T2,T1) -> Option<T3>> Scan<'a,T1,T2,T3,F>{
    pub fn new(f: F, scan: T2) -> Scan<'a,T1,T2,T3,F>{
        Scan{
            func: f,
            scan: scan,
            sender: Sender::new(),
            _marker2: PhantomData,
            _marker3: PhantomData,
        }
    }
}

impl<'a,T1, T2, T3, F: FnMut(&mut T2,T1) -> Option<T3>> StreamInner<'a,T3> for Scan<'a,T1,T2,T3,F>{
    #[inline]
    fn push(&mut self, st: Rc<UnsafeCell<dyn SinkInner<'a,T3>+'a>>){
        self.sender.push(st)
    }

    #[inline]
    fn send(&mut self, t: T3){
        self.sender.send(t)
    }
}

impl<'a,T1, T2, T3, F: FnMut(&mut T2,T1) -> Option<T3>> Remove for Scan<'a,T1,T2,T3,F>{
    fn remove_raw(&mut self, st: *const c_void){
        self.sender.remove_raw(st)
    }
}

impl<'a,T1, T2, T3, F: FnMut(&mut T2,T1) -> Option<T3>> SinkInner<'a, T1> for Scan<'a,T1,T2,T3,F>{
    #[inline]
    fn call(&mut self, t: T1) -> Result<(),()>{
        let send = (self.func)(&mut self.scan, t);
        if let Some(send) = send{
            self.sender.send(send);
        }
        Ok(())
    }

    #[inline]
    fn is_alive(&self) -> bool {
        true
    }
}