Crates.io | broom |
lib.rs | broom |
version | 0.3.2 |
source | src |
created_at | 2020-03-23 18:21:09.91599 |
updated_at | 2022-10-23 00:35:19.486893 |
description | An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection |
homepage | |
repository | https://github.com/zesterer/broom |
max_upload_size | |
id | 221845 |
size | 26,505 |
An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection.
use broom::prelude::*;
// The type you want the heap to contain
pub enum Object {
Num(f64),
List(Vec<Handle<Self>>),
}
// Tell the garbage collector how to explore a graph of this object
impl Trace<Self> for Object {
fn trace(&self, tracer: &mut Tracer<Self>) {
match self {
Object::Num(_) => {},
Object::List(objects) => objects.trace(tracer),
}
}
}
// Create a new heap
let mut heap = Heap::default();
// Temporary objects are cheaper than rooted objects, but don't survive heap cleans
let a = heap.insert_temp(Object::Num(42.0));
let b = heap.insert_temp(Object::Num(1337.0));
// Turn the numbers into a rooted list
let c = heap.insert(Object::List(vec![a, b]));
// Change one of the numbers - this is safe, even if the object is self-referential!
*heap.get_mut(a).unwrap() = Object::Num(256.0);
// Create another number object
let d = heap.insert_temp(Object::Num(0.0));
// Clean up unused heap objects
heap.clean();
// a, b and c are all kept alive because c is rooted and a and b are its children
assert!(heap.contains(a));
assert!(heap.contains(b));
assert!(heap.contains(c));
// Because `d` was temporary and unused, it did not survive the heap clean
assert!(!heap.contains(d));
This crate makes no specific promises about performance. It is designed with a 'best attempt' approach; this means that it should be fast enough for most purposes but is probably not competitive with garbage collectors that have had years of development work ploughed into them.
There are a few things I want to do with broom
if I get the time:
If you're interested in working on any of these things, feel free to open a pull request!
Broom is licensed under either of:
Apache License 2.0, (http://www.apache.org/licenses/LICENSE-2.0)
MIT license (http://opensource.org/licenses/MIT)