Skip to main content

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