//! Defer a close to be ran on drop. use std::{ ops::Drop, }; pub struct Defer(Option) where F: FnOnce() -> (); impl Defer where F: FnOnce() { #[cfg(nightly)] #[inline] pub const fn new(from: F) -> Self { Self(Some(from)) } #[cfg(not(nightly))] #[inline] pub fn new(from: F) -> Self { Self(Some(from)) } pub fn forget(mut self) -> F { let value = self.0.take(); std::mem::forget(self); value.unwrap() } #[inline] pub fn now(self) {} } impl Drop for Defer where F: FnOnce() { fn drop(&mut self) { self.0.take().unwrap()(); } }