You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
leanify-many/src/defer.rs

40 lines
619 B

//! Defer a close to be ran on drop.
use std::{
ops::Drop,
};
pub struct Defer<F>(Option<F>)
where F: FnOnce() -> ();
impl<F> Defer<F>
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<F> Drop for Defer<F>
where F: FnOnce()
{
fn drop(&mut self)
{
self.0.take().unwrap()();
}
}