✨ cron
This commit is contained in:
parent
18c663fcdb
commit
46cb21dc2a
8 changed files with 575 additions and 6 deletions
14
src/defer.rs
14
src/defer.rs
|
@ -1,16 +1,22 @@
|
|||
use std::mem::take;
|
||||
|
||||
pub struct Defer {
|
||||
f: Box<dyn Fn()>,
|
||||
f: Option<Box<dyn FnOnce()>>,
|
||||
}
|
||||
|
||||
impl Defer {
|
||||
pub fn new<F: Fn() + 'static>(f: F) -> Self {
|
||||
Self { f: Box::new(f) }
|
||||
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");
|
||||
self.f.as_ref()();
|
||||
if let Some(f) = take(&mut self.f) {
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue