1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Once,
};

pub struct OnceBool {
    start: Once,
    enabled: AtomicBool,
}

impl OnceBool {
    pub const fn new() -> Self {
        Self {
            start: Once::new(),
            enabled: AtomicBool::new(false),
        }
    }

    pub fn get<F: FnOnce() -> bool>(&self, f: F) -> bool {
        self.start.call_once(|| {
            let enabled = f();
            self.enabled.store(enabled, Ordering::SeqCst);
        });

        self.enabled.load(Ordering::SeqCst)
    }
}