bsr 0.11.0

Tracing garbage collector for Amsel
Documentation
//! Contains tracing trait and implementation for tracing garbage collection.

use std::fmt::Debug;

/// A trait for tracing garbage collection.
/// # Safety
/// Impls must be careful to mark all reachable objects or reachable memory may be freed.
/// To enforce this, the [Trace] trait is unsafe.
pub unsafe trait Trace: Debug {
    /// Mark direct children of this object as reachable.
    /// This includes [Gc] pointers and all other types that implement [Trace].
    fn mark_children(&self);
}

/// To allow automatic derived implementations of [Trace], we need to implement [Trace] for all common types.
macro_rules! impl_nop_trace {
    ($ty:ty) => {
        unsafe impl Trace for $ty {
            fn mark_children(&self) {}
        }
    };
    ($( $ty:ty ) ,*) => {
        $( impl_nop_trace!($ty); )*
    };
}

impl_nop_trace!(i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, bool, char, String, ());

/// Implement [Trace] for all references to types that implement [Trace].
unsafe impl<T: Trace> Trace for &T {
    fn mark_children(&self) {
        (*self).mark_children();
    }
}

/// Implement [Trace] for all slices of types that implement [Trace].
unsafe impl<T: Trace> Trace for &[T] {
    fn mark_children(&self) {
        for item in self.iter() {
            item.mark_children();
        }
    }
}