scoped_gc/
lib.rs

1#![feature(dropck_eyepatch)]
2
3/// This module lets you create garbage-collected scopes
4///
5/// ```compile_fail
6/// use scoped_gc::{Gc, GcScope, Trace};
7///
8/// pub struct NamedObject {
9///   pub name: String,
10/// }
11///
12/// unsafe impl Trace for NamedObject {
13///   unsafe fn mark(&self) {}
14///   unsafe fn root(&self) {}
15///   unsafe fn unroot(&self) {}
16/// }
17///
18/// fn main() {
19///   let message: Gc<NamedObject>;
20///   {
21///     let scope: GcScope = GcScope::new();
22///     message = scope.alloc(NamedObject { name: String::from("Hello, World!") }).unwrap();
23///   }
24///   println!("{}", message.name);
25/// }
26/// ```
27///
28/// ```compile_fail
29/// use scoped_gc::{Gc, GcScope, Trace};
30///
31/// pub struct RefNamedObject<'a> {
32///   pub name: &'a str,
33/// }
34///
35/// unsafe impl<'a> Trace for RefNamedObject<'a> {
36///   unsafe fn mark(&self) {}
37///   unsafe fn root(&self) {}
38///   unsafe fn unroot(&self) {}
39/// }
40///
41/// fn main() {
42///   let scope: GcScope = GcScope::new();
43///   let message: Gc<RefNamedObject>;
44///   {
45///     let hello_world: String = String::from("Hello, World!");
46///     message = scope.alloc(RefNamedObject { name: &hello_world }).unwrap();
47///   }
48/// }
49/// ```
50///
51/// ```compile_fail
52/// // Check that the drop order between the GC scope and values is enforced.
53///
54/// use scoped_gc::{Gc, GcScope, Trace};
55///
56/// pub struct NamedObject {
57///   pub name: String,
58/// }
59///
60/// unsafe impl Trace for NamedObject {
61///   unsafe fn mark(&self) {}
62///   unsafe fn root(&self) {}
63///   unsafe fn unroot(&self) {}
64/// }
65///
66/// fn main() {
67///   let mut stack: Vec<Gc<NamedObject>> = Vec::new();
68///   let scope: GcScope = GcScope::new();
69///   stack.push(scope.alloc(NamedObject { name: String::from("Hello, World!") }).unwrap())
70///   // `scope` is dropped first and frees the `NamedObject`
71///   // `stack` is dropped second, but it contains a `Gc`!
72///   // It will try to decrement the root count of an already freed value
73/// }
74/// ```
75
76mod gc;
77mod gc_alloc_err;
78mod gc_box;
79mod gc_ref_cell;
80mod gc_scope;
81mod trace;
82
83#[cfg(test)]
84mod test;
85
86pub use gc::Gc;
87pub use gc_alloc_err::GcAllocErr;
88pub use gc_ref_cell::{GcRef, GcRefCell, GcRefMut};
89pub use gc_scope::GcScope;
90pub use trace::Trace;