use std::ptr::NonNull;
pub trait Collectible {
fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>>;
}
pub(super) struct DeferredClosure<F: 'static + FnOnce()> {
f: Option<F>,
link: Option<NonNull<dyn Collectible>>,
}
impl<F: 'static + FnOnce()> DeferredClosure<F> {
#[inline]
pub fn new(f: F) -> Self {
DeferredClosure {
f: Some(f),
link: None,
}
}
}
impl<F: 'static + FnOnce()> Collectible for DeferredClosure<F> {
#[inline]
fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>> {
&mut self.link
}
}
impl<F: 'static + FnOnce()> Drop for DeferredClosure<F> {
#[inline]
fn drop(&mut self) {
if let Some(f) = self.f.take() {
f();
}
}
}