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].
pub use ;
use ;
thread_local!
/// 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 ;
/// 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 ;
/// 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`.
>);
/// A `RefCell`-like wrapper that can only be accessed via
/// [Key].
;
/// Wraps an immutably borrowed reference to a value in a `KeyCell`.
///
/// See [KeyCell::ro].
;
/// Wraps a mutably borrowed reference to a value in a `KeyCell`.
///
/// See [KeyCell::rw].
;
/// Converts [Rc] or [Weak] into a `Weak`.
///
/// Used to make the API take either an `Rc` or a `Weak`.