pub struct Defer<T: FnOnce()>(pub Option<T>);

impl<T: FnOnce()> Drop for Defer<T> {
    fn drop(&mut self) {
        if let Some(f) = self.0.take() {
            f();
        }
    }
}

#[macro_export]
macro_rules! defer {
    ($($x:tt)*) => {
        let _defer = $crate::defer::Defer(Some(move || {$($x)*}));
    };
}
