winit/
utils.rs

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
7use std::ops::Deref;
8use std::sync::OnceLock;
9
10pub(crate) struct Lazy<T> {
11    cell: OnceLock<T>,
12    init: fn() -> T,
13}
14
15impl<T> Lazy<T> {
16    pub const fn new(f: fn() -> T) -> Self {
17        Self { cell: OnceLock::new(), init: f }
18    }
19}
20
21impl<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}