cachelito_core/
keys.rs

1use std::fmt::Debug;
2
3/// Trait defining how to generate a cache key for a given type.
4///
5/// This trait must be implemented for any type that will be used as a function
6/// parameter in a cached function. The cache key is used to uniquely identify
7/// cached results.
8///
9/// # Examples
10///
11/// ```
12/// use cachelito_core::CacheableKey;
13///
14/// #[derive(Debug)]
15/// struct UserId(u64);
16///
17/// impl CacheableKey for UserId {
18///     fn to_cache_key(&self) -> String {
19///         format!("user_{}", self.0)
20///     }
21/// }
22/// ```
23pub trait CacheableKey {
24    /// Converts this value into a string that can be used as a cache key.
25    ///
26    /// The returned string should uniquely identify the value to ensure
27    /// correct cache behavior.
28    fn to_cache_key(&self) -> String;
29}
30
31/// Marker trait for types that want to use the *default* cache key behavior.
32///
33/// Implement this trait for any type that should automatically get a cache key
34/// derived from its `Debug` representation. This is the simplest way to make
35/// a type cacheable.
36///
37/// # Examples
38///
39/// ```
40/// use cachelito_core::DefaultCacheableKey;
41///
42/// #[derive(Debug, Clone)]
43/// struct Product {
44///     id: u32,
45///     name: String,
46/// }
47///
48/// // Enable default cache key generation based on Debug
49/// impl DefaultCacheableKey for Product {}
50/// ```
51///
52/// # Note
53///
54/// Types implementing this trait must also implement `Debug`, as the default
55/// cache key is generated using `format!("{:?}", value)`.
56pub trait DefaultCacheableKey: Debug {}
57
58/// Blanket implementation for any type that explicitly opts in via `DefaultCacheableKey`.
59///
60/// This automatically implements `CacheableKey::to_cache_key()` for any type that
61/// implements `DefaultCacheableKey`, using the type's `Debug` representation as the key.
62///
63/// # Performance Note
64///
65/// The cache key is generated by formatting the value with `{:?}`. For complex types,
66/// consider implementing `CacheableKey` directly for better performance.
67impl<T> CacheableKey for T
68where
69    T: DefaultCacheableKey + ?Sized,
70{
71    fn to_cache_key(&self) -> String {
72        format!("{:?}", self)
73    }
74}
75
76// ============================================================================
77// Standard Library Type Implementations
78// ============================================================================
79// These implementations allow all common Rust types to be used as cache keys
80// without requiring manual implementation.
81
82// Unsigned integer types
83impl DefaultCacheableKey for u8 {}
84impl DefaultCacheableKey for u16 {}
85impl DefaultCacheableKey for u32 {}
86impl DefaultCacheableKey for u64 {}
87impl DefaultCacheableKey for u128 {}
88impl DefaultCacheableKey for usize {}
89
90// Signed integer types
91impl DefaultCacheableKey for i8 {}
92impl DefaultCacheableKey for i16 {}
93impl DefaultCacheableKey for i32 {}
94impl DefaultCacheableKey for i64 {}
95impl DefaultCacheableKey for i128 {}
96impl DefaultCacheableKey for isize {}
97
98// Floating point types
99impl DefaultCacheableKey for f32 {}
100impl DefaultCacheableKey for f64 {}
101
102// Boolean type
103impl DefaultCacheableKey for bool {}
104
105// Character type
106impl DefaultCacheableKey for char {}
107
108// String types
109impl DefaultCacheableKey for String {}
110impl DefaultCacheableKey for &str {}
111
112// Tuple types (up to 5 elements)
113impl<T1: DefaultCacheableKey> DefaultCacheableKey for (T1,) {}
114impl<T1: DefaultCacheableKey, T2: DefaultCacheableKey> DefaultCacheableKey for (T1, T2) {}
115impl<T1: DefaultCacheableKey, T2: DefaultCacheableKey, T3: DefaultCacheableKey> DefaultCacheableKey
116    for (T1, T2, T3)
117{
118}
119impl<
120        T1: DefaultCacheableKey,
121        T2: DefaultCacheableKey,
122        T3: DefaultCacheableKey,
123        T4: DefaultCacheableKey,
124    > DefaultCacheableKey for (T1, T2, T3, T4)
125{
126}
127impl<
128        T1: DefaultCacheableKey,
129        T2: DefaultCacheableKey,
130        T3: DefaultCacheableKey,
131        T4: DefaultCacheableKey,
132        T5: DefaultCacheableKey,
133    > DefaultCacheableKey for (T1, T2, T3, T4, T5)
134{
135}
136
137// Option and Result wrapper types
138impl<T: DefaultCacheableKey> DefaultCacheableKey for Option<T> {}
139
140// Collection types
141impl<T: DefaultCacheableKey> DefaultCacheableKey for Vec<T> {}
142impl<T: DefaultCacheableKey> DefaultCacheableKey for &[T] {}