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
//! # Zero-Cost Mutable Cyclic Borrows #
//!
//! A `RefCell`-like dynamic borrowing system that permits recursive borrowing
//! with zero runtime overhead.
//!
//! This crate implements [KeyCell] of which only a single instance can be
//! mutably borrowed at a time using a [Key]. Simultaneous immutable borrows
//! are permitted.
//!
//! `Key` is a ZST, and only a single instance can exist per thread. The only
//! dynamic check this library performs is upon calling [Key::acquire] to ensure
//! that there exists no other instance in the same thread. This ensures that we
//! can only mutably borrow one `KeyCell` per thread.
//!
//! Once we have a mutable borrow of a `KeyCell` we can relinquish this borrow
//! temporarily using [Key::split_rw] to get
//! `&mut Key`, and borrow other `KeyCell`s. This is what allows us to implement
//! recursive calls with mutable access.
//!
//! # Comparison to `RefCell` #
//!
//! Unlike `RefCell`, borrows on [KeyCell]s will never fail. They also don't
//! need to perform any runtime checks. All borrow checking is performed at
//! compile time.
//!
//! 1. Borrowing is zero-cost.
//! 2. Borrowing will never fail or panic.
//! 3. Only a single `KeyCell` can be mutably borrowed per thread.
//!
//! # Alternatives #
//!
//! The core idea in this crate relies on acquiring mutable borrows through a
//! unique object (the [Key]). The same idea is implemented in a few other
//! crates.
//!
//! - [ghost-cell](https://crates.io/crates/ghost-cell)
//! - [qcell](https://crates.io/crates/qcell)
//! - [token-cell](https://crates.io/crates/token-cell)
//! - [frankencell](https://crates.io/crates/frankencell)
//!
//! # Examples #
//!
//! This crate uses [ro](KeyCell::ro) and
//! [rw](KeyCell::rw) instead of `borrow` and `borrow_mut` to access data.
//!
//! ```
//! use mutcy::{Key, KeyCell, Rw};
//!
//! let mut key = Key::acquire();
//!
//! let kc1 = KeyCell::new(0i32, ());
//! let kc2 = KeyCell::new(String::new(), ());
//!
//! *kc2.rw(&mut key) += "Hello";
//! *kc1.rw(&mut key) += 1;
//! *kc2.rw(&mut key) += "World";
//!
//! let item1 = kc1.ro(&key);
//! let item2 = kc1.ro(&key);
//!
//! println!("{} - {}", *item1, *item2);
//! ```
//!
//! With this library it's possible to define methods that take [Rw] and
//! transfer mutability to other [KeyCell]s when needed. The compile-time borrow
//! checker ensures that no mutable aliasing occurs.
//!
//! In the following example we show how a struct can accept a `self:
//! Rw<Self>` and relinquish its own borrows to access some other
//! `KeyCell`.
//!
//! ```
//! #![feature(arbitrary_self_types)]
//! use mutcy::{Key, KeyCell, Meta, Rw};
//! use std::rc::Rc;
//!
//! struct MyStruct {
//!     value: i32,
//! }
//!
//! impl MyStruct {
//!     fn my_function(self: Rw<Self>) {
//!         self.value += 1;
//!
//!         // This relinquishes any borrows to `self`.
//!         let (this, key) = Key::split_rw(self);
//!
//!         // We can now access any other KeyCell using `key`.
//!         let mut string = this.meta().other.rw(key);
//!         *string += "Hello world";
//!
//!         self.value += 1;
//!     }
//! }
//!
//! struct MyStructMeta {
//!     other: Rc<KeyCell<String>>,
//! }
//!
//! impl Meta for MyStruct {
//!     type Data = MyStructMeta;
//! }
//!
//! let mut key = Key::acquire();
//!
//! let other = Rc::new(KeyCell::new(String::new(), ()));
//!
//! let my_struct = KeyCell::new(
//!     MyStruct { value: 0 },
//!     MyStructMeta {
//!         other: other.clone(),
//!     },
//! );
//!
//! my_struct.rw(&mut key).my_function();
//!
//! println!("{}", *other.ro(&key));
//! println!("{}", my_struct.ro(&key).value);
//! ```
//!
//! # Metadata #
//!
//! [KeyCell::new] takes two arguments, the primary data to wrap, and metadata.
//! Metadata can always be accessed immutably as it does not require a [Key].
//!
//! This allows us to reference and access other [KeyCell]s without having to
//! clone an `Rc<KeyCell<_>>` out of the initial item.
//!
//! For more details see [Meta].
#![feature(arbitrary_self_types)]
#![deny(
    missing_docs,
    rustdoc::broken_intra_doc_links,
    unused_imports,
    unused_qualifications
)]

