2024-02-25 08:19:27 -08:00
|
|
|
// A poly-fill for `lazy_cell`
|
2024-04-27 21:29:11 +08:00
|
|
|
// Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stabilized.
|
2024-02-25 08:19:27 -08:00
|
|
|
|
|
|
|
|
// This isn't used on every platform, which can come up as dead code warnings.
|
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
|
|
|
|
|
pub(crate) struct Lazy<T> {
|
|
|
|
|
cell: OnceLock<T>,
|
|
|
|
|
init: fn() -> T,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Lazy<T> {
|
|
|
|
|
pub const fn new(f: fn() -> T) -> Self {
|
|
|
|
|
Self { cell: OnceLock::new(), init: f }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Deref for Lazy<T> {
|
|
|
|
|
type Target = T;
|
2024-04-26 19:11:44 +04:00
|
|
|
|
2024-02-25 08:19:27 -08:00
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &'_ T {
|
|
|
|
|
self.cell.get_or_init(self.init)
|
|
|
|
|
}
|
|
|
|
|
}
|