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

pub trait StreamInner<'a,T>: Remove{
    fn push(&mut self, st: Rc<UnsafeCell<dyn SinkInner<'a,T> + 'a>>);
    fn send(&mut self, t: T);
}

pub trait SinkInner<'a,T>{
    fn call(&mut self, t: T) -> Result<(),()>;
    fn is_alive(&self) -> bool;
}

pub trait WithInner<'a,T>{
    fn inner_rc(&self) -> Rc<UnsafeCell<dyn StreamInner<'a,T> + 'a>>;
    fn remove_rc(&self) -> Rc<UnsafeCell<dyn Remove + 'a>>;
    fn into_inner(self) -> Rc<UnsafeCell<dyn StreamInner<'a,T> + 'a>>;
    fn into_remove(self) -> Rc<UnsafeCell<dyn Remove + 'a>>;
}

pub trait Remove{
    fn remove_raw(&mut self, st: *const c_void);
}

impl<R: Remove + ?Sized> Remove for Vec<Rc<UnsafeCell<R>>>{
    fn remove_raw(&mut self, st: *const c_void){
        for parent in self.iter_mut(){
            unsafe{ (*parent.get()).remove_raw(st) };
        }
    }
}

impl<R: Remove + ?Sized> Remove for Rc<UnsafeCell<Vec<Rc<UnsafeCell<R>>>>>{
    fn remove_raw(&mut self, st: *const c_void){
        unsafe{
            for parent in (*self.get()).iter_mut(){
                (*parent.get()).remove_raw(st);
            }
        }
    }
}