pub use self::{
    callback::Callback,
    meta::Meta,
    signal::{Connection, Signal},
};
use std::{
    cell::{Cell, UnsafeCell},
    marker::PhantomData,
    ops::{Deref, DerefMut},
    rc::{Rc, Weak},
};

mod callback;
mod meta;
mod signal;

thread_local! {
    static KEY: Cell<bool> = const { Cell::new(true) }
}

/// Immutable reference to [RoGuard].
///
/// Convenience wrapper that allows the creation of methods that take `Ro`
/// without specifying lifetimes.
///
/// # Examples #
///
/// ```
/// #![feature(arbitrary_self_types)]
/// use mutcy::{Meta, Ro};
///
/// struct X;
///
/// impl Meta for X {
///     type Data = ();
/// }
///
/// impl X {
///     fn my_function(self: Ro<Self>) {
///         // ...
///     }
/// }
/// ```
pub type Ro<'a, 'b, T> = &'a RoGuard<'b, T>;

/// Mutable reference to [RwGuard].
///
/// Convenience wrapper that allows the creation of methods that take `Rw`
/// without specifying lifetimes.
///
/// # Examples #
///
/// ```
/// #![feature(arbitrary_self_types)]
/// use mutcy::{Meta, Rw};
///
/// struct X;
///
/// impl Meta for X {
///     type Data = ();
/// }
///
/// impl X {
///     fn my_function(self: Rw<Self>) {
///         // ...
///     }
/// }
/// ```
pub type Rw<'a, 'b, T> = &'a mut RwGuard<'b, T>;

/// A per-thread unique key used to access data inside [KeyCell]s.
///
/// This type enforces the existence of only a single instance of itself per
/// thread. By doing so it guards against mutable aliasing when borrowing from
/// `KeyCell`.
pub struct Key(PhantomData<*mut ()>);

impl Key {
    /// Acquire the thread-local Key.
    ///
    /// # Panics #
    ///
    /// Panics if an instance already exists for the current thread. Calling
    /// acquire after dropping the key will not panic.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::Key;
    ///
    /// let key = Key::acquire();
    /// ```
    pub fn acquire() -> Self {
        KEY.with(|key| {
            assert!(key.get());
            key.set(false);
        });

        Key(PhantomData)
    }

    /// Split an [Ro] into its consitutuent [KeyCell] and immutable [Key].
    ///
    /// Similar to [split_rw](Key::split_rw). Unlike `split_rw`, the `Ro`
    /// remains accessible because multiple simultaneous immutable borrows
    /// are allowed.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Ro};
    ///
    /// fn function(kc1: Ro<i32>, kc2: &KeyCell<i32>) {
    ///     let (_, key) = Key::split_ro(kc1);
    ///     println!("{}", **kc1);
    ///     println!("{}", *kc2.ro(key));
    /// }
    ///
    /// let kc1 = KeyCell::new(0, ());
    /// let kc2 = KeyCell::new(10, ());
    ///
    /// let mut key = Key::acquire();
    ///
    /// let mut kc1_borrow = kc1.ro(&mut key);
    ///
    /// function(&kc1_borrow, &kc2);
    /// ```
    pub fn split_ro<'a: 'b, 'b, T: Meta>(item: &'b RoGuard<'a, T>) -> (&'b KeyCell<T>, &'b Key) {
        RoGuard::split(item)
    }

    /// Split an [Rw] into its consitutuent [KeyCell] and mutable [Key].
    ///
    /// Whenever we want to borrow another item while already holding a `Rw`
    /// we need to unborrow. This function unborrows, granting you access to
    /// the key that was used to borrow the initial `KeyCell`.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell, Rw};
    ///
    /// fn function(kc1: Rw<i32>, kc2: &KeyCell<i32>) {
    ///     **kc1 += 1; // kc1 = 1
    ///
    ///     let (kc1_ref, key) = Key::split_rw(kc1);
    ///
    ///     let mut kc2_mut = kc2.rw(key);
    ///     *kc2_mut += 1;
    ///
    ///     // Relinquish the split.
    ///     **kc1 += 1; // kc1 = 2;
    ///
    ///     // Compile error.
    ///     // *kc2_mut += 1;
    /// }
    ///
    /// let kc1 = KeyCell::new(0, ());
    /// let kc2 = KeyCell::new(10, ());
    ///
    /// let mut key = Key::acquire();
    ///
    /// let mut kc1_borrow = kc1.rw(&mut key);
    ///
    /// function(&mut kc1_borrow, &kc2);
    /// ```
    pub fn split_rw<'a: 'b, 'b, T: Meta>(
        item: &'b mut RwGuard<'a, T>,
    ) -> (&'b KeyCell<T>, &'b mut Key) {
        RwGuard::split(item)
    }
}

