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::any::Any;
8use std::ops::Deref;
9use std::sync::OnceLock;
10
11pub(crate) struct Lazy<T> {
12    cell: OnceLock<T>,
13    init: fn() -> T,
14}
15
16impl<T> Lazy<T> {
17    pub const fn new(f: fn() -> T) -> Self {
18        Self { cell: OnceLock::new(), init: f }
19    }
20}
21
22impl<T> Deref for Lazy<T> {
23    type Target = T;
24
25    #[inline]
26    fn deref(&self) -> &'_ T {
27        self.cell.get_or_init(self.init)
28    }
29}
30
31pub trait AsAny {
32    fn as_any(&self) -> &dyn Any;
33    fn as_any_mut(&mut self) -> &mut dyn Any;
34}
35
36impl<T: Any> AsAny for T {
37    #[inline(always)]
38    fn as_any(&self) -> &dyn Any {
39        self
40    }
41
42    #[inline(always)]
43    fn as_any_mut(&mut self) -> &mut dyn Any {
44        self
45    }
46}