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
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]
use kash::kash;
use std::{
thread::sleep,
time::{Duration, Instant},
};
#[kash]
fn slow_fn(n: u32) -> String {
if n == 0 {
return "done".to_string();
}
sleep(Duration::new(1, 0));
slow_fn(n - 1)
}
pub fn main() {
println!("Initial run...");
let now = Instant::now();
let _ = slow_fn(10);
println!("Elapsed: {}\n", now.elapsed().as_secs());
println!("Cached run...");
let now = Instant::now();
let _ = slow_fn(10);
println!("Elapsed: {}\n", now.elapsed().as_secs());
// Inspect the cache
// {
// println!(" ** Cache info **");
// let cache = SLOW_FN.clone();
// assert_eq!(cache.cache_hits().unwrap(), 1);
// println!("hits=1 -> {:?}", cache.cache_hits().unwrap() == 1);
// assert_eq!(cache.cache_misses().unwrap(), 11);
// println!("misses=11 -> {:?}", cache.cache_misses().unwrap() == 11);
// // make sure the cache-lock is dropped
// }
println!("done!");
}