Skip to main content

read_fonts/model/
once.rs

1//! Choose between std::sync::OnceLock and once_cell::race::OnceBox based on the
2//! `std` feature.
3
4#[cfg(feature = "std")]
5pub(crate) type Once<T> = std::sync::OnceLock<T>;
6
7#[cfg(not(feature = "std"))]
8pub(crate) use once_impl::Once;
9
10#[cfg(not(feature = "std"))]
11mod once_impl {
12    use alloc::boxed::Box;
13    use once_cell::race::OnceBox;
14
15    #[derive(Default)]
16    pub struct Once<T>(OnceBox<T>);
17
18    impl<T> Once<T> {
19        pub const fn new() -> Self {
20            Self(OnceBox::new())
21        }
22
23        pub fn get_or_init(&self, f: impl FnOnce() -> T) -> &T {
24            self.0.get_or_init(|| Box::new(f()))
25        }
26    }
27}