1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
macro_rules! defer { ($code:expr) => { let _cleanup = { struct Cleanup<F: FnMut()>(F); impl<F: FnMut()> Drop for Cleanup<F> { fn drop(&mut self) { (self.0)(); } } Cleanup(move || { $code; }) }; }; }