-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_error_noclone.rs
More file actions
53 lines (45 loc) · 1.2 KB
/
custom_error_noclone.rs
File metadata and controls
53 lines (45 loc) · 1.2 KB
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
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]
use kash::kash;
use std::{
sync::Arc,
thread::sleep,
time::{Duration, Instant},
};
enum MyError {
Err,
}
#[kash]
fn slow_fn(n: u32) -> Result<String, Arc<MyError>> {
if n == 0 {
return Err(Arc::new(MyError::Err));
}
sleep(Duration::new(1, 0));
slow_fn(n - 1)
}
#[kash(result)]
fn slow_fn_with_result_flag(n: u32) -> Result<String, MyError> {
if n == 0 {
return Err(MyError::Err);
}
sleep(Duration::new(1, 0));
slow_fn_with_result_flag(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());
println!("Initial run...");
let now = Instant::now();
let _ = slow_fn_with_result_flag(10);
println!("Elapsed: {}\n", now.elapsed().as_secs());
println!("Cached run...");
let now = Instant::now();
let _ = slow_fn_with_result_flag(10);
println!("Elapsed: {}\n", now.elapsed().as_secs());
println!("done!");
}