Skip to main content

task_demo/
task_demo.rs

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