impl Drop for Key {
    /// Drops the key. Once the key is dropped, the next [acquire](Key::acquire)
    /// will not panic.
    fn drop(&mut self) {
        KEY.with(|key| {
            key.set(true);
        });
    }
}

/// A `RefCell`-like wrapper that can only be accessed via
/// [Key].
pub struct KeyCell<T: Meta>(UnsafeCell<T>, T::Data);

impl<T: Meta> KeyCell<T> {
    /// Creates a new `KeyCell` from an object and its associated
    /// [metadata](Meta).
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::KeyCell;
    ///
    /// let kc = KeyCell::new(123, ());
    /// ```
    pub fn new(item: T, meta: T::Data) -> Self {
        Self(UnsafeCell::new(item), meta)
    }

    /// Immutably borrows the wrapped value.
    ///
    /// Requires a `&Key` to ensure no mutable borrow exists.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell};
    ///
    /// let key = Key::acquire();
    /// let kc = KeyCell::new(123, ());
    ///
    /// let borrowed1 = kc.ro(&key);
    /// let borrowed2 = kc.ro(&key);
    /// ```
    pub fn ro<'a>(&'a self, key: &'a Key) -> RoGuard<'a, T> {
        RoGuard(self, key)
    }

    /// Mutably borrows the wrapped value.
    ///
    /// Requires a `&mut Key` that ensures that no other borrows exist
    /// simultaneously.
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{Key, KeyCell};
    ///
    /// let mut key = Key::acquire();
    /// let kc = KeyCell::new(123, ());
    ///
    /// let borrowed1 = kc.rw(&mut key);
    ///
    /// let borrowed2 = kc.ro(&key);
    ///
    /// // This does not compile.
    /// // let _ = *borrowed1;
    /// ```
    pub fn rw<'a>(&'a self, key: &'a mut Key) -> RwGuard<'a, T> {
        RwGuard(self, key)
    }

    /// Acquire metadata. See [Meta].
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{KeyCell, Meta};
    /// struct A;
    ///
    /// impl Meta for A {
    ///     type Data = i32;
    /// }
    ///
    /// let kc = KeyCell::new(A, 123);
    ///
    /// assert_eq!(*kc.meta(), 123);
    /// ```
    pub fn meta(&self) -> &T::Data {
        &self.1
    }

    /// Acquire mutable reference to metadata. See [Meta].
    ///
    /// # Examples #
    ///
    /// ```
    /// use mutcy::{KeyCell, Meta};
    /// struct A;
    ///
    /// impl Meta for A {
    ///     type Data = i32;
    /// }
    ///
    /// let mut kc = KeyCell::new(A, 123);
    ///
    /// *kc.meta_mut() = 100;
    /// assert_eq!(*kc.meta(), 100);
    /// ```
    pub fn meta_mut(&mut self) -> &mut T::Data {
        &mut self.1
    }
}

/// Wraps an immutably borrowed reference to a value in a `KeyCell`.
///
/// See [KeyCell::ro].
pub struct RoGuard<'a, T: Meta>(&'a KeyCell<T>, &'a Key);

impl<'a, T: Meta> RoGuard<'a, T> {
    fn split(this: &Self) -> (&KeyCell<T>, &Key) {
        (this.0, this.1)
    }
}

impl<'a, T: Meta> Deref for RoGuard<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.0.0.get() }
    }
}

/// Wraps a mutably borrowed reference to a value in a `KeyCell`.
///
/// See [KeyCell::rw].
pub struct RwGuard<'a, T: Meta>(&'a KeyCell<T>, &'a mut Key);

impl<'a, T: Meta> RwGuard<'a, T> {
    fn split(this: &mut Self) -> (&KeyCell<T>, &mut Key) {
        (this.0, this.1)
    }
}

impl<'a, T: Meta> Deref for RwGuard<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.0.0.get() }
    }
}

impl<'a, T: Meta> DerefMut for RwGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.0.0.get() }
    }
}

/// Converts [Rc] or [Weak] into a `Weak`.
///
/// Used to make the API take either an `Rc` or a `Weak`.
pub trait IntoWeak<T: Meta> {
    /// Perform the conversion.
    fn into(&self) -> Weak<KeyCell<T>>;
}

impl<T: Meta> IntoWeak<T> for Rc<KeyCell<T>> {
    fn into(&self) -> Weak<KeyCell<T>> {
        Rc::downgrade(self)
    }
}

impl<T: Meta> IntoWeak<T> for Weak<KeyCell<T>> {
    fn into(&self) -> Weak<KeyCell<T>> {
        self.clone()
    }
}