light_cache/policy/
noop.rs

1use std::hash::BuildHasher;
2
3use parking_lot::{Mutex, MutexGuard};
4
5use crate::LightCache;
6use super::{Policy, Prune};
7
8#[derive(Debug)]
9/// A policy that does nothing
10/// 
11/// While this policy does nothing, we still need to give it a mutex so that it can satisfy the Policy trait
12/// It would not be good if a user accidently called prune on a noop policy and got a panic
13pub struct NoopPolicy(Mutex<()>);
14
15impl NoopPolicy {
16    /// Create a new NoopPolicy
17    pub fn new() -> Self {
18        NoopPolicy(Mutex::new(()))
19    }
20}
21
22impl<K, V> Policy<K, V> for NoopPolicy
23where
24    K: Eq + std::hash::Hash + Copy,
25    V: Clone + Sync,
26{
27    type Inner = ();
28
29    fn lock_inner(&self) -> MutexGuard<()> {
30        self.0.lock()
31    }
32
33    #[inline]
34    fn get<S: BuildHasher>(&self, key: &K, cache: &LightCache<K, V, S, Self>) -> Option<V> {
35        cache.get_no_policy(key)
36    }
37
38    #[inline]
39    fn insert<S: BuildHasher>(&self, key: K, value: V, cache: &LightCache<K, V, S, Self>) -> Option<V> {
40        cache.insert_no_policy(key, value)
41    }
42
43    #[inline]
44    fn remove<S: BuildHasher>(&self, key: &K, cache: &LightCache<K, V, S, Self>) -> Option<V> {
45        cache.remove_no_policy(key)
46    }
47}
48
49impl<K, V> Prune<K, V, NoopPolicy> for () {
50    fn prune<S: BuildHasher>(&mut self, _: &LightCache<K, V, S, NoopPolicy>) {}
51}