pub trait Finalize {
fn finalize(&self) {}
}
#[cfg(feature = "nightly")]
impl<T: ?Sized> Finalize for T {
#[inline]
default fn finalize(&self) {}
}
pub unsafe trait Trace: Finalize {
unsafe fn trace(&self);
unsafe fn root(&self);
unsafe fn unroot(&self);
fn finalize_glue(&self);
}
#[macro_export]
macro_rules! unsafe_empty_trace {
() => {
#[inline]
unsafe fn trace(&self) {}
#[inline]
unsafe fn root(&self) {}
#[inline]
unsafe fn unroot(&self) {}
#[inline]
fn finalize_glue(&self) {
$crate::Finalize::finalize(self)
}
}
}
#[macro_export]
macro_rules! custom_trace {
($this:ident, $body:expr) => {
#[inline]
unsafe fn trace(&self) {
#[inline]
unsafe fn mark<T: $crate::Trace>(it: &T) {
$crate::Trace::trace(it);
}
let $this = self;
$body
}
#[inline]
unsafe fn root(&self) {
#[inline]
unsafe fn mark<T: $crate::Trace>(it: &T) {
$crate::Trace::root(it);
}
let $this = self;
$body
}
#[inline]
unsafe fn unroot(&self) {
#[inline]
unsafe fn mark<T: $crate::Trace>(it: &T) {
$crate::Trace::unroot(it);
}
let $this = self;
$body
}
#[inline]
fn finalize_glue(&self) {
$crate::Finalize::finalize(self);
#[inline]
fn mark<T: $crate::Trace>(it: &T) {
$crate::Trace::finalize_glue(it);
}
let $this = self;
$body
}
}
}
impl<T: ?Sized> Finalize for &'static T {}
unsafe impl<T: ?Sized> Trace for &'static T {
unsafe_empty_trace!();
}
macro_rules! simple_empty_finalize_trace {
($($T:ty),*) => {
$(
impl Finalize for $T {}
unsafe impl Trace for $T { unsafe_empty_trace!(); }
)*
}
}
simple_empty_finalize_trace![usize, bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, String];
impl<T: Trace> Finalize for Box<T> {}
unsafe impl<T: Trace> Trace for Box<T> {
custom_trace!(this, {
mark(&**this);
});
}
impl<T: Trace> Finalize for Vec<T> {}
unsafe impl<T: Trace> Trace for Vec<T> {
custom_trace!(this, {
for e in this {
mark(e);
}
});
}
impl<T: Trace> Finalize for Option<T> {}
unsafe impl<T: Trace> Trace for Option<T> {
custom_trace!(this, {
if let Some(ref v) = *this {
mark(v);
}
});
}