use std::mem::take; pub struct Defer { f: Option>, } impl Defer { pub fn new(f: F) -> Self { Self { f: Some(Box::new(f)), } } } impl Drop for Defer { fn drop(&mut self) { log::debug!("Calling defer function"); if let Some(f) = take(&mut self.f) { f(); } } }