/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moka-0.12.10/src/common/entry.rs
Line | Count | Source |
1 | | use 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 | | /// |
20 | | pub struct Entry<K, V> { |
21 | | key: Option<Arc<K>>, |
22 | | value: V, |
23 | | is_fresh: bool, |
24 | | is_old_value_replaced: bool, |
25 | | } |
26 | | |
27 | | impl<K, V> Debug for Entry<K, V> |
28 | | where |
29 | | K: Debug, |
30 | | V: Debug, |
31 | | { |
32 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
33 | 0 | f.debug_struct("Entry") |
34 | 0 | .field("key", self.key()) |
35 | 0 | .field("value", &self.value) |
36 | 0 | .field("is_fresh", &self.is_fresh) |
37 | 0 | .field("is_old_value_replaced", &self.is_old_value_replaced) |
38 | 0 | .finish() |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl<K, V> Entry<K, V> { |
43 | 0 | pub(crate) fn new( |
44 | 0 | key: Option<Arc<K>>, |
45 | 0 | value: V, |
46 | 0 | is_fresh: bool, |
47 | 0 | is_old_value_replaced: bool, |
48 | 0 | ) -> Self { |
49 | 0 | Self { |
50 | 0 | key, |
51 | 0 | value, |
52 | 0 | is_fresh, |
53 | 0 | is_old_value_replaced, |
54 | 0 | } |
55 | 0 | } Unexecuted instantiation: <moka::common::entry::Entry<hickory_proto::op::query::Query, hickory_resolver::dns_lru::LruValue>>::new Unexecuted instantiation: <moka::common::entry::Entry<_, _>>::new |
56 | | |
57 | | /// Returns a reference to the wrapped key. |
58 | 0 | pub fn key(&self) -> &K { |
59 | 0 | self.key.as_ref().expect("Bug: Key is None") |
60 | 0 | } |
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 | 0 | pub fn value(&self) -> &V { |
67 | 0 | &self.value |
68 | 0 | } |
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 | 0 | pub fn into_value(self) -> V { |
75 | 0 | self.value |
76 | 0 | } Unexecuted instantiation: <moka::common::entry::Entry<hickory_proto::op::query::Query, hickory_resolver::dns_lru::LruValue>>::into_value Unexecuted instantiation: <moka::common::entry::Entry<_, _>>::into_value |
77 | | |
78 | | /// Returns `true` if the value in this `Entry` was not cached and was freshly |
79 | | /// computed. |
80 | 0 | pub fn is_fresh(&self) -> bool { |
81 | 0 | self.is_fresh |
82 | 0 | } |
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 | 0 | pub fn is_old_value_replaced(&self) -> bool { |
90 | 0 | self.is_old_value_replaced |
91 | 0 | } |
92 | | } |