1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::borrow::Borrow;
use std::hash::Hash;

pub trait InsertionPolicy<K> {
    fn should_add(&mut self, key: &K) -> bool;
    fn should_replace(&mut self, candidate: &K, victim: &K) -> bool;

    fn on_cache_hit<Q: ?Sized>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq;

    fn on_cache_miss<Q: ?Sized>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq;

    fn clear(&mut self);

    fn invalidate<Q: ?Sized>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq;
}

pub trait EvictionPolicy<K> {
    fn get_victim(&mut self) -> Option<K>;

    fn on_eviction(&mut self, key: &K);
    fn on_insert(&mut self, key: &K);
    fn on_update(&mut self, key: &K);
    fn on_cache_hit<Q: ?Sized>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq;

    fn clear(&mut self);

    fn invalidate<Q: ?Sized>(&mut self, key: &Q)
    where
        K: Borrow<Q>,
        Q: Hash + Eq;
}