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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::{assert_active_manager, ChannelType, Manager, Mode};
use std::{cell::RefCell, cmp::Ordering, fmt, rc::Rc};

/// List of [Node](crate::Node)s to a signal `T`.
///
/// A `slot` is a list in which one can store node handles.
///
/// ```
/// use revent::{Anchor, Grapher, Manager, Node, Slot};
/// use std::{cell::RefCell, rc::Rc};
///
/// trait BasicSignal {
///     fn basic(&mut self);
/// }
///
/// struct MyAnchor {
///     basic_slot: Slot<dyn BasicSignal>,
///     mng: Manager,
/// }
/// impl MyAnchor {
///     fn new() -> Self {
///         let mng = Manager::new();
///         Self {
///             basic_slot: Slot::new("basic_slot", &mng),
///             mng,
///         }
///     }
/// }
/// impl Anchor for MyAnchor {
///     fn manager(&self) -> &Manager {
///         &self.mng
///     }
/// }
///
/// // ---
///
/// struct MyNode;
/// impl Node<MyAnchor, ()> for MyNode {
///     fn register_emits(_: &MyAnchor) -> () { () }
///
///     fn register_listens(hub: &mut MyAnchor, item: Rc<RefCell<Self>>) {
///         hub.basic_slot.register(item);
///     }
///     const NAME: &'static str = "MyNode";
/// }
/// impl BasicSignal for MyNode {
///     fn basic(&mut self) {
///         println!("Hello from MyNode::basic");
///     }
/// }
///
/// // ---
///
/// let mut hub = MyAnchor::new();
/// let item = hub.subscribe(|_| MyNode);
/// hub.basic_slot.emit(|x| x.basic());
/// hub.unsubscribe(&item);
///
/// Grapher::new(hub.manager()).graph_to_file("target/slot-example.png").unwrap();
/// ```
pub struct Slot<T: ?Sized> {
    manager: Manager,
    name: &'static str,
    nodes: Rc<RefCell<Vec<Rc<RefCell<T>>>>>,
}

impl<T: ?Sized> Slot<T> {
    /// Create a new slot object.
    ///
    /// The manager is used to organize multiple single/slot objects and to ensure that there are no
    /// recursive (double mutable borrow) signal chains.
    ///
    /// `name` is used for error reporting and graph generation in [Manager].
    pub fn new(name: &'static str, manager: &Manager) -> Self {
        manager.ensure_new(name, ChannelType::Direct);
        Self {
            manager: manager.clone(),
            name,
            nodes: Rc::new(RefCell::new(Vec::new())),
        }
    }

    /// Emit a signal on this signal slot.
    pub fn emit<F>(&mut self, mut caller: F)
    where
        F: FnMut(&mut T),
    {
        for item in self.nodes.borrow_mut().iter_mut() {
            let mut item = item.borrow_mut();
            caller(&mut *item);
        }
    }

    /// Continue emitting only if the caller returns `true`. Stops if `false`.
    pub fn emit_short<F>(&mut self, mut caller: F)
    where
        F: FnMut(&mut T) -> bool,
    {
        for item in self.nodes.borrow_mut().iter_mut() {
            let mut item = item.borrow_mut();
            if !caller(&mut *item) {
                break;
            }
        }
    }

    /// Sort the nodes to this slot.
    pub fn sort_by<F>(&mut self, mut compare: F)
    where
        F: FnMut(&T, &T) -> Ordering,
    {
        self.nodes.borrow_mut().sort_by(|x, y| {
            let x = x.borrow();
            let y = y.borrow();
            compare(&*x, &*y)
        });
    }

