sigs_slots/lib.rs
1use std::rc::Rc;
2use std::cell::RefCell;
3use std::sync::Arc;
4use std::sync::RwLock;
5
6/// Generic Event Enum for transmitting a signal
7pub enum Event<T> {
8 Sig(T),
9}
10
11/// Generic Signal trait to be implemented by the object emitting the signals
12pub trait Signal<T> {
13 /// Connects the given slot to this signal's list of consumers
14 /// # Arguments
15 /// * 'slot' - An Rc wrapped RefCell containing the slot to be connected
16 fn connect(&mut self, slot: Rc<RefCell<dyn Slot<T>>>);
17 /// Emits the given signal to all connected slots
18 /// # Arguments
19 /// * 'event' - the event to be sent to the slots
20 fn emit(&mut self, event: Event<T>);
21}
22
23#[cfg(feature = "threadsafe")]
24/// Generic Sync Signal trait to be implemented by the object emitting the signals
25pub trait SyncSignal<T> {
26 // Connects the given slot to this signal's list of consumers
27 // # Arguments
28 // * 'slot' - An Arc wrapped RwLock containing the slot to be connected
29 fn connect(&mut self, slot: Arc<RwLock<dyn Slot<T>>>);
30 /// Emits the given signal to all connected slots
31 /// # Arguments
32 /// * 'event' - the event to be sent to the slots
33 fn emit(&mut self, event: Event<T>);
34}
35
36/// Generic slot trait to be implemented by the object consuming the signals
37pub trait Slot<T> {
38 /// Consumes the event emitted by the signal(s) this slot is connected to
39 /// # Arguments
40 /// * 'event' - the event this slot is consuming
41 fn consume(&mut self, event: &Event<T>);
42}