gladiator 0.0.0-alpha

A concurrent, modular and small garbage collector.
Documentation
use std::marker::PhantomData;

/// The kind of a signal.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum SignalKind {
    /// The value should mark any children values.
    Mark,

    /// The root counter of the value should be incremented.
    Root,

    /// The root counter of the value should be decremented.
    Unroot,
}

/// A signal being sent through [`Trace::send`].
pub struct Signal<'arena, T: Trace<'arena, T>> {
    kind: SignalKind,
    safety: PhantomData<&'arena T>,
}

impl<'arena, T: Trace<'arena, T>> Signal<'arena, T> {
    /// Creates a new signal.
    #[inline]
    pub(crate) fn new(kind: SignalKind) -> Self {
        Self { kind, safety: PhantomData }
    }

    /// Returns the kind of this signal.
    #[inline]
    pub fn kind(&self) -> SignalKind {
        self.kind
    }
}

/// Any object which can be traced by the garbage collector.
pub unsafe trait Trace<'arena, T: Trace<'arena, T>>: 'arena {
    /// Sends a signal to the children of this value.
    #[inline]
    unsafe fn send(&self, _signal: &Signal<'arena, T>) {}

    /// Finalizes this value before it is collected by the garbage collector.
    #[inline]
    unsafe fn finalize(&mut self) {}
}