OnEvictCallback

Trait OnEvictCallback 

Source
pub trait OnEvictCallback {
    // Required method
    fn on_evict<K, V>(&self, key: &K, val: &V);
}
Expand description

OnEvictCallback is used to apply a callback for evicted entry.

§Example

use caches::{RawLRU, OnEvictCallback, Cache};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

struct EvictedCounter {
    ctr: Arc<AtomicU64>,
}

impl EvictedCounter {
    pub fn new(ctr: Arc<AtomicU64>) -> Self {
        Self {
            ctr,
        }
    }
}

impl OnEvictCallback for EvictedCounter {
    fn on_evict<K, V>(&self, _: &K, _: &V) {
        self.ctr.fetch_add(1, Ordering::SeqCst);
    }
}

let counter = Arc::new(AtomicU64::new(0));

let mut cache: RawLRU<u64, u64, EvictedCounter> = RawLRU::with_on_evict_cb(1, EvictedCounter::new(counter.clone())).unwrap();

cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);

assert_eq!(counter.load(Ordering::SeqCst), 2);

Required Methods§

Source

fn on_evict<K, V>(&self, key: &K, val: &V)

on_evict is a callback function will be invoked if an entry is evicted.

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.

Implementors§