#[derive(Debug)]
pub struct Defer<F: FnOnce()> {
f: Option<F>,
}
impl<F: FnOnce()> Defer<F> {
#[inline(always)]
pub const fn new(f: F) -> Self {
Self {
f: Some(f),
}
}
#[inline(always)]
pub fn cancel(&mut self) {
self.f.take();
}
#[inline(always)]
pub fn run(&mut self) -> bool {
if let Some(f) = self.f.take() {
f();
true
} else {
false
}
}
}
impl<F: FnOnce()> Drop for Defer<F> {
#[inline(always)]
fn drop(&mut self) {
self.run();
}
}