use core::sync::atomic::{AtomicU8, Ordering::Relaxed};
pub(crate) struct LazyBool(AtomicU8);
impl LazyBool {
const UNINIT: u8 = u8::MAX;
pub const fn new() -> Self {
Self(AtomicU8::new(Self::UNINIT))
}
#[cold]
fn cold_init(&self, init: impl FnOnce() -> bool) -> bool {
let val = u8::from(init());
self.0.store(val, Relaxed);
val != 0
}
#[inline]
pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool {
let val = self.0.load(Relaxed);
if val == Self::UNINIT {
return self.cold_init(init);
}
val != 0
}
}