Skip to main content

transformer_demo/
transformer_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10use qubit_function::{
11    ArcTransformer,
12    BoxTransformer,
13    RcTransformer,
14    Transformer,
15};
16use std::collections::HashMap;
17use std::thread;
18
19fn main() {
20    println!("=== Transformer Demo - Type Transformation (consumes T) ===\n");
21
22    // ====================================================================
23    // Part 1: BoxTransformer - Single ownership, reusable
24    // ====================================================================
25    println!("--- BoxTransformer ---");
26    let double = BoxTransformer::new(|x: i32| x * 2);
27    println!("double.apply(21) = {}", double.apply(21));
28    println!("double.apply(42) = {}", double.apply(42));
29
30    // Identity and constant
31    let identity = BoxTransformer::<i32, i32>::identity();
32    println!("identity.apply(42) = {}", identity.apply(42));
33
34    let constant = BoxTransformer::constant("hello");
35    println!("constant.apply(123) = {}", constant.apply(123));
36    println!();
37
38    // ====================================================================
39    // Part 2: ArcTransformer - Thread-safe, cloneable
40    // ====================================================================
41    println!("--- ArcTransformer ---");
42    let arc_double = ArcTransformer::new(|x: i32| x * 2);
43    let arc_cloned = arc_double.clone();
44
45    println!("arc_double.apply(21) = {}", arc_double.apply(21));
46    println!("arc_cloned.apply(42) = {}", arc_cloned.apply(42));
47
48    // Multi-threaded usage
49    let for_thread = arc_double.clone();
50    let handle = thread::spawn(move || for_thread.apply(100));
51    println!(
52        "In main thread: arc_double.apply(50) = {}",
53        arc_double.apply(50)
54    );
55    println!("In child thread: result = {}", handle.join().unwrap());
56    println!();
57
58    // ====================================================================
59    // Part 3: RcTransformer - Single-threaded, cloneable
60    // ====================================================================
61    println!("--- RcTransformer ---");
62    let rc_double = RcTransformer::new(|x: i32| x * 2);
63    let rc_cloned = rc_double.clone();
64
65    println!("rc_double.apply(21) = {}", rc_double.apply(21));
66    println!("rc_cloned.apply(42) = {}", rc_cloned.apply(42));
67    println!();
68
69    // ====================================================================
70    // Part 4: Practical Examples
71    // ====================================================================
72    println!("=== Practical Examples ===\n");
73
74    // Example 1: String transformation
75    println!("--- String Transformation ---");
76    let to_upper = BoxTransformer::new(|s: String| s.to_uppercase());
77    println!(
78        "to_upper.apply('hello') = {}",
79        to_upper.apply("hello".to_string())
80    );
81    println!(
82        "to_upper.apply('world') = {}",
83        to_upper.apply("world".to_string())
84    );
85    println!();
86
87    // Example 2: Type conversion pipeline
88    println!("--- Type Conversion Pipeline ---");
89    let parse_int = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
90    let double_int = BoxTransformer::new(|x: i32| x * 2);
91    let to_string = BoxTransformer::new(|x: i32| x.to_string());
92
93    let pipeline = parse_int.and_then(double_int).and_then(to_string);
94    println!(
95        "pipeline.apply('21') = {}",
96        pipeline.apply("21".to_string())
97    );
98    println!();
99
100    // Example 3: Shared transformation logic
101    println!("--- Shared Transformation Logic ---");
102    let square = ArcTransformer::new(|x: i32| x * x);
103
104    // Can be shared across different parts of the program
105    let transformer1 = square.clone();
106    let transformer2 = square.clone();
107
108    println!("transformer1.apply(5) = {}", transformer1.apply(5));
109    println!("transformer2.apply(7) = {}", transformer2.apply(7));
110    println!("square.apply(3) = {}", square.apply(3));
111    println!();
112
113    // Example 4: Transformer registry
114    println!("--- Transformer Registry ---");
115    let mut transformers: HashMap<String, RcTransformer<i32, String>> = HashMap::new();
116
117    transformers.insert(
118        "double".to_string(),
119        RcTransformer::new(|x: i32| format!("Doubled: {}", x * 2)),
120    );
121    transformers.insert(
122        "square".to_string(),
123        RcTransformer::new(|x: i32| format!("Squared: {}", x * x)),
124    );
125
126    if let Some(transformer) = transformers.get("double") {
127        println!("Transformer 'double': {}", transformer.apply(7));
128    }
129    if let Some(transformer) = transformers.get("square") {
130        println!("Transformer 'square': {}", transformer.apply(7));
131    }
132    println!();
133
134    // ====================================================================
135    // Part 5: Trait Usage
136    // ====================================================================
137    println!("=== Trait Usage ===\n");
138
139    fn apply_transformer<F: Transformer<i32, String>>(f: &F, x: i32) -> String {
140        f.apply(x)
141    }
142
143    let to_string = BoxTransformer::new(|x: i32| format!("Value: {}", x));
144    println!("Via trait: {}", apply_transformer(&to_string, 42));
145
146    println!("\n=== Demo Complete ===");
147}