1
0
Fork 0
forked from wry/wry
wry/src/utils/once.rs
2022-05-04 14:36:18 +02:00

26 lines
488 B
Rust

#![allow(dead_code)]
use std::{cell::Cell, future::Future};
#[derive(Default)]
pub struct Once {
done: Cell<bool>,
}
impl Once {
pub fn set(&self) -> bool {
!self.done.replace(true)
}
pub fn exec<F: FnOnce()>(&self, f: F) {
if !self.done.replace(true) {
f();
}
}
pub async fn exec_async<G: Future<Output = ()>, F: FnOnce() -> G>(&self, f: F) {
if !self.done.replace(true) {
f().await;
}
}
}