protobuf/
lazy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use once_cell::sync::OnceCell;

/// Lazily initialized static variable.
///
/// Used in generated code.
///
/// Currently a wrapper around `once_cell`s `OnceCell`.
pub struct Lazy<T> {
    once_cell: OnceCell<T>,
}

impl<T> Lazy<T> {
    /// Uninitialized state.
    pub const fn new() -> Lazy<T> {
        Lazy {
            once_cell: OnceCell::new(),
        }
    }

    /// Lazily initialize the value.
    pub fn get(&self, f: impl FnOnce() -> T) -> &T {
        self.once_cell.get_or_init(f)
    }
}