mutcy 0.5.2

Zero-cost safe mutable cyclic borrows using borrow relinquishing
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use self::{
    fractional_index::{FractionalIndex, FractionalIndexType},
    inner::{Action, SignalInner},
};
use super::{IntoWeak, Key, KeyCell, Meta, Rw};
use std::{borrow::Borrow, rc::Rc};

mod fractional_index;
mod inner;
#[cfg(test)]
mod tests;

type Handler<T> = dyn Fn(&mut Key, &T) -> Action;

struct Receiver<T> {
    handler: Rc<Handler<T>>,
    relative_index: FractionalIndexType,
    id: u64,
}

trait Disconnectable {
    fn disconnect(&self, key: &mut Key, id: u64, relative_index: FractionalIndexType);
}

impl<T: 'static> Disconnectable for KeyCell<SignalInner<T>> {
    fn disconnect(&self, key: &mut Key, id: u64, relative_index: FractionalIndexType) {
        self.rw(key).disconnect(id, relative_index);
    }
}

/// References a connection created via [Signal::connect].
pub struct Connection {
    inner: Rc<dyn Disconnectable>,
    relative_index: FractionalIndexType,
    id: u64,
}

impl Connection {
    /// Disconnect this connection.
    ///
    /// Does nothing if the connection is already removed. This can happen if we
    /// destroy the receiver object and run [emit](Signal::emit).
    ///
    /// # Time Complexity #
    ///
    /// Takes `O(connection_count)` time. All connections that come after this
    /// connection need to be shifted by one position. In the worst case if
    /// disconnecting the first connection, all other connections must be
    /// shifted.
    pub fn disconnect(self, key: &mut Key) {
        self.inner.disconnect(key, self.id, self.relative_index);
    }
}

/// Holds weak references to [KeyCell]s on which callbacks can be run.
///
/// # Examples #
///
/// ```
/// use mutcy::{Key, KeyCell, Signal};
/// use std::rc::Rc;
///
/// let key = &mut Key::acquire();
///
/// let signal = Signal::new();
/// let item = Rc::new(KeyCell::new(1, ()));
///
/// signal.connect(key, &item, |this, event| {
///     **this *= event;
/// });
///
/// assert_eq!(1, *item.ro(key));
///
/// signal.emit(key, 2);
/// assert_eq!(2, *item.ro(key));
///
/// signal.emit(key, 2);
/// assert_eq!(4, *item.ro(key));
///
/// signal.emit(key, 3);
/// assert_eq!(12, *item.ro(key));
/// ```
///
/// # Connection Ordering #
///
/// [Connect](Signal::connect)ions are invoked upon a call to
/// [emit](Signal::emit). The order of these invocations corresponds
/// to the order in which `connect` was called.
///
/// Using [before](Signal::before) or [after](Signal::after) allows different
/// ordering using fractional indexing.
///
/// ```
/// use mutcy::{Key, KeyCell, Rw, Signal};
/// use std::rc::Rc;
///
/// let key = &mut Key::acquire();
///
/// let signal = Signal::new();
/// let item = Rc::new(KeyCell::new(0, ()));
///
/// signal.before(key).connect(key, &item, |this, event| {
///     **this *= event;
/// });
/// ```
pub struct Signal<T> {
    inner: Rc<KeyCell<SignalInner<T>>>,
    index: FractionalIndex,
}

