use std::{cell::OnceCell, fmt::Debug, ops::Deref};
#[cfg(doc)]
use crate::FromExpr;
use crate::{IntoExpr, Table, TableRow, Transaction};
pub struct Lazy<'transaction, T: Table> {
pub(crate) id: TableRow<T>,
pub(crate) lazy: OnceCell<Box<T::Lazy<'transaction>>>,
pub(crate) txn: &'transaction Transaction<T::Schema>,
}
#[cfg_attr(feature = "__mutants", mutants::skip)]
impl<T: Table> Debug for Lazy<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Lazy").field(&self.id).finish()
}
}
impl<'transaction, T: Table> Lazy<'transaction, T> {
pub fn table_row(&self) -> TableRow<T> {
self.id
}
}
impl<'transaction, T: Table> Clone for Lazy<'transaction, T> {
fn clone(&self) -> Self {
Self {
id: self.id,
lazy: OnceCell::new(),
txn: self.txn,
}
}
}
impl<'transaction, T: Table> Deref for Lazy<'transaction, T> {
type Target = T::Lazy<'transaction>;
fn deref(&self) -> &Self::Target {
self.lazy.get_or_init(|| {
let select = self.txn.query_one(T::into_select(self.id.into_expr()));
Box::new(T::select_lazy(select))
})
}
}