moka/ops.rs
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//! Cache operations.
/// Operations used by the `and_compute_with` and similar methods.
pub mod compute {
use std::sync::Arc;
use crate::Entry;
/// Instructs the `and_compute_with` and similar methods how to modify the cached
/// entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Op<V> {
/// No-operation. Do not modify the cached entry.
Nop,
/// Insert or update the value of the cached entry.
Put(V),
/// Remove the cached entry.
Remove,
}
/// The result of the `and_compute_with` and similar methods.
#[derive(Debug)]
pub enum CompResult<K, V> {
/// The entry did not exist and still does not exist.
StillNone(Arc<K>),
/// The entry already existed and was not modified. The returned entry
/// contains the existing value.
Unchanged(Entry<K, V>),
/// The entry did not exist and was inserted. The returned entry contains
/// the inserted value.
Inserted(Entry<K, V>),
/// The entry already existed and its value was replaced with a new one. The
/// returned entry contains the new value (not the replaced value).
ReplacedWith(Entry<K, V>),
/// The entry already existed and was removed. The returned entry contains
/// the removed value.
///
/// Note: `StillNone` is returned instead of `Removed` if `Op::Remove` was
/// requested but the entry did not exist.
Removed(Entry<K, V>),
}
impl<K, V> CompResult<K, V> {
/// Returns the contained `Some(Entry)` if any. Otherwise returns `None`.
/// Consumes the `self` value.
pub fn into_entry(self) -> Option<Entry<K, V>> {
match self {
CompResult::StillNone(_) => None,
CompResult::Unchanged(entry) => Some(entry),
CompResult::Inserted(entry) => Some(entry),
CompResult::ReplacedWith(entry) => Some(entry),
CompResult::Removed(entry) => Some(entry),
}
}
/// Unwraps the contained `Entry`, consuming the `self` value.
///
/// # Panics
///
/// Panics if the `self` value is `StillNone`.
pub fn unwrap(self) -> Entry<K, V> {
match self {
CompResult::StillNone(_) => panic!("`CompResult::unwrap` called on `StillNone`"),
CompResult::Unchanged(entry) => entry,
CompResult::Inserted(entry) => entry,
CompResult::ReplacedWith(entry) => entry,
CompResult::Removed(entry) => entry,
}
}
}
}