impl<T: 'static> Signal<T> {
    /// Creates a new `Signal`.
    pub fn new() -> Self {
        Signal {
            inner: Rc::new(KeyCell::new(SignalInner::new(), ())),
            index: FractionalIndex::new(),
        }
    }

    /// Creates a cloned `Signal` whose connections are invoked **before** the
    /// parent signal's connections.
    ///
    /// By default, connections are invoked in the order they were created via
    /// [`connect`](Signal::connect). This method creates a priority tier -
    /// all connections made to the cloned signal will execute *before* any
    /// connections on the parent signal.
    ///
    /// ## Invocation Order
    /// - Parent signal: Base priority
    /// - `before(key)` clones: Higher priority (executed first)
    /// - `after(key)` clones: Lower priority (executed last)
    ///
    /// ## Ordering Notes ##
    /// - `signal.before(key).after(key)` creates a middle priority tier between
    ///   the parent and `before` clone.
    /// - Subsequent calls to `before(key)`/`after(key)` on clones maintain
    ///   their relative ordering.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw, Signal};
    /// use std::{cell::RefCell, rc::Rc};
    ///
    /// let key = &mut Key::acquire();
    ///
    /// let signal = Signal::new();
    /// let shared = Rc::new(RefCell::new(Vec::new()));
    ///
    /// let item = Rc::new(KeyCell::new(1, ()));
    ///
    /// let shared_clone = shared.clone();
    /// signal.connect(key, &item, move |this, event| {
    ///     shared_clone.borrow_mut().push(1);
    ///     println!("This runs after");
    /// });
    ///
    /// let shared_clone = shared.clone();
    /// signal.before(key).connect(key, &item, move |this, event| {
    ///     shared_clone.borrow_mut().push(0);
    ///     println!("This runs before");
    /// });
    ///
    /// signal.emit(key, ());
    /// assert_eq!(&*shared.borrow(), &[0, 1]);
    /// ```
    pub fn before(&self, key: &mut Key) -> Self {
        if let Some(index) = self.index.left() {
            Self {
                inner: self.inner.clone(),
                index,
            }
        } else {
            self.subsignal_at(key, self.index.value() - 1)
        }
    }

    /// Creates a cloned `Signal` whose connections are invoked **after** the
    /// parent signal's connections.
    ///
    /// By default, connections are invoked in the order they were created via
    /// [`connect`](Signal::connect). This method creates a priority tier -
    /// all connections made to the cloned signal will execute *after* any
    /// connections on the parent signal.
    ///
    /// ## Invocation Order
    /// - Parent signal: Base priority
    /// - `before(key)` clones: Higher priority (executed first)
    /// - `after(key)` clones: Lower priority (executed last)
    ///
    /// ## Ordering Notes ##
    /// - `signal.after(key).before(key)` creates a middle priority tier between
    ///   the parent and `after` clone.
    /// - Subsequent calls to `before(key)`/`after(key)` on clones maintain
    ///   their relative ordering.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw, Signal};
    /// use std::{cell::RefCell, rc::Rc};
    ///
    /// let key = &mut Key::acquire();
    ///
    /// let signal = Signal::new();
    /// let shared = Rc::new(RefCell::new(Vec::new()));
    ///
    /// let item = Rc::new(KeyCell::new(1, ()));
    ///
    /// let shared_clone = shared.clone();
    /// signal.after(key).connect(key, &item, move |this, event| {
    ///     shared_clone.borrow_mut().push(1);
    ///     println!("This runs after");
    /// });
    ///
    /// let shared_clone = shared.clone();
    /// signal.connect(key, &item, move |this, event| {
    ///     shared_clone.borrow_mut().push(0);
    ///     println!("This runs before");
    /// });
    ///
    /// signal.emit(key, ());
    /// assert_eq!(&*shared.borrow(), &[0, 1]);
    /// ```
    pub fn after(&self, key: &mut Key) -> Self {
        if let Some(index) = self.index.right() {
            Self {
                inner: self.inner.clone(),
                index,
            }
        } else {
            self.subsignal_at(key, self.index.value() + 1)
        }
    }

    /// Connects a receiver to this signal.
    ///
    /// Handlers are removed automatically if the receiver has no strong
    /// references left.
    ///
    /// # Note #
    ///
    /// Holding a `Signal` instance inside `handler` may cause a reference
    /// cycle. If the `Signal` is instead held inside `receiver`, no
    /// reference cycle will be possible.
    ///
    /// # Panics #
    ///
    /// Panics if the receiver has no strong
    /// reference. This is to prevent cases such as [Rc::new_cyclic]
    /// connecting a weak followed by an [emit](Signal::emit) causing the
    /// not-yet constructed `Rc` from being removed.
    ///
    /// # Ordering #
    ///
    /// Handlers are invoked in the same order in which they were `connect`ed.
    /// To specify a different ordering, see [Signal::before] and
    /// [Signal::after].
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw, Signal};
    /// use std::rc::Rc;
    ///
    /// let key = &mut Key::acquire();
    ///
    /// let signal = Signal::new();
    /// let item = Rc::new(KeyCell::new(1, ()));
    ///
    /// signal.connect(key, &item, |this, event| {
    ///     **this *= event;
    /// });
    /// ```
    pub fn connect<R, I, F>(&self, key: &mut Key, receiver: &I, handler: F) -> Connection
    where
        R: Meta + 'static,
        I: IntoWeak<R>,
        F: Fn(Rw<R>, &T) + 'static,
    {
        let receiver = receiver.into();
        let id = self
            .inner
            .rw(key)
            .connect(receiver, self.index.value(), handler);

        Connection {
            inner: self.inner.clone(),
            relative_index: self.index.value(),
            id,
        }
    }

    /// Creates a new subsignal.
    ///
    /// Creates a new [Signal] object and connects it to this (parent) signal.
    /// Any [emit](Signal::emit) run on the
    /// parent signal is forwarded to the subsignal at this particular order as
    /// specified by previous [before](Signal::before)/
    /// [after](Signal::after) calls.
    ///
    /// The main purpose of a subsignal is to group together a set of slots for
    /// two reasons.
    ///
    /// 1. Cluster connections into a single priority group.
    /// 2. To enable fast [disconnect](Connection)ion.
    ///
    /// # Performance #
    /// Subsignals are an extra indirection step making them about 3-4x slower
    /// than a non-subsignal connection.
    ///
    /// # Examples #
    ///
    /// ## Example: Clustering into  single priority group ##
    ///
    /// In the following example we group together a set of connections. Note
    /// that because `subsignal_1` is created before `subsignal_2`, that any
    /// [emit](Signal::emit) will invoke `subsignal_1` before `subsignal_2`;
    /// this is the same ordering that [connect](Signal::connect) uses.
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw, Signal};
    /// use std::rc::Rc;
    ///
    /// let key = &mut Key::acquire();
    ///
    /// let signal = Signal::new();
    /// let subsignal_1 = signal.subsignal(key);
    /// let subsignal_2 = signal.subsignal(key);
    ///
    /// let item = Rc::new(KeyCell::new(String::new(), ()));
    ///
    /// subsignal_1.connect(key, &item, |this, event| {
    ///     **this += "a";
    /// });
    ///
    /// subsignal_2.connect(key, &item, |this, event| {
    ///     **this += "c";
    /// });
    ///
    /// // Even though this is connected later, it's connected to subsignal_1, which is ordered
    /// // before subsignal_2, so this will run before any connection to subsignal_2.
    /// subsignal_1.connect(key, &item, |this, event| {
    ///     **this += "b";
    /// });
    ///
    /// signal.emit(key, ());
    ///
    /// assert_eq!(*item.ro(key), "abc");
    /// ```
    ///
    /// ## Example: Fast disconnection ##
    ///
    /// [Connection::disconnect] is an `O(N)` operation. As such, frequently
    /// connecting and disconnecting a connection to a significantly
    /// populated [Signal] would lead to slowdowns.
    ///
    /// To remedy this, we can use a subsignal to hold the frequently changing
    /// set of connections.
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw, Signal};
    /// use std::rc::Rc;
    ///
    /// let key = &mut Key::acquire();
    ///
    /// let signal: Signal<()> = Signal::new();
    ///
    /// // Without the `subsignal(key)` call here, the last loop in
    /// // this example runs much slower.
    /// let frequently_changing = signal.before(key).subsignal(key);
    ///
    /// let item = Rc::new(KeyCell::new(String::new(), ()));
    ///
    /// for _ in 0..1_000 {
    ///     signal.connect(key, &item, |_, _| {});
    /// }
    ///
    /// for _ in 0..1_000 {
    ///     // This is fast because the subsignal contains at most 1 element.
    ///     let conn = frequently_changing.connect(key, &item, |_, _| {});
    ///     conn.disconnect(key);
    /// }
    /// ```
    pub fn subsignal(&self, key: &mut Key) -> Self {
        let index = self.index.value();
        let inner = self.inner.rw(key).subsignal(index);

        Self {
            inner,
            index: FractionalIndex::new(),
        }
    }

    fn subsignal_at(&self, key: &mut Key, index: FractionalIndexType) -> Self {
        debug_assert!(index % 2 == 1);

        if let Some(subsignal) = self.inner.rw(key).preexisting_subsignal(index) {
            return subsignal;
        }

        let inner = self.inner.rw(key).subsignal(index);

        let subsignal = Self {
            inner,
            index: FractionalIndex::new(),
        };

        self.inner
            .rw(key)
            .register_subsignal(subsignal.clone(), index);

        subsignal
    }

    /// Emits a value to all receivers.
    ///
    /// Since [KeyCell] permits recursive borrowing, it is possible to call
    /// [Signal::emit] recursively. If that happens, the most deeply nested
    /// `emit` call invalidates the preceding emit call. Once the call stack
    /// returns to the preceding `emit` call, we immediately return from it. We
    /// do this because we can assume that subsequent emits have more
    /// up-to-date information to emit.
    ///
    /// This allows us to add a receiver that effectively modifies the data and
    /// re-emits.
    ///
    /// # Examples #
    ///
    /// ## Plain emitting ##
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Signal};
    /// use std::rc::Rc;
    ///
    /// let key = &mut Key::acquire();
    /// let signal = Signal::new();
    /// let receiver = Rc::new(KeyCell::new(String::new(), ()));
    ///
    /// signal.connect(key, &receiver, |this, value| {
    ///     **this += &format!("Number: {}\n", value);
    /// });
    ///
    /// // Both call-by-moving and by-reference work.
    /// signal.emit(key, 123);
    /// signal.emit(key, &456);
    ///
    /// assert_eq!(*receiver.ro(key), "Number: 123\nNumber: 456\n");
    /// ```
    ///
    /// ## Nested emits ##
    ///
    /// A signal handler can call `emit` on the same signal that invoked the
    /// handler. In those cases, the outermost `emit` will return early upon
    /// the signal handler returning. The nested `emit` call will run all
    /// receivers as if it were calling it as the outer `emit`. From any
    /// call to `emit`, we are thus guaranteed that all receivers are called.
    ///
    /// In the example here, the outer emit will effectively observe that
    /// receivers are called with the argument `0` until `b` is reached.
    /// After that, the emit resets to `1` and all receivers
    /// are again executed with this new argument. `C(0)` is never executed.
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Meta, Signal};
    /// use std::{cell::RefCell, rc::Rc};
    ///
    /// let key = &mut Key::acquire();
    /// let signal = Signal::new();
    ///
    /// let record = Rc::new(RefCell::new(String::new()));
    ///
    /// struct X {
    ///     record: Rc<RefCell<String>>,
    /// }
    ///
    /// impl Meta for X {
    ///     type Data = Signal<i32>;
    /// }
    ///
    /// let receiver = Rc::new(KeyCell::new(
    ///     X {
    ///         record: record.clone(),
    ///     },
    ///     signal.clone(),
    /// ));
    ///
    /// signal.connect(key, &receiver, |this, value| {
    ///     *this.record.borrow_mut() += &format!("A({}) ", value);
    /// });
    ///
    /// signal.connect(key, &receiver, |this, value| {
    ///     *this.record.borrow_mut() += &format!("B({}) ", value);
    ///     if *value == 0 {
    ///         *this.record.borrow_mut() += &format!("RE-EMIT({}) ", value + 1);
    ///         let (cell, key) = Key::split_rw(this);
    ///         cell.meta().emit(key, value + 1);
    ///     }
    /// });
    ///
    /// signal.connect(key, &receiver, |this, value| {
    ///     *this.record.borrow_mut() += &format!("C({}) ", value);
    /// });
    ///
    /// signal.emit(key, 0);
    ///
    /// assert_eq!(*record.borrow(), "A(0) B(0) RE-EMIT(1) A(1) B(1) C(1) ");
    /// ```
    pub fn emit<B>(&self, key: &mut Key, item: B)
    where
        B: Borrow<T>,
    {
        self.inner.rw(key).emit(item.borrow());
    }

    #[cfg(test)]
    const fn test_ordering_depth() -> u32 {
        FractionalIndexType::BITS - 1
    }

    /// Get the amount of subscribers.
    #[cfg(test)]
    pub fn test_subscriber_count(&self, key: &mut Key) -> usize {
        self.inner.ro(key).len()
    }

    #[cfg(test)]
    fn test_ordering_subsignal_count(&self, key: &mut Key) -> usize {
        self.inner.ro(key).ordering_subsignals()
    }
}

impl<T> Clone for Signal<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            index: self.index,
        }
    }
}

impl<T: 'static> Default for Signal<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: 'static> Eq for Signal<T> {}

/// Compares two signals. Equal when the underlying signal object is the same,
/// and their order values are the same.
///
/// # Examples #
///
/// ```
/// use mutcy::{Key, Signal};
///
/// let key = &mut Key::acquire();
///
/// let signal = Signal::<()>::new();
///
/// // Equal cases.
/// assert!(signal == signal);
/// assert!(signal.clone() == signal);
/// assert!(signal.before(key) == signal.before(key));
/// assert!(signal.after(key) == signal.after(key));
/// assert!(signal.after(key) == signal.after(key));
///
/// // Unequal cases.
/// assert!(signal.before(key) != signal);
/// assert!(signal.after(key) != signal);
/// assert!(signal != signal.subsignal(key));
///
/// // Note that subsignals append a new signal object to the current order, so these are not
/// // equal.
/// assert!(signal.subsignal(key) != signal.subsignal(key));
/// ```
impl<T: 'static> PartialEq for Signal<T> {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.inner, &other.inner) && self.index.value() == other.index.value()
    }
}