cachelito-core 0.16.0

Core functionality for cachelito - global cache with LRU/FIFO/LFU/ARC/Random/TLRU/W-TinyLFU eviction policies
Documentation
use std::fmt::Debug;

/// Trait defining how to generate a cache key for a given type.
///
/// This trait must be implemented for any type that will be used as a function
/// parameter in a cached function. The cache key is used to uniquely identify
/// cached results.
///
/// # Examples
///
/// ```
/// use cachelito_core::CacheableKey;
///
/// #[derive(Debug)]
/// struct UserId(u64);
///
/// impl CacheableKey for UserId {
///     fn to_cache_key(&self) -> String {
///         format!("user_{}", self.0)
///     }
/// }
/// ```
pub trait CacheableKey {
    /// Converts this value into a string that can be used as a cache key.
    ///
    /// The returned string should uniquely identify the value to ensure
    /// correct cache behavior.
    fn to_cache_key(&self) -> String;
}

/// Marker trait for types that want to use the *default* cache key behavior.
///
/// Implement this trait for any type that should automatically get a cache key
/// derived from its `Debug` representation. This is the simplest way to make
/// a type cacheable.
///
/// # Examples
///
/// ```
/// use cachelito_core::DefaultCacheableKey;
///
/// #[derive(Debug, Clone)]
/// struct Product {
///     id: u32,
///     name: String,
/// }
///
/// // Enable default cache key generation based on Debug
/// impl DefaultCacheableKey for Product {}
/// ```
///
/// # Note
///
/// Types implementing this trait must also implement `Debug`, as the default
/// cache key is generated using `format!("{:?}", value)`.
pub trait DefaultCacheableKey: Debug {}

/// Blanket implementation for any type that explicitly opts in via `DefaultCacheableKey`.
///
/// This automatically implements `CacheableKey::to_cache_key()` for any type that
/// implements `DefaultCacheableKey`, using the type's `Debug` representation as the key.
///
/// # Performance Note
///
/// The cache key is generated by formatting the value with `{:?}`. For complex types,
/// consider implementing `CacheableKey` directly for better performance.
impl<T> CacheableKey for T
where
    T: DefaultCacheableKey + ?Sized,
{
    fn to_cache_key(&self) -> String {
        format!("{:?}", self)
    }
}

// ============================================================================
// Standard Library Type Implementations
// ============================================================================
// These implementations allow all common Rust types to be used as cache keys
// without requiring manual implementation.

// Unsigned integer types
impl DefaultCacheableKey for u8 {}
impl DefaultCacheableKey for u16 {}
impl DefaultCacheableKey for u32 {}
impl DefaultCacheableKey for u64 {}
impl DefaultCacheableKey for u128 {}
impl DefaultCacheableKey for usize {}

// Signed integer types
impl DefaultCacheableKey for i8 {}
impl DefaultCacheableKey for i16 {}
impl DefaultCacheableKey for i32 {}
impl DefaultCacheableKey for i64 {}
impl DefaultCacheableKey for i128 {}
impl DefaultCacheableKey for isize {}

// Floating point types
impl DefaultCacheableKey for f32 {}
impl DefaultCacheableKey for f64 {}

// Boolean type
impl DefaultCacheableKey for bool {}

// Character type
impl DefaultCacheableKey for char {}

// String types
impl DefaultCacheableKey for String {}
impl DefaultCacheableKey for &str {}

// Tuple types (up to 5 elements)
impl<T1: DefaultCacheableKey> DefaultCacheableKey for (T1,) {}
impl<T1: DefaultCacheableKey, T2: DefaultCacheableKey> DefaultCacheableKey for (T1, T2) {}
impl<T1: DefaultCacheableKey, T2: DefaultCacheableKey, T3: DefaultCacheableKey> DefaultCacheableKey
    for (T1, T2, T3)
{
}
impl<
        T1: DefaultCacheableKey,
        T2: DefaultCacheableKey,
        T3: DefaultCacheableKey,
        T4: DefaultCacheableKey,
    > DefaultCacheableKey for (T1, T2, T3, T4)
{
}
impl<
        T1: DefaultCacheableKey,
        T2: DefaultCacheableKey,
        T3: DefaultCacheableKey,
        T4: DefaultCacheableKey,
        T5: DefaultCacheableKey,
    > DefaultCacheableKey for (T1, T2, T3, T4, T5)
{
}

// Option and Result wrapper types
impl<T: DefaultCacheableKey> DefaultCacheableKey for Option<T> {}

// Collection types
impl<T: DefaultCacheableKey> DefaultCacheableKey for Vec<T> {}
impl<T: DefaultCacheableKey> DefaultCacheableKey for &[T] {}