From 171617d00ae9ce4256f3393fe5bc2f497a4d788e Mon Sep 17 00:00:00 2001 From: Falk Woldmann Lu <52786457+FalkWoldmann@users.noreply.github.com> Date: Wed, 3 Dec 2025 10:06:11 +0100 Subject: [PATCH] Use LazyLock for std environments and OnceBox for no_std --- naga/src/racy_lock.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/naga/src/racy_lock.rs b/naga/src/racy_lock.rs index fcb70b792..62991d793 100644 --- a/naga/src/racy_lock.rs +++ b/naga/src/racy_lock.rs @@ -1,15 +1,34 @@ +#[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 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 +38,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;