naga/
racy_lock.rs

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#![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;

/// An alternative to [`LazyLock`] based on [`OnceBox`].
///
/// [`LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html
pub struct RacyLock<T: 'static> {
    inner: OnceBox<T>,
    init: fn() -> T,
}

impl<T: 'static> RacyLock<T> {
    /// Creates a new [`RacyLock`], which will initialize using the provided `init` function.
    pub const fn new(init: fn() -> T) -> Self {
        Self {
            inner: OnceBox::new(),
            init,
        }
    }

    /// Loads the internal value, initializing it if required.
    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()
    }
}