1use qubit_function::{
11 ArcCallable,
12 BoxCallable,
13 BoxCallableOnce,
14 BoxCallableWith,
15 BoxRunnable,
16 BoxRunnableOnce,
17 BoxRunnableWith,
18 Callable,
19 CallableOnce,
20 CallableWith,
21 Runnable,
22 RunnableOnce,
23 RunnableWith,
24};
25
26fn main() {
27 println!("=== Task Demo ===\n");
28
29 demo_reusable_tasks();
30 demo_mutable_input_tasks();
31 demo_once_tasks();
32 demo_shared_callable();
33}
34
35fn demo_reusable_tasks() {
36 println!("--- Reusable zero-argument tasks ---");
37
38 let mut attempts = 0;
39 let mut callable = BoxCallable::new(move || {
40 attempts += 1;
41 Ok::<i32, String>(attempts * 10)
42 });
43 println!("Callable first call: {:?}", callable.call());
44 println!("Callable second call: {:?}", callable.call());
45
46 let mut runnable = BoxRunnable::new(|| {
47 println!("Runnable side effect executed");
48 Ok::<(), String>(())
49 });
50 println!("Runnable result: {:?}", runnable.run());
51 println!();
52}
53
54fn demo_mutable_input_tasks() {
55 println!("--- Mutable-input tasks ---");
56
57 let mut state = 40;
58 let mut callable = BoxCallableWith::new(|input: &mut i32| {
59 *input += 2;
60 Ok::<i32, String>(*input)
61 });
62 println!("CallableWith result: {:?}", callable.call_with(&mut state));
63 println!("State after CallableWith: {state}");
64
65 let mut runnable = BoxRunnableWith::new(|input: &mut i32| {
66 *input *= 2;
67 Ok::<(), String>(())
68 });
69 println!("RunnableWith result: {:?}", runnable.run_with(&mut state));
70 println!("State after RunnableWith: {state}");
71 println!();
72}
73
74fn demo_once_tasks() {
75 println!("--- One-time tasks ---");
76
77 let callable_once = BoxCallableOnce::new(|| Ok::<String, String>(String::from("ready")));
78 println!("CallableOnce result: {:?}", callable_once.call());
79
80 let runnable_once = BoxRunnableOnce::new(|| {
81 println!("RunnableOnce side effect executed");
82 Ok::<(), String>(())
83 });
84 println!("RunnableOnce result: {:?}", runnable_once.run());
85 println!();
86}
87
88fn demo_shared_callable() {
89 println!("--- Shared callable ---");
90
91 let mut call_count = 0;
92 let shared = ArcCallable::new(move || {
93 call_count += 1;
94 Ok::<usize, String>(call_count)
95 });
96
97 let mut first = shared.clone();
98 let mut second = shared.clone();
99 println!("ArcCallable first clone: {:?}", first.call());
100 println!("ArcCallable second clone: {:?}", second.call());
101}