protobuf/lazy.rs
1use once_cell::sync::OnceCell;
2
3/// Lazily initialized static variable.
4///
5/// Used in generated code.
6///
7/// Currently a wrapper around `once_cell`s `OnceCell`.
8pub struct Lazy<T> {
9 once_cell: OnceCell<T>,
10}
11
12impl<T> Lazy<T> {
13 /// Uninitialized state.
14 pub const fn new() -> Lazy<T> {
15 Lazy {
16 once_cell: OnceCell::new(),
17 }
18 }
19
20 /// Lazily initialize the value.
21 pub fn get(&self, f: impl FnOnce() -> T) -> &T {
22 self.once_cell.get_or_init(f)
23 }
24}