use std::sync::Arc;
use foyer_common::{
code::{Key, Value},
error::Result,
properties::Properties,
};
use serde::{de::DeserializeOwned, Serialize};
use crate::record::Record;
pub trait State: Send + Sync + 'static + Default {}
impl<T> State for T where T: Send + Sync + 'static + Default {}
pub trait Config: Send + Sync + 'static + Clone + Serialize + DeserializeOwned + Default {}
impl<T> Config for T where T: Send + Sync + 'static + Clone + Serialize + DeserializeOwned + Default {}
#[expect(clippy::type_complexity)]
pub enum Op<E>
where
E: Eviction,
{
Noop,
Immutable(Box<dyn Fn(&E, &Arc<Record<E>>) + Send + Sync + 'static>),
Mutable(Box<dyn FnMut(&mut E, &Arc<Record<E>>) + Send + Sync + 'static>),
}
impl<E> Op<E>
where
E: Eviction,
{
pub fn noop() -> Self {
Self::Noop
}
pub fn immutable<F>(f: F) -> Self
where
F: Fn(&E, &Arc<Record<E>>) + Send + Sync + 'static,
{
Self::Immutable(Box::new(f))
}
pub fn mutable<F>(f: F) -> Self
where
F: FnMut(&mut E, &Arc<Record<E>>) + Send + Sync + 'static,
{
Self::Mutable(Box::new(f))
}
}
pub trait Eviction: Send + Sync + 'static + Sized {
type Config: Config;
type Key: Key;
type Value: Value;
type Properties: Properties;
type State: State;
fn new(capacity: usize, config: &Self::Config) -> Self;
fn update(&mut self, capacity: usize, config: Option<&Self::Config>) -> Result<()>;
fn push(&mut self, record: Arc<Record<Self>>);
fn pop(&mut self) -> Option<Arc<Record<Self>>>;
fn remove(&mut self, record: &Arc<Record<Self>>);
fn clear(&mut self) {
while self.pop().is_some() {}
}
fn acquire() -> Op<Self>;
fn release() -> Op<Self>;
}
pub mod fifo;
pub mod lfu;
pub mod lru;
pub mod s3fifo;
pub mod sieve;
#[cfg(any(test, feature = "test_utils"))]
pub mod test_utils;