| Crates.io | shredder |
| lib.rs | shredder |
| version | 0.2.0 |
| created_at | 2020-06-06 05:07:09.150038+00 |
| updated_at | 2021-08-13 23:17:00.71551+00 |
| description | Garbage collection as a library for Rust |
| homepage | |
| repository | https://github.com/Others/shredder |
| max_upload_size | |
| id | 250557 |
| size | 175,414 |
shredder is a library providing a garbage collected smart pointer: Gc.
This is useful for times when you want shared access to some data, but the structure
of the data has unpredictable cycles in it. (So Arc would not be appropriate.)
shredder has the following features:
DerefGc gives you a garbage collected and Deref smart pointer where possibleAtomicGc for cases where you need atomic operationsdrop for 'static datafinalize for non-'static datashredder has the following limitations:
Gc data requires acquiring a guard (although you can use DerefGc in many cases to avoid this)Rc/Arc: requires all Gc objects have straightforward ownership semanticsGc and internal metadata is small, but there is bloat during collection (will fix!)std features (will fix!)Here is an easy example, showing how Gc works:
use std::cell::RefCell;
use shredder::{
number_of_active_handles, number_of_tracked_allocations, run_with_gc_cleanup, Gc, Scan,
};
#[derive(Scan)]
struct Node {
data: String,
directed_edges: Vec<Gc<RefCell<Node>>>,
}
fn main() {
// Using `run_with_gc_cleanup` is good practice, since it helps ensure destructors are run
run_with_gc_cleanup(|| {
let a = Gc::new(RefCell::new(Node {
data: "A".to_string(),
directed_edges: Vec::new(),
}));
let b = Gc::new(RefCell::new(Node {
data: "B".to_string(),
directed_edges: Vec::new(),
}));
// Usually would need `get` for `Gc` data, but `RefCell` is a special case
a.borrow_mut().directed_edges.push(b.clone());
b.borrow_mut().directed_edges.push(a);
// We now have cyclical data!
});
// Everything was cleaned up!
assert_eq!(number_of_tracked_allocations(), 0);
assert_eq!(number_of_active_handles(), 0);
}
If you're playing with this and run into a problem, go ahead and make a Github issue. Eventually there will be a FAQ.
If you're interested in helping with shredder, feel free to reach out.
I'm @Others on the Rust discord. Or just look for an issue marked help wanted or good first issue