use std::ptr::NonNull;
pub trait Collectible {
fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>>;
#[inline]
fn drop_and_dealloc(&mut self) -> bool {
unsafe { Box::from_raw(self as *mut Self) };
true
}
}
pub(super) struct DeferredClosure<F: 'static + FnOnce() + Sync> {
f: Option<F>,
link: Option<NonNull<dyn Collectible>>,
}
impl<F: 'static + FnOnce() + Sync> DeferredClosure<F> {
#[inline]
pub fn new(f: F) -> DeferredClosure<F> {
DeferredClosure {
f: Some(f),
link: None,
}
}
}
impl<F: 'static + FnOnce() + Sync> Collectible for DeferredClosure<F> {
#[inline]
fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>> {
&mut self.link
}
#[inline]
fn drop_and_dealloc(&mut self) -> bool {
if let Some(f) = self.f.take() {
f();
}
unsafe { Box::from_raw(self as *mut Self) };
true
}
}
pub(super) struct DeferredIncrementalClosure<F: 'static + FnMut() -> bool + Sync> {
f: F,
link: Option<NonNull<dyn Collectible>>,
}
impl<F: 'static + FnMut() -> bool + Sync> DeferredIncrementalClosure<F> {
#[inline]
pub fn new(f: F) -> DeferredIncrementalClosure<F> {
DeferredIncrementalClosure { f, link: None }
}
}
impl<F: 'static + FnMut() -> bool + Sync> Collectible for DeferredIncrementalClosure<F> {
#[inline]
fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>> {
&mut self.link
}
#[inline]
fn drop_and_dealloc(&mut self) -> bool {
if (self.f)() {
unsafe { Box::from_raw(self as *mut Self) };
true
} else {
false
}
}
}