22 lines
386 B
Rust
22 lines
386 B
Rust
use std::mem::take;
|
|
|
|
pub struct Defer {
|
|
f: Option<Box<dyn FnOnce()>>,
|
|
}
|
|
|
|
impl Defer {
|
|
pub fn new<F: FnOnce() + 'static>(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();
|
|
}
|
|
}
|
|
}
|