use core::ops::Deref;
use crate::cell::{Block, OnceCell};
#[derive(Debug)]
pub struct Lazy<T, B, F = fn() -> T> {
cell: OnceCell<T, B>,
init: F,
}
impl<T, B, F> Lazy<T, B, F> {
#[inline]
pub const fn new(init: F) -> Self {
Self { cell: OnceCell::new(), init }
}
}
impl<T, B, F> Lazy<T, B, F>
where
B: Block,
F: Fn() -> T,
{
#[inline]
pub fn is_initialized(lazy: &Self) -> bool {
lazy.cell.is_initialized()
}
#[inline]
pub fn is_poisoned(lazy: &Self) -> bool {
lazy.cell.is_poisoned()
}
#[inline]
pub fn get_or_init(lazy: &Self) -> &T {
lazy.cell.get_or_init(|| (lazy.init)())
}
}
impl<T, B, F> Deref for Lazy<T, B, F>
where
B: Block,
F: Fn() -> T,
{
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
Lazy::get_or_init(self)
}
}