diff --git a/naga/src/racy_lock.rs b/naga/src/racy_lock.rs index fcb70b7926b..a4f72197cc6 100644 --- a/naga/src/racy_lock.rs +++ b/naga/src/racy_lock.rs @@ -1,15 +1,35 @@ +#[cfg(no_std)] use alloc::boxed::Box; +#[cfg(no_std)] use once_cell::race::OnceBox; +#[cfg(std)] +use std::sync::LazyLock; -/// An alternative to [`LazyLock`] based on [`OnceBox`]. +#[cfg(std)] +type Inner = LazyLock T>; +#[cfg(no_std)] +type Inner = OnceBox; + +/// Lazy static helper that uses [`LazyLock`] with `std` and [`OnceBox`] otherwise. /// /// [`LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html +/// [`OnceBox`]: https://docs.rs/once_cell/latest/once_cell/race/struct.OnceBox.html pub struct RacyLock { - inner: OnceBox, + inner: Inner, + #[cfg(no_std)] init: fn() -> T, } impl RacyLock { + #[cfg(std)] + /// Creates a new [`RacyLock`], which will initialize using the provided `init` function. + pub const fn new(init: fn() -> T) -> Self { + Self { + inner: LazyLock::new(init), + } + } + + #[cfg(no_std)] /// Creates a new [`RacyLock`], which will initialize using the provided `init` function. pub const fn new(init: fn() -> T) -> Self { Self { @@ -19,6 +39,17 @@ impl RacyLock { } } +#[cfg(std)] +impl core::ops::Deref for RacyLock { + type Target = T; + + /// Loads the internal value, initializing it if required. + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +#[cfg(no_std)] impl core::ops::Deref for RacyLock { type Target = T;