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
/*
dumpster, a cycle-tracking garbage collector for Rust. Copyright (C) 2023 Clayton Ramsey.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
//! A cycle-tracking concurrent garbage collector with an easy-to-use API.
//!
//! Most garbage collectors are _tracing_ garbage collectors, meaning that they keep track of a set
//! of roots which are directly accessible from the stack, and then use those roots to find the set
//! of all accessible allocations.
//! However, because Rust does not allow us to hook into when a value is moved, it's quite difficult
//! to detect when a garbage-collected value stops being a root.
//!
//! `dumpster` takes a different approach.
//! It begins by using simple reference counting, then automatically detects cycles.
//! Allocations are freed when their reference count reaches zero or when they are only accessible
//! via their descendants.
//!
//! Garbage-collected pointers can be created and destroyed in _O(1)_ amortized time, but destroying
//! a garbage-collected pointer may take _O(r)_, where _r_ is the number of existing
//! garbage-collected references, on occasion.
//! However, the cleanups that require _O(r)_ performance are performed once every _O(1/r)_ times
//! a reference is dropped, yielding an amortized _O(1)_ runtime.
//!
//! # Why should you use this crate?
//!
//! In short, `dumpster` offers a great mix of usability, performance, and flexibility.
//!
//! - `dumpster`'s API is a drop-in replacement for `std`'s reference-counted shared allocations
//! (`Rc` and `Arc`).
//! - It's very performant and has builtin implementations of both thread-local and concurrent
//! garbage collection.
//! - There are no restrictions on the reference structure within a garbage-collected allocation
//! (references may point in any way you like).
//! - It's trivial to make a custom type Trace using the provided derive macros.
//! - You can even store `?Sized` data in a garbage-collected pointer!
//!
//! # Module structure
//!
//! `dumpster` contains 3 core modules: the root (this module), as well as [`sync`] and [`unsync`].
//! `sync` contains an implementation of thread-safe garbage-collected pointers, while `unsync`
//! contains an implementation of thread-local garbage-collected pointers which cannot be shared
//! across threads.
//! Thread-safety requires some synchronization overhead, so for a single-threaded application,
//! it is recommended to use `unsync`.
//!
//! The project root contains common definitions across both `sync` and `unsync`.
//! Types which implement [`Trace`] can immediately be used in `unsync`, but in order to use
//! `sync`'s garbage collector, the types must also implement [`Sync`].
//!
//! # Examples
//!
//! If your code is meant to run as a single thread, or if your data doesn't need to be shared
//! across threads, you should use [`unsync::Gc`] to store your allocations.
//!
//! ```
//! use dumpster::unsync::Gc;
//! use std::cell::Cell;
//!
//! let my_gc = Gc::new(Cell::new(0451));
//!
//! let other_gc = my_gc.clone(); // shallow copy
//! other_gc.set(512);
//!
//! assert_eq!(my_gc.get(), 512);
//! ```
//!
//! For data which is shared across threads, you can use [`sync::Gc`] with the exact same API.
//!
//! ```
//! use dumpster::sync::Gc;
//! use std::sync::Mutex;
//!
//! let my_shared_gc = Gc::new(Mutex::new(25));
//! let other_shared_gc = my_shared_gc.clone();
//!
//! std::thread::scope(|s| {
//! s.spawn(move || {
//! *other_shared_gc.lock().unwrap() = 35;
//! });
//! });
//!
//! println!("{}", *my_shared_gc.lock().unwrap());
//! ```
//!
//! It's trivial to use custom data structures with the provided derive macro.
//!
//! ```
//! use dumpster::{unsync::Gc, Trace};
//! use std::cell::RefCell;
//!
//! #[derive(Trace)]
//! struct Foo {
//! refs: RefCell<Vec<Gc<Foo>>>,
//! }
//!
//! let foo = Gc::new(Foo {
//! refs: RefCell::new(Vec::new()),
//! });
//!
//! foo.refs.borrow_mut().push(foo.clone());
//!
//! drop(foo);
//!
//! // even though foo had a self reference, it still got collected!
//! ```
//!
//! # Installation
//!
//! To use `dumpster`, add the following lines to your `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! dumpster = "2.1.0"
//! ```
//!
//! # Optional features
//!
//! ## `derive`
//!
//! `derive` is enabled by default.
//! It enables the derive macro for `Trace`, which makes it easy for users to implement their
//! own Trace types.
//!
//! ```
//! use dumpster::{unsync::Gc, Trace};
//! use std::cell::RefCell;
//!
//! #[derive(Trace)] // no manual implementation required
//! struct Foo(RefCell<Option<Gc<Foo>>>);
//!
//! let my_foo = Gc::new(Foo(RefCell::new(None)));
//! *my_foo.0.borrow_mut() = Some(my_foo.clone());
//!
//! drop(my_foo); // my_foo will be automatically cleaned up
//! ```
//!
//! ## `either`
//!
//! `either` is disabled by default. It adds support for the [`either`](https://crates.io/crates/either) crate,
//! specifically by implementing [`Trace`] for [`either::Either`](https://docs.rs/either/1.13.0/either/enum.Either.html).
//!
//! ## `coerce-unsized`
//!
//! `coerce-unsized` is disabled by default.
//! This enables the implementation of [`std::ops::CoerceUnsized`] for each garbage collector,
//! making it possible to use `Gc` with `!Sized` types conveniently.
//! To use `coerce-unsized`, edit your installation to `Cargo.toml` to include the feature.
//!
//! ```toml
//! [dependencies]
//! dumpster = { version = "2.1.0", features = ["coerce-unsized"]}
//! ```
//!
//! ## Loom support
//!
//! `dumpster` has experimental support for permutation testing under [`loom`](https://github.com/tokio-rs/loom).
//! It is expected to be unstable and buggy.
//! To compile `dumpster` using `loom`, add `--cfg loom` to `RUSTFLAGS` when compiling, for example:
//!
//! ```sh
//! RUSTFLAGS='--cfg loom' cargo test
//! ```
//!
//! # License
//!
//! `dumpster` is licensed under the Mozilla Public License, version 2.0.
//! For more details, refer to
//! [LICENSE.md](https://github.com/claytonwramsey/dumpster/blob/master/LICENSE.md).
//!
//! This project includes portions of code derived from the Rust standard library,
//! which is dual-licensed under the MIT and Apache 2.0 licenses.
//! Copyright (c) The Rust Project Developers.
/// Contains the sealed trait for [`Trace`].
/// The trait that any garbage-collected data must implement.
///
/// This trait should usually be implemented by using `#[derive(Trace)]`, using the provided
/// macro.
/// Only data structures using raw pointers or other magic should manually implement `Trace`.
///
/// To manually implement `Trace` you need to implement [`TraceWith<V>`].
/// Any type that implements `TraceWith` for all <code>V: [Visitor]</code>
/// automatically implements `Trace`.
///
/// # Examples
///
/// Implementing `Trace` for a scalar type which contains no garbage-collected references
/// is very easy.
/// Accepting a visitor is simply a no-op.
///
/// ```
/// use dumpster::{TraceWith, Visitor};
///
/// struct Foo(u8);
///
/// unsafe impl<V: Visitor> TraceWith<V> for Foo {
/// fn accept(&self, visitor: &mut V) -> Result<(), ()> {
/// Ok(())
/// }
/// }
/// ```
///
/// However, if a data structure contains a garbage collected pointer, it must delegate to its
/// fields in `accept`.
///
/// ```
/// use dumpster::{unsync::Gc, TraceWith, Visitor};
///
/// struct Bar(Gc<Bar>);
///
/// unsafe impl<V: Visitor> TraceWith<V> for Bar {
/// fn accept(&self, visitor: &mut V) -> Result<(), ()> {
/// self.0.accept(visitor)
/// }
/// }
/// ```
///
/// A data structure with two or more fields which could own a garbage-collected pointer should
/// delegate to both fields in a consistent order:
///
/// ```
/// use dumpster::{unsync::Gc, TraceWith, Visitor};
///
/// struct Baz {
/// a: Gc<Baz>,
/// b: Gc<Baz>,
/// }
///
/// unsafe impl<V: Visitor> TraceWith<V> for Baz {
/// fn accept(&self, visitor: &mut V) -> Result<(), ()> {
/// self.a.accept(visitor)?;
/// self.b.accept(visitor)?;
/// Ok(())
/// }
/// }
/// ```
///
/// `Trace` is dyn-compatible, so you can use it as a subtrait
/// to allocate your own trait object.
///
/// ```
/// use dumpster::{
/// unsync::{coerce_gc, Gc},
/// Trace,
/// };
///
/// trait MyTrait: Trace {}
/// impl<T: Trace> MyTrait for T {}
///
/// let gc: Gc<i32> = Gc::new(5);
/// let gc: Gc<dyn MyTrait> = coerce_gc!(gc);
/// ```
/// The underlying tracing implementation powering the [`Trace`] trait.
///
/// # Safety
///
/// If the implementation of this trait is incorrect, this will result in undefined behavior,
/// typically double-frees or use-after-frees.
/// This includes [`TraceWith::accept`], even though it is a safe function, since its correctness
/// is required for safety.
///
/// The garbage collector in `dumpster` requires strong assumptions about the values inside of a
/// `Gc`; by implementing `TraceWith`, you are responsible for these assumptions.
/// Specifically, in order to be `TraceWith`, a value must have a _tree-like_ ownership structure.
/// If some type `T` implements `TraceWith`, it means that no references to a value inside `T` will
/// remain valid while `T` is moved. For instance, this means that `Rc` can never be `Trace`, as
/// moving one `Rc` will not invalidate other `Rc`s pointing to the same allocation.
/// We allow exceptions for fields of `T` that are not visited by the implementation of
/// [`TraceWith::accept`], such as borrows (see the implementation of `TraceWith` for `&T`) and
/// naturally for [`unsync::Gc`] and [`sync::Gc`].
///
/// Any structure whose implementation of `TraceWith` comes from `#[derive(Trace)]` satisfies the
/// tree-like requirement.
pub unsafe
/// A visitor for a garbage collected value.
///
/// This visitor allows us to hide details of the implementation of the garbage-collection procedure
/// from implementors of [`Trace`].
///
/// When accepted by a `Trace`, this visitor will be delegated down until it reaches a
/// garbage-collected pointer.
/// Then, the garbage-collected pointer will call one of `visit_sync` or `visit_unsync`, depending
/// on which type of pointer it is.
///
/// In general, it's not expected for consumers of this library to write their own visitors.