Trait Cache

Source
pub trait Cache {
    type Arg;
    type Output;

    // Required methods
    fn new() -> Self;
    fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>;
    fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>;
    fn clear(&self);
}
Expand description

The cache for synchronized memoization.

Required Associated Types§

Required Methods§

Source

fn new() -> Self

Creates an empty cache.

Source

fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>

Gets the cell of the arg in cache. Returns None if arg is not cached. This method should only acquire read lock if needed.

Source

fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>

Gets the cell of the arg in cache, creates if arg is not cached. This method may acquire write lock if needed.

Source

fn clear(&self)

Clears the cache.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Arg, Output> Cache for RwLock<HashMap<Arg, Arc<OnceCell<Output>>>>
where Arg: Eq + Hash,

Use HashMap with RwLock as Cache.

Source§

type Arg = Arg

Source§

type Output = Output

Source§

fn new() -> Self

Source§

fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>

Source§

fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>

Source§

fn clear(&self)

Source§

impl<Arg, Output> Cache for CHashMap<Arg, Arc<OnceCell<Output>>>
where Arg: Eq + Hash,

Use CHashMap as Cache.

Source§

type Arg = Arg

Source§

type Output = Output

Source§

fn new() -> Self

Source§

fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>

Source§

fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>

Source§

fn clear(&self)

Source§

impl<Output> Cache for RwLock<Vec<Arc<OnceCell<Output>>>>

Use Vec with RwLock as Cache for sequences.

Source§

type Arg = usize

Source§

type Output = Output

Source§

fn new() -> Self

Source§

fn get(&self, arg: &Self::Arg) -> Option<Arc<OnceCell<Self::Output>>>

Source§

fn get_or_new(&self, arg: Self::Arg) -> Arc<OnceCell<Self::Output>>

Source§

fn clear(&self)

Implementors§