use super::{query::QueryAction, LocalField};
use crate::{
index::{Transaction, TransactionList},
object::{self, AEADReader, Pool},
};
#[non_exhaustive]
#[derive(Clone)]
pub struct Intent<T> {
pub name: String,
pub strategy: T,
}
impl<T> Intent<T> {
#[inline(always)]
pub fn new(name: impl AsRef<str>, strategy: T) -> Self {
Intent {
name: name.as_ref().to_string(),
strategy,
}
}
}
impl<T: Store + 'static> From<Intent<Box<T>>> for Intent<Box<dyn Store>> {
#[inline(always)]
fn from(a: Intent<Box<T>>) -> Self {
Intent {
name: a.name,
strategy: a.strategy,
}
}
}
impl<T: Load + 'static> From<Intent<Box<T>>> for Intent<Box<dyn Load>> {
#[inline(always)]
fn from(a: Intent<Box<T>>) -> Self {
Intent {
name: a.name,
strategy: a.strategy,
}
}
}
pub trait Store {
fn store(&mut self, transaction: &mut dyn Transaction, object: &mut dyn object::Writer);
}
impl<T: Store> Store for LocalField<T> {
fn store(&mut self, transaction: &mut dyn Transaction, object: &mut dyn object::Writer) {
self.field.store(transaction, object)
}
}
pub trait Load {
fn load(&mut self, pool: Pool<AEADReader>, transaction_list: TransactionList);
}
impl<K, T> Load for T
where
T: Query<Key = K>,
{
#[inline(always)]
fn load(&mut self, pool: Pool<AEADReader>, transaction_list: TransactionList) {
Query::select(self, pool, transaction_list, |_| QueryAction::Take)
}
}
pub trait Query {
type Key;
fn select(
&mut self,
pool: Pool<AEADReader>,
transaction_list: TransactionList,
predicate: impl Fn(&Self::Key) -> QueryAction,
);
}