1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Key derivation strategies for cache keys
//!
//! This module provides different strategies for generating unique and consistent cache keys
//! for function caching. The choice of key derivation strategy affects how cache hits and
//! misses are determined.
//!
//! # Key Derivation Approaches
//!
//! The library supports two main approaches for key derivation:
//!
//! * **Runtime Key Derivation**: Creates cache keys based on the runtime values of function
//! arguments. This is the default approach and provides high-precision caching, where functions
//! are only considered cache hits when called with identical argument values.
//!
//! * **Compile-Time Key Derivation**: Creates cache keys based on the function's signature
//! (name, module path, parameter types, and return type) without considering actual parameter
//! values. This is useful for functions where you want to cache based on the function identity
//! rather than specific arguments, such as functions that fetch fresh data regardless of input.
//!
//! # Examples
//!
//! ```
//! use fncache::key_derivation::KeyDerivation;
//! use fncache::{fncache, MemoryBackend, init_global_cache};
//!
//! // Initialize global cache
//! init_global_cache(MemoryBackend::new()).unwrap();
//!
//! // Default runtime key derivation - cache based on parameter values
//! #[fncache]
//! fn calculate_with_runtime_key(x: i32, y: i32) -> i32 {
//! println!("Computing result");
//! x + y
//! }
//!
//! // Compile-time key derivation - cache based on function signature only
//! #[fncache(key_derivation = "CompileTime")]
//! fn fetch_with_compile_time_key(resource_id: i32) -> String {
//! println!("Fetching resource");
//! format!("Resource {}", resource_id)
//! }
//!
//! // First call computes, second call uses cache
//! calculate_with_runtime_key(1, 2); // Prints "Computing result"
//! calculate_with_runtime_key(1, 2); // Uses cached result
//! calculate_with_runtime_key(3, 4); // Prints "Computing result" (different parameters)
//!
//! // With compile-time keys, all calls with any parameters use the same cache entry
//! fetch_with_compile_time_key(1); // Prints "Fetching resource"
//! fetch_with_compile_time_key(2); // Uses cached result despite different parameter
//! ```
use DefaultHasher;
use ;
/// Strategy to use for deriving cache keys.
///
/// This enum represents the available key derivation strategies that determine
/// how cache keys are generated for cached functions.
///
/// # Examples
///
/// ```
/// use fncache::key_derivation::KeyDerivation;
///
/// // Using runtime key derivation (default)
/// let strategy1 = KeyDerivation::default(); // Returns Runtime
///
/// // Explicitly selecting compile-time key derivation
/// let strategy2 = KeyDerivation::CompileTime;
/// ```
/// Generate a compile-time key for a function.
///
/// This creates a deterministic hash based on the function name,
/// module path, parameter types, and return type. The hash is consistent
/// across multiple runs of the program, as long as the function signature
/// doesn't change.
///
/// This function is primarily used by the `fncache` procedural macro when
/// `key_derivation = "CompileTime"` is specified.
///
/// # Arguments
///
/// * `fn_name` - The function name (e.g., "add", "fetch_data")
/// * `mod_path` - The module path of the function (e.g., "my_crate::utils")
/// * `param_type_names` - A slice of parameter type names as strings
/// * `return_type_name` - The return type name as a string
///
/// # Returns
///
/// A 64-bit hash value that uniquely identifies the function signature
///
/// # Examples
///
/// ```
/// use fncache::key_derivation::generate_compile_time_key;
///
/// let key = generate_compile_time_key(
/// "add", // Function name
/// "my_app::math", // Module path
/// &["i32", "i32"], // Parameter types
/// "i32", // Return type
/// );
///
/// // This will always produce the same hash for the same inputs
/// ```
/// Extracts the type name from a type as a string.
///
/// This helper function uses Rust's `std::any::type_name` to extract the
/// fully qualified type name of a type. It is used by the `fncache` procedural
/// macro to get the type names for compile-time key generation.
///
/// This function is only available when the `compile-time-keys` feature is enabled.
///
/// # Type Parameters
///
/// * `T` - The type to extract the name from
///
/// # Arguments
///
/// * `_` - An unused reference to a value of type `T` (used only for type inference)
///
/// # Returns
///
/// A static string containing the fully qualified type name
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "compile-time-keys")]
/// # {
/// use fncache::key_derivation::type_name_of;
///
/// let name1 = type_name_of(&42_i32); // "i32"
/// let name2 = type_name_of(&"hello".to_string()); // "alloc::string::String"
/// # }
/// ```