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!(
57        "In child thread: result = {}",
58        handle.join().expect("thread should not panic")
59    );
60    println!();
61
62    // ====================================================================
63    // Part 3: RcTransformer - Single-threaded, cloneable
64    // ====================================================================
65    println!("--- RcTransformer ---");
66    let rc_double = RcTransformer::new(|x: i32| x * 2);
67    let rc_cloned = rc_double.clone();
68
69    println!("rc_double.apply(21) = {}", rc_double.apply(21));
70    println!("rc_cloned.apply(42) = {}", rc_cloned.apply(42));
71    println!();
72
73    // ====================================================================
74    // Part 4: Practical Examples
75    // ====================================================================
76    println!("=== Practical Examples ===\n");
77
78    // Example 1: String transformation
79    println!("--- String Transformation ---");
80    let to_upper = BoxTransformer::new(|s: String| s.to_uppercase());
81    println!(
82        "to_upper.apply('hello') = {}",
83        to_upper.apply("hello".to_string())
84    );
85    println!(
86        "to_upper.apply('world') = {}",
87        to_upper.apply("world".to_string())
88    );
89    println!();
90
91    // Example 2: Type conversion pipeline
92    println!("--- Type Conversion Pipeline ---");
93    let parse_int = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
94    let double_int = BoxTransformer::new(|x: i32| x * 2);
95    let to_string = BoxTransformer::new(|x: i32| x.to_string());
96
97    let pipeline = parse_int.and_then(double_int).and_then(to_string);
98    println!(
99        "pipeline.apply('21') = {}",
100        pipeline.apply("21".to_string())
101    );
102    println!();
103
104    // Example 3: Shared transformation logic
105    println!("--- Shared Transformation Logic ---");
106    let square = ArcTransformer::new(|x: i32| x * x);
107
108    // Can be shared across different parts of the program
109    let transformer1 = square.clone();
110    let transformer2 = square.clone();
111
112    println!("transformer1.apply(5) = {}", transformer1.apply(5));
113    println!("transformer2.apply(7) = {}", transformer2.apply(7));
114    println!("square.apply(3) = {}", square.apply(3));
115    println!();
116
117    // Example 4: Transformer registry
118    println!("--- Transformer Registry ---");
119    let mut transformers: HashMap<String, RcTransformer<i32, String>> = HashMap::new();
120
121    transformers.insert(
122        "double".to_string(),
123        RcTransformer::new(|x: i32| format!("Doubled: {}", x * 2)),
124    );
125    transformers.insert(
126        "square".to_string(),
127        RcTransformer::new(|x: i32| format!("Squared: {}", x * x)),
128    );
129
130    if let Some(transformer) = transformers.get("double") {
131        println!("Transformer 'double': {}", transformer.apply(7));
132    }
133    if let Some(transformer) = transformers.get("square") {
134        println!("Transformer 'square': {}", transformer.apply(7));
135    }
136    println!();
137
138    // ====================================================================
139    // Part 5: Trait Usage
140    // ====================================================================
141    println!("=== Trait Usage ===\n");
142
143    fn apply_transformer<F: Transformer<i32, String>>(f: &F, x: i32) -> String {
144        f.apply(x)
145    }
146
147    let to_string = BoxTransformer::new(|x: i32| format!("Value: {}", x));
148    println!("Via trait: {}", apply_transformer(&to_string, 42));
149
150    println!("\n=== Demo Complete ===");
151}