const_lru/entry/
mod.rs

1use num_traits::{PrimInt, Unsigned};
2
3mod occupied;
4mod vacant;
5
6pub use occupied::*;
7pub use vacant::*;
8
9use crate::ConstLru;
10
11/// A view into a single entry in a ConstLru, which may either be vacant or occupied.
12#[derive(Debug)]
13pub enum Entry<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> {
14    Occupied(OccupiedEntry<'a, K, V, CAP, I>),
15    Vacant(VacantEntry<'a, K, V, CAP, I>),
16}
17
18impl<'a, K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> Entry<'a, K, V, CAP, I> {
19    pub(crate) fn new(const_lru: &'a mut ConstLru<K, V, CAP, I>, k: K) -> Self {
20        if CAP == 0 {
21            panic!("Entry API only works for CAP > 0");
22        }
23        let insert_bs_i = match const_lru.get_index_of(&k) {
24            Ok(tup) => return Self::Occupied(OccupiedEntry::new(const_lru, k, tup)),
25            Err(i) => i,
26        };
27        Self::Vacant(VacantEntry::new(const_lru, k, insert_bs_i))
28    }
29}
30
31impl<'a, K, V, const CAP: usize, I: PrimInt + Unsigned> Entry<'a, K, V, CAP, I> {
32    /// Returns a reference to this entry’s key.
33    pub fn key(&self) -> &K {
34        match self {
35            Self::Occupied(e) => e.key(),
36            Self::Vacant(e) => e.key(),
37        }
38    }
39}
40
41impl<'a, K: Ord, V, const CAP: usize, I: PrimInt + Unsigned> Entry<'a, K, V, CAP, I> {
42    /// Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
43    ///
44    /// Also moves the entry to most-recently-used position if previously existing
45    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
46        match self {
47            Self::Occupied(e) => e.into_mut(),
48            Self::Vacant(e) => e.insert(default()).0,
49        }
50    }
51
52    /// Ensures a value is in the entry by inserting, if empty, the result of the default function .
53    /// This method allows for generating key-derived values for insertion by providing the default function a reference to the key
54    /// that was moved during the .entry(key) method call.
55    ///
56    /// The reference to the moved key is provided so that cloning or copying the key is unnecessary, unlike with `.or_insert_with(|| ... )`
57    pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
58        match self {
59            Self::Occupied(e) => e.into_mut(),
60            Self::Vacant(e) => {
61                let v = default(e.key());
62                e.insert(v).0
63            }
64        }
65    }
66
67    /// Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
68    ///
69    /// Also moves the entry to most-recently-used position if previously existing
70    pub fn or_insert(self, default: V) -> &'a mut V {
71        match self {
72            Self::Occupied(e) => e.into_mut(),
73            Self::Vacant(e) => e.insert(default).0,
74        }
75    }
76}
77
78impl<'a, K: Ord, V: Default, const CAP: usize, I: PrimInt + Unsigned> Entry<'a, K, V, CAP, I> {
79    /// Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
80    ///
81    /// Also moves the entry to most-recently-used position if previously existing
82    pub fn or_default(self) -> &'a mut V {
83        self.or_insert(V::default())
84    }
85}