1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#![no_std]
#![forbid(unsafe_code)]

/// Defer the execution until it get dropped
#[macro_export]
macro_rules! defer {
    ($($body:tt)*) => {
        let _guard = {
            struct Guard<F: FnOnce()>(Option<F>);

            impl<F: FnOnce()> Drop for Guard<F> {
                fn drop(&mut self) {
                    self.0.take().map(|f| f());
                }
            }

            Guard(Some(|| { $($body)* ;}))
        };
    };
}