naga/
racy_lock.rs
#![cfg_attr(
not(any(glsl_out, hlsl_out, msl_out, feature = "wgsl-in", wgsl_out)),
expect(
dead_code,
reason = "RacyLock is only required for the above configurations"
)
)]
use alloc::boxed::Box;
use once_cell::race::OnceBox;
pub struct RacyLock<T: 'static> {
inner: OnceBox<T>,
init: fn() -> T,
}
impl<T: 'static> RacyLock<T> {
pub const fn new(init: fn() -> T) -> Self {
Self {
inner: OnceBox::new(),
init,
}
}
pub fn get(&self) -> &T {
self.inner.get_or_init(|| Box::new((self.init)()))
}
}
impl<T: 'static> core::ops::Deref for RacyLock<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.get()
}
}