41 lines
758 B
Rust
41 lines
758 B
Rust
|
|
#[macro_export]
|
|
macro_rules! invoke_once {
|
|
() => {
|
|
unsafe {
|
|
static mut __VALUE: bool = false;
|
|
if __VALUE { false } else { __VALUE = true; true }
|
|
}
|
|
};
|
|
|
|
// if this is used inside of an unsafe codeblock
|
|
// use this branch to avoid the unnecessary unsafe block warning
|
|
(unsafe) => {
|
|
{
|
|
static mut __VALUE: bool = false;
|
|
if __VALUE { false } else { __VALUE = true; true }
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct OnDrop<F: FnOnce()>(Option<F>);
|
|
|
|
impl<F: FnOnce()> OnDrop<F> {
|
|
pub fn new(fun: F) -> Self {
|
|
Self(Some(fun))
|
|
}
|
|
}
|
|
|
|
impl<F: FnOnce()> Drop for OnDrop<F> {
|
|
fn drop(&mut self) {
|
|
if let Some(fun) = self.0.take() {
|
|
fun()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|