| 1 | // A poly-fill for `lazy_cell` |
| 2 | // Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stabilized. |
| 3 | |
| 4 | // This isn't used on every platform, which can come up as dead code warnings. |
| 5 | #![allow (dead_code)] |
| 6 | |
| 7 | use std::ops::Deref; |
| 8 | use std::sync::OnceLock; |
| 9 | |
| 10 | pub(crate) struct Lazy<T> { |
| 11 | cell: OnceLock<T>, |
| 12 | init: fn() -> T, |
| 13 | } |
| 14 | |
| 15 | impl<T> Lazy<T> { |
| 16 | pub const fn new(f: fn() -> T) -> Self { |
| 17 | Self { cell: OnceLock::new(), init: f } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<T> Deref for Lazy<T> { |
| 22 | type Target = T; |
| 23 | |
| 24 | #[inline ] |
| 25 | fn deref(&self) -> &'_ T { |
| 26 | self.cell.get_or_init(self.init) |
| 27 | } |
| 28 | } |
| 29 | |