Files
x/src/branching.rs
2024-01-30 02:09:24 -05:00

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()
}
}
}