cache_rs/config/
lfu.rs

1//! Configuration for the Least Frequently Used (LFU) cache.
2
3use core::fmt;
4use core::num::NonZeroUsize;
5
6/// Configuration for an LFU (Least Frequently Used) cache.
7///
8/// LFU tracks the frequency of access for each item and evicts
9/// the least frequently used items when the cache reaches capacity.
10///
11/// # Examples
12///
13/// ```
14/// use cache_rs::config::lfu::LfuCacheConfig;
15/// use core::num::NonZeroUsize;
16///
17/// // Create a config with capacity of 100 items
18/// let config = LfuCacheConfig::new(NonZeroUsize::new(100).unwrap());
19///
20/// assert_eq!(config.capacity(), NonZeroUsize::new(100).unwrap());
21/// ```
22#[derive(Clone, Copy)]
23pub struct LfuCacheConfig {
24    /// Maximum number of key-value pairs the cache can hold
25    capacity: NonZeroUsize,
26}
27
28impl LfuCacheConfig {
29    /// Creates a new configuration for an LFU cache.
30    ///
31    /// # Arguments
32    /// * `capacity` - Maximum number of key-value pairs the cache can hold
33    pub fn new(capacity: NonZeroUsize) -> Self {
34        Self { capacity }
35    }
36
37    /// Returns the maximum number of key-value pairs the cache can hold.
38    pub fn capacity(&self) -> NonZeroUsize {
39        self.capacity
40    }
41}
42
43impl fmt::Debug for LfuCacheConfig {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.debug_struct("LfuCacheConfig")
46            .field("capacity", &self.capacity)
47            .finish()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_lfu_config_creation() {
57        let config = LfuCacheConfig::new(NonZeroUsize::new(100).unwrap());
58        assert_eq!(config.capacity().get(), 100);
59    }
60}