1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use core::ops::Deref;
use std::sync::OnceLock;
#[derive(Debug)]
pub struct Lazy<A: Clone, T, E> {
args: A,
f: fn(A) -> Result<T, E>,
ready: OnceLock<Result<T, E>>,
}
impl<A: Clone, T, E> Lazy<A, T, E> {
pub fn new(args: A, f: fn(A) -> Result<T, E>) -> Self {
Self {
args,
f,
ready: OnceLock::new(),
}
}
pub fn get(&self) -> &Result<T, E> {
self.ready.get_or_init(|| (self.f)(self.args.clone()))
}
}
impl<A: Clone, T, E> Deref for Lazy<A, T, E> {
type Target = Result<T, E>;
fn deref(&self) -> &Self::Target {
self.get()
}
}