    /// Add or remove a subscriber object to this slot.
    ///
    /// The action taken depends on whether [Anchor::subscribe](crate::Anchor::subscribe) or
    /// [Anchor::unsubscribe](crate::Anchor::unsubscribe) was called.
    ///
    /// When adding: pushes the item to the end of the list. See [sort_by](Slot::sort_by) if a different order is
    /// desired.
    /// When removing: `find`s the first matching instance and removes it.
    ///
    /// # Panics #
    ///
    /// Panics if called from [Anchor::unsubscribe](crate::Anchor::unsubscribe) while not being
    /// registered.
    ///
    /// Panics if called more than once for the same object from within
    /// [Anchor::subscribe](crate::Anchor::subscribe).
    pub fn register(&mut self, item: Rc<RefCell<T>>) {
        assert_active_manager(&self.manager);
        crate::STACK.with(|x| {
            let mode = x.borrow_mut().last().unwrap().0;
            match mode {
                Mode::Adding => {
                    self.manager.register_listen(self.name);
                    self.nodes.borrow_mut().push(item);
                }
                Mode::Removing => {
                    let mut subs = self.nodes.borrow_mut();
                    match subs
                        .iter()
                        .enumerate()
                        .find(|(_, value)| Rc::ptr_eq(&item, value))
                    {
                        Some((idx, _)) => {
                            subs.remove(idx);
                        }
                        None => {
                            panic!(
                                "revent: unable to deregister nonexistent item: {:?}",
                                self.name
                            );
                        }
                    }
                }
            }
        });
    }
}

impl<T: ?Sized> Clone for Slot<T> {
    /// Cloning is only valid from within an [Anchor::subscribe](crate::Anchor::subscribe) context.
    fn clone(&self) -> Self {
        assert_active_manager(&self.manager);
        self.manager.register_emit(self.name);
        Self {
            manager: self.manager.clone(),
            name: self.name,
            nodes: self.nodes.clone(),
        }
    }
}

struct PointerWrapper<T: ?Sized>(Rc<RefCell<Vec<Rc<RefCell<T>>>>>);

impl<T: ?Sized> fmt::Debug for PointerWrapper<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.0.borrow().iter().map(|x| x.as_ptr()))
            .finish()
    }
}

impl<T: ?Sized> fmt::Debug for Slot<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Slot")
            .field("name", &self.name)
            .field("nodes", &PointerWrapper(self.nodes.clone()))
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use crate::{Anchor, Manager, Node, Slot};
    use std::{cell::RefCell, rc::Rc};

    #[test]
    #[should_panic(expected = "revent: signal modification outside of Anchor context")]
    fn using_signal_push_outside_subscribe() {
        trait Interface {}
        impl Interface for () {}

        let mut signal: Slot<dyn Interface> = Slot::new("signal", &Manager::new());

        signal.register(Rc::new(RefCell::new(())));
    }

    #[test]
    #[should_panic(expected = "revent: signal modification outside of Anchor context")]
    fn using_signal_clone_outside_subscribe() {
        trait Interface {}
        impl Interface for () {}

        let signal: Slot<dyn Interface> = Slot::new("signal", &Manager::new());

        let _ = signal.clone();
    }

    #[test]
    #[should_panic(expected = "revent: manager is different")]
    fn subscribing_with_different_manager() {
        trait Interface {}
        impl Interface for () {}

        // ---

        struct MyAnchor {
            signal_a: Slot<dyn Interface>,
            manager: Manager,
        }

        let mut hub = MyAnchor {
            signal_a: Slot::new("signal_a", &Manager::new()),
            manager: Manager::new(),
        };

        impl Anchor for MyAnchor {
            fn manager(&self) -> &Manager {
                &self.manager
            }
        }

        // ---

        struct MyEmitter;
        struct MyNode;
        impl Node<MyAnchor, MyEmitter> for MyNode {
            fn register_emits(_: &MyAnchor) -> MyEmitter {
                MyEmitter
            }
            fn register_listens(hub: &mut MyAnchor, item: Rc<RefCell<Self>>) {
                hub.signal_a.register(item);
            }
            const NAME: &'static str = "MyNode";
        }
        impl Interface for MyNode {}

        hub.subscribe(|_| MyNode);
    }

    #[test]
    #[should_panic(expected = "revent: name is already registered to this manager: \"signal\"")]
    fn double_subscription() {
        let mng = &Manager::new();

        Slot::<()>::new("signal", mng);
        Slot::<()>::new("signal", mng);
    }
}