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::rc::Rc;
use std::cell::RefCell;
use std::sync::Arc;
use std::sync::RwLock;

/// Generic Event Enum for transmitting a signal
pub enum Event<T> {
    Sig(T),
}

/// Generic Signal trait to be implemented by the object emitting the signals
pub trait Signal<T> {
    /// Connects the given slot to this signal's list of consumers
    /// # Arguments
    /// * 'slot' - An Rc wrapped RefCell containing the slot to be connected
    fn connect(&mut self, slot: Rc<RefCell<dyn Slot<T>>>);
    /// Emits the given signal to all connected slots
    /// # Arguments
    /// * 'event' - the event to be sent to the slots
    fn emit(&mut self, event: Event<T>);
}

#[cfg(feature = "threadsafe")]
/// Generic Sync Signal trait to be implemented by the object emitting the signals
pub trait SyncSignal<T> {
    // Connects the given slot to this signal's list of consumers
    // # Arguments
    //  * 'slot' - An Arc wrapped RwLock containing the slot to be connected
    fn connect(&mut self, slot: Arc<RwLock<dyn Slot<T>>>);
    /// Emits the given signal to all connected slots
    /// # Arguments
    /// * 'event' - the event to be sent to the slots
    fn emit(&mut self, event: Event<T>);
}

/// Generic slot trait to be implemented by the object consuming the signals
pub trait Slot<T> {
    /// Consumes the event emitted by the signal(s) this slot is connected to
    /// # Arguments
    /// * 'event' - the event this slot is consuming
    fn consume(&mut self, event: &Event<T>);
}