moka/common/
entry.rs

1use std::{fmt::Debug, sync::Arc};
2
3/// A snapshot of a single entry in the cache.
4///
5/// `Entry` is constructed from the methods like `or_insert` on the struct returned
6/// by cache's `entry` or `entry_by_ref` methods. `Entry` holds the cached key and
7/// value at the time it was constructed. It also carries extra information about the
8/// entry; [`is_fresh`](#method.is_fresh) method returns `true` if the value was not
9/// cached and was freshly computed.
10///
11/// See the followings for more information about `entry` and `entry_by_ref` methods:
12///
13/// - `sync::Cache`:
14///     - [`entry`](./sync/struct.Cache.html#method.entry)
15///     - [`entry_by_ref`](./sync/struct.Cache.html#method.entry_by_ref)
16/// - `future::Cache`:
17///     - [`entry`](./future/struct.Cache.html#method.entry)
18///     - [`entry_by_ref`](./future/struct.Cache.html#method.entry_by_ref)
19///
20pub struct Entry<K, V> {
21    key: Option<Arc<K>>,
22    value: V,
23    is_fresh: bool,
24    is_old_value_replaced: bool,
25}
26
27impl<K, V> Debug for Entry<K, V>
28where
29    K: Debug,
30    V: Debug,
31{
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        f.debug_struct("Entry")
34            .field("key", self.key())
35            .field("value", &self.value)
36            .field("is_fresh", &self.is_fresh)
37            .field("is_old_value_replaced", &self.is_old_value_replaced)
38            .finish()
39    }
40}
41
42impl<K, V> Entry<K, V> {
43    pub(crate) fn new(
44        key: Option<Arc<K>>,
45        value: V,
46        is_fresh: bool,
47        is_old_value_replaced: bool,
48    ) -> Self {
49        Self {
50            key,
51            value,
52            is_fresh,
53            is_old_value_replaced,
54        }
55    }
56
57    /// Returns a reference to the wrapped key.
58    pub fn key(&self) -> &K {
59        self.key.as_ref().expect("Bug: Key is None")
60    }
61
62    /// Returns a reference to the wrapped value.
63    ///
64    /// Note that the returned reference is _not_ pointing to the original value in
65    /// the cache. Instead, it is pointing to the cloned value in this `Entry`.
66    pub fn value(&self) -> &V {
67        &self.value
68    }
69
70    /// Consumes this `Entry`, returning the wrapped value.
71    ///
72    /// Note that the returned value is a clone of the original value in the cache.
73    /// It was cloned when this `Entry` was constructed.
74    pub fn into_value(self) -> V {
75        self.value
76    }
77
78    /// Returns `true` if the value in this `Entry` was not cached and was freshly
79    /// computed.
80    pub fn is_fresh(&self) -> bool {
81        self.is_fresh
82    }
83
84    /// Returns `true` if an old value existed in the cache and was replaced by the
85    /// value in this `Entry`.
86    ///
87    /// Note that the new value can be the same as the old value. This method still
88    /// returns `true` in that case.
89    pub fn is_old_value_replaced(&self) -> bool {
90        self.is_old_value_replaced
91    }
92}