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