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!("In main thread: arc_double.apply(50) = {}", arc_double.apply(50));
53    println!(
54        "In child thread: result = {}",
55        handle.join().expect("thread should not panic")
56    );
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!("to_upper.apply('hello') = {}", to_upper.apply("hello".to_string()));
79    println!("to_upper.apply('world') = {}", to_upper.apply("world".to_string()));
80    println!();
81
82    // Example 2: Type conversion pipeline
83    println!("--- Type Conversion Pipeline ---");
84    let parse_int = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
85    let double_int = BoxTransformer::new(|x: i32| x * 2);
86    let to_string = BoxTransformer::new(|x: i32| x.to_string());
87
88    let pipeline = parse_int.and_then(double_int).and_then(to_string);
89    println!("pipeline.apply('21') = {}", pipeline.apply("21".to_string()));
90    println!();
91
92    // Example 3: Shared transformation logic
93    println!("--- Shared Transformation Logic ---");
94    let square = ArcTransformer::new(|x: i32| x * x);
95
96    // Can be shared across different parts of the program
97    let transformer1 = square.clone();
98    let transformer2 = square.clone();
99
100    println!("transformer1.apply(5) = {}", transformer1.apply(5));
101    println!("transformer2.apply(7) = {}", transformer2.apply(7));
102    println!("square.apply(3) = {}", square.apply(3));
103    println!();
104
105    // Example 4: Transformer registry
106    println!("--- Transformer Registry ---");
107    let mut transformers: HashMap<String, RcTransformer<i32, String>> = HashMap::new();
108
109    transformers.insert(
110        "double".to_string(),
111        RcTransformer::new(|x: i32| format!("Doubled: {}", x * 2)),
112    );
113    transformers.insert(
114        "square".to_string(),
115        RcTransformer::new(|x: i32| format!("Squared: {}", x * x)),
116    );
117
118    if let Some(transformer) = transformers.get("double") {
119        println!("Transformer 'double': {}", transformer.apply(7));
120    }
121    if let Some(transformer) = transformers.get("square") {
122        println!("Transformer 'square': {}", transformer.apply(7));
123    }
124    println!();
125
126    // ====================================================================
127    // Part 5: Trait Usage
128    // ====================================================================
129    println!("=== Trait Usage ===\n");
130
131    fn apply_transformer<F: Transformer<i32, String>>(f: &F, x: i32) -> String {
132        f.apply(x)
133    }
134
135    let to_string = BoxTransformer::new(|x: i32| format!("Value: {}", x));
136    println!("Via trait: {}", apply_transformer(&to_string, 42));
137
138    println!("\n=== Demo Complete ===");
139}