use super::slot::Slot;
type Build<T, E> = dyn Fn() -> Result<T, E> + Send + Sync;
pub struct Lazy<T, E> {
build: Box<Build<T, E>>,
value: Slot<T>,
}
impl<T, E> Lazy<T, E> {
pub fn new(build: impl Fn() -> Result<T, E> + Send + Sync + 'static) -> Self {
Self {
build: Box::new(build),
value: Slot::empty(),
}
}
pub fn get(&self) -> Result<&T, E> {
self.value.get_or_try_init(|| (self.build)())
}
pub fn built(&self) -> Option<&T> {
self.value.get()
}
}