cache_mod/error.rs
1//! Error type returned by `cache-mod` constructors and operations.
2
3use core::fmt;
4
5/// Errors produced by `cache-mod`.
6///
7/// The enum is `#[non_exhaustive]` — new variants may be added in minor
8/// releases as additional cache types and eviction policies land.
9///
10/// # Example
11///
12/// ```
13/// use cache_mod::{CacheError, LruCache};
14///
15/// // Zero capacity is rejected up-front.
16/// let result = LruCache::<u32, u32>::new(0);
17/// assert_eq!(result.err(), Some(CacheError::InvalidCapacity));
18/// ```
19#[non_exhaustive]
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21pub enum CacheError {
22 /// A cache constructor was called with a capacity of zero.
23 ///
24 /// Every eviction policy in this crate requires at least one entry of
25 /// headroom, so capacity must be `>= 1`.
26 InvalidCapacity,
27}
28
29impl fmt::Display for CacheError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Self::InvalidCapacity => f.write_str("cache capacity must be non-zero"),
33 }
34 }
35}
36
37#[cfg(feature = "std")]
38impl std::error::Error for CacheError {}