gladiator 0.0.0-alpha

A concurrent, modular and small garbage collector.
Documentation
use gladiator::{trace::Trace, Arena, Gc};

struct GcTest<'arena> {
    id: usize,
    other: Option<Gc<'arena, Self>>,
}

impl<'arena> GcTest<'arena> {
    pub fn new(id: usize) -> Self {
        Self { id, other: None }
    }

    pub fn new_ref(id: usize, other: Gc<'arena, Self>) -> Self {
        Self { id, other: Some(other) }
    }
}

unsafe impl<'arena> Trace<'arena, Self> for GcTest<'arena> {
    unsafe fn send(&self, signal: &gladiator::trace::Signal<'arena, Self>) {
        match &self.other {
            Some(other) => other.send(signal),
            _ => {},
        }
    }

    unsafe fn finalize(&mut self) {
        println!("GcTest#{} finalized", self.id);
    }
}

impl<'arena> Drop for GcTest<'arena> {
    fn drop(&mut self) {
        println!("GcTest#{} dropped", self.id);
    }
}

fn main() {
    let arena = Arena::new();

    {
        arena.host(GcTest::new(1));
        arena.collect(); // note that garbage is collected right away
        println!();
    }

    // circular references
    {
        let ref1 = arena.host(GcTest::new(1));
        let ref2 = arena.host(GcTest::new_ref(2, ref1.as_weak()));
        ref1.borrow_mut().other = Some(ref2.as_weak());
        arena.collect();
        println!("(nothing)");
    }
    arena.collect();
    println!("swept");
}