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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::cell::UnsafeCell;
use std::rc::Rc;
use std::fmt::{self, Debug};
use std::os::raw::c_void;

use crate::priv_traits::*;
use crate::{StreamRc, Stream, StreamExt};

pub fn stream_rc<'a, T: Clone + 'a>() -> (SenderRc<'a, T>, StreamRc<'a,T>){
    let mut se = SenderRc::new();
    let pt = se.stream();

    (se, pt)
}

#[derive(Default)]
pub struct SenderInner<'a, T>{
    sinks: Vec<Rc<UnsafeCell<dyn SinkInner<'a,T> + 'a>>>,
}

impl<'a, T> SenderInner<'a, T>{
    pub fn new() -> SenderInner<'a,T>{
        SenderInner{ sinks: vec![],  }
    }
}

impl<'a, T: Clone> SenderInner<'a, T>{
    #[inline]
    pub fn send(&mut self, t: T){
        let mut all_alive = true;
        for sink in &mut self.sinks {
            all_alive &= unsafe{ (*sink.get()).call(t.clone()).is_ok() }
        }

        if !all_alive {
            self.clean_up()
        }
    }

    pub fn clean_up(&mut self) {
        self.sinks.retain(|sink| unsafe{ (*sink.get()).is_alive() });
    }
}

impl<'a, T: Clone> StreamInner<'a,T> for SenderInner<'a, T>{
    #[inline]
    fn push(&mut self, st: Rc<UnsafeCell<dyn SinkInner<'a,T> + 'a>>){
        self.sinks.push( st )
    }

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

impl<'a, T> Remove for SenderInner<'a, T>{
    fn remove_raw(&mut self, st: *const c_void){
        let found = self.sinks.iter().position(|sink|{
            sink.get() as *const c_void == st
        });
        if let Some(found) = found{
            self.sinks.remove(found);
        }
    }
}

impl<'a, T: Clone> SinkInner<'a,T> for SenderInner<'a, T>{
    #[inline]
    fn call(&mut self, t: T) -> Result<(),()>{
        self.send(t);
        Ok(())
    }

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

/// Events Sender
///
/// Sending part of a Sender/Stream pair. Keeps track
/// of the children streams and notifies them of any new
/// event if they are still alive.
#[derive(Clone, Default)]
pub struct SenderRc<'a, T>{
    inner: Rc<UnsafeCell<SenderInner<'a, T>>>,
}

impl<'a, T> Debug for SenderRc<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Sender with {} receivers", self.num_receivers())
    }
}

impl<'a, T> SenderRc<'a, T>{
    pub fn new() -> SenderRc<'a,T>{
        SenderRc{ inner: Rc::new(UnsafeCell::new(SenderInner::new())) }
    }

    // pub fn from_sink(sink: Rc<UnsafeCell<SinkInner<'a,T> + 'a>>) -> Sender<'a,T>{
    //     Sender{ inner: Rc::new(UnsafeCell::new(SenderInner::from_sink(sink))) }
    // }
    //
    // pub fn from_sinks(sinks: Vec<Rc<UnsafeCell<SinkInner<'a,T> + 'a>>>) -> Sender<'a,T>{
    //     Sender{ inner: Rc::new(UnsafeCell::new(SenderInner::from_sinks(sinks))) }
    // }

    pub fn remove_raw(&mut self, st: *const c_void){
        unsafe{ (*self.inner.get()).remove_raw(st) }
    }

    pub fn num_receivers(&self) -> usize{
        unsafe{ (*self.inner.get()).sinks.len() }
    }

    pub fn into_inner(self) -> Rc<UnsafeCell<SenderInner<'a,T>>>{
        self.inner
    }
}

impl<'a, T: Clone> SenderRc<'a, T>{
    pub fn push(&mut self, st: Rc<UnsafeCell<dyn SinkInner<'a,T> + 'a>>){
        unsafe{ (*self.inner.get()).push(st) }
    }

    #[inline]
    pub fn send(&self, t: T){
        let inner = unsafe{ &mut *self.inner.get() };
        inner.send(t)
    }
}

impl<'a, T: Clone + 'a> SenderRc<'a, T>{
    #[inline]
    pub fn stream(&mut self) -> StreamRc<'a,T>{
        StreamRc::new(self.inner.clone())
    }
}

impl<'a, T: Clone + 'a> From<Stream<'a,T>> for StreamRc<'a,T>{
    fn from(st: Stream<'a,T>) -> StreamRc<'a,T>{
        StreamRc::from_parent(st, Rc::new(UnsafeCell::new(SenderInner::new())))
    }
}

impl<'a, 'b, T: Clone + 'b, S: WithInner<'a,T> + StreamInner<'a,T>> StreamExt<'b,T>  for &'b S where 'a: 'b{
    //TODO: this should be a unique inner
    fn unique(self) -> Stream<'b,T>{
        Stream::from_parent(self, Rc::new(UnsafeCell::new(SenderInner::new())))
    }

    fn rc(self) -> StreamRc<'b,T>{
        StreamRc::from_parent(self, Rc::new(UnsafeCell::new(SenderInner::new())))
    }
}