pub trait LazyExecutor<T>
where
T: Send + Sync,
{
fn compute(&mut self) -> T;
#[allow(clippy::all)]
fn new() -> Box<dyn LazyExecutor<T> + Send + Sync>
where
Self: Default + Sized + Sync + Send + 'static,
{
Box::new(Self::default())
}
}
pub struct LazyField<T>
where
T: Send + Sync,
{
pub lazy_executor: Box<dyn LazyExecutor<T> + Send + Sync>,
pub field: T,
pub has_computed: bool,
}
impl<T> LazyField<T>
where
T: Send + Sync,
T: Default + Clone,
{
pub fn new(lazy_executor: Box<dyn LazyExecutor<T> + Send + Sync>) -> Self {
Self {
lazy_executor,
field: T::default(),
has_computed: false,
}
}
pub fn compute(&mut self) -> T {
if self.has_computed {
self.field.clone()
} else {
self.field = self.lazy_executor.compute();
self.has_computed = true;
self.field.clone()
}
}
}