aboutsummaryrefslogtreecommitdiffstats
path: root/src/defer.rs
blob: 4f23ed19dcd260eb78f5c902da6c2fb19a8547b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)*}));
    };
}