pub struct BoxTransformer<T, R> { /* private fields */ }Expand description
BoxTransformer - transformer wrapper based on Box<dyn Fn>
A transformer wrapper that provides single ownership with reusable transformation. The transformer consumes the input and can be called multiple times.
§Features
- Based on:
Box<dyn Fn(T) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can be called multiple times (each call consumes its input)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
Implementations§
Source§impl<T, R> BoxTransformer<T, R>
impl<T, R> BoxTransformer<T, R>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
Creates a new transformer.
Wraps the provided closure in the appropriate smart pointer type for this transformer implementation.
Examples found in repository?
25fn main() {
26 println!("=== TransformerOnce Demo ===\n");
27
28 // BoxTransformer TransformerOnce demonstration
29 println!("1. BoxTransformer TransformerOnce demonstration:");
30 let double = BoxTransformer::new(|x: i32| x * 2);
31 let result = double.apply(21);
32 println!(" double.apply(21) = {}", result);
33
34 // Convert to BoxTransformerOnce
35 let double = BoxTransformer::new(|x: i32| x * 2);
36 let boxed = double.into_box();
37 let result = boxed.apply(21);
38 println!(" double.into_box().apply(21) = {}", result);
39
40 // Convert to function
41 let double = BoxTransformer::new(|x: i32| x * 2);
42 let func = double.into_fn();
43 let result = func(21);
44 println!(" double.into_fn()(21) = {}", result);
45
46 println!();
47
48 // RcTransformer TransformerOnce demonstration
49 println!("2. RcTransformer TransformerOnce demonstration:");
50 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
51 let result = uppercase.apply("hello".to_string());
52 println!(" uppercase.apply(\"hello\") = {}", result);
53
54 // Use after cloning
55 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
56 let uppercase_clone = uppercase.clone();
57 let result1 = uppercase.apply("world".to_string());
58 let result2 = uppercase_clone.apply("rust".to_string());
59 println!(" uppercase.apply(\"world\") = {}", result1);
60 println!(" uppercase_clone.apply(\"rust\") = {}", result2);
61
62 println!();
63
64 // ArcTransformer TransformerOnce demonstration
65 println!("3. ArcTransformer TransformerOnce demonstration:");
66 let parse_and_double = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0) * 2);
67 let result = parse_and_double.apply("21".to_string());
68 println!(" parse_and_double.apply(\"21\") = {}", result);
69
70 // Thread safety demonstration
71 println!("4. ArcTransformer thread safety demonstration:");
72 let double = ArcTransformer::new(|x: i32| x * 2);
73 let double_arc = Arc::new(double);
74 let _double_clone = Arc::clone(&double_arc);
75
76 let handle = thread::spawn(move || {
77 // Create a new transformer in the thread to demonstrate thread safety
78 let new_double = ArcTransformer::new(|x: i32| x * 2);
79 new_double.apply(21)
80 });
81
82 let result = handle.join().unwrap();
83 println!(" Executed in thread: new_double.apply(21) = {}", result);
84
85 println!("\n=== Demo completed ===");
86}More examples
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}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
Creates a new named transformer.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn(T) -> R + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn(T) -> R + 'static,
Creates a new named transformer with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn clear_name(&mut self)
pub fn clear_name(&mut self)
Clears the name of this transformer.
Sourcepub fn identity() -> BoxTransformer<T, T>
pub fn identity() -> BoxTransformer<T, T>
Creates an identity transformer.
Creates a transformer that returns the input value unchanged. Useful for default values or placeholder implementations.
§Returns
Returns a new transformer instance that returns the input unchanged.
Examples found in repository?
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}Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>where
T: 'static,
R: 'static,
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>where
T: 'static,
R: 'static,
P: Predicate<T> + 'static,
Creates a conditional transformer that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the transformation operation
§Returns
Returns a conditional transformer that only executes when the
predicate returns true.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::transformers::*;
let transformer = BoxTransformer::new({
|value: i32| value * 2
});
let conditional = transformer.when(|value: &i32| *value > 0).or_else(|value: i32| value);
assert_eq!(conditional.apply(5), 10); // transformed
assert_eq!(conditional.apply(-1), -1); // identity (unchanged)Sourcepub fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>where
T: 'static,
R: 'static,
S: 'static,
F: Transformer<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>where
T: 'static,
R: 'static,
S: 'static,
F: Transformer<R, S> + 'static,
Chains execution with another transformer, executing the current transformer first, then the subsequent transformer.
§Parameters
after- The subsequent transformer to execute after the current transformer completes
§Returns
Returns a new transformer that executes the current transformer and the subsequent transformer in sequence.
§Examples
use qubit_function::transformers::*;
let transformer1 = BoxTransformer::new({
|value: i32| value + 1
});
let transformer2 = BoxTransformer::new({
|value: i32| value * 2
});
let chained = transformer1.and_then(transformer2);
assert_eq!(chained.apply(5), 12); // (5 + 1) * 2 = 12Examples found in repository?
20fn main() {
21 println!("=== FnTransformerOps Example ===\n");
22
23 // 1. Basic and_then composition
24 println!("1. Basic and_then composition:");
25 let double = |x: i32| x * 2;
26 let to_string = |x: i32| x.to_string();
27 let composed = double.and_then(to_string);
28 println!(
29 " double.and_then(to_string).apply(21) = {}",
30 composed.apply(21)
31 );
32 println!();
33
34 // 2. Chained and_then composition
35 println!("2. Chained and_then composition:");
36 let add_one = |x: i32| x + 1;
37 let double = |x: i32| x * 2;
38 let to_string = |x: i32| x.to_string();
39 let chained = add_one.and_then(double).and_then(to_string);
40 println!(
41 " add_one.and_then(double).and_then(to_string).apply(5) = {}",
42 chained.apply(5)
43 ); // (5 + 1) * 2 = 12
44 println!();
45
46 // 3. compose reverse composition
47 println!("3. compose reverse composition:");
48 let double = |x: i32| x * 2;
49 let add_one = |x: i32| x + 1;
50 let composed = double.compose(add_one);
51 println!(
52 " double.compose(add_one).apply(5) = {}",
53 composed.apply(5)
54 ); // (5 + 1) * 2 = 12
55 println!();
56
57 // 4. Conditional transformation when
58 println!("4. Conditional transformation when:");
59 let double = |x: i32| x * 2;
60 let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
61 println!(" double.when(x > 0).or_else(negate):");
62 println!(" transform(5) = {}", conditional.apply(5)); // 10
63 println!(" transform(-5) = {}", conditional.apply(-5)); // 5
64 println!();
65
66 // 5. Complex composition
67 println!("5. Complex composition:");
68 let add_one = |x: i32| x + 1;
69 let double = |x: i32| x * 2;
70 let triple = |x: i32| x * 3;
71 let to_string = |x: i32| x.to_string();
72
73 let complex = add_one
74 .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
75 .and_then(to_string);
76
77 println!(" add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
78 println!(" transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
79 println!(" transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
80 println!(" transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
81 println!();
82
83 // 6. Type conversion
84 println!("6. Type conversion:");
85 let to_string = |x: i32| x.to_string();
86 let get_length = |s: String| s.len();
87 let length_transformer = to_string.and_then(get_length);
88 println!(
89 " to_string.and_then(get_length).apply(12345) = {}",
90 length_transformer.apply(12345)
91 ); // 5
92 println!();
93
94 // 7. Closures that capture environment
95 println!("7. Closures that capture environment:");
96 let multiplier = 3;
97 let multiply = move |x: i32| x * multiplier;
98 let add_ten = |x: i32| x + 10;
99 let with_capture = multiply.and_then(add_ten);
100 println!(
101 " multiply(3).and_then(add_ten).apply(5) = {}",
102 with_capture.apply(5)
103 ); // 5 * 3 + 10 = 25
104 println!();
105
106 // 8. Function pointers
107 println!("8. Function pointers:");
108 fn double_fn(x: i32) -> i32 {
109 x * 2
110 }
111 fn add_one_fn(x: i32) -> i32 {
112 x + 1
113 }
114 let fn_composed = double_fn.and_then(add_one_fn);
115 println!(
116 " double_fn.and_then(add_one_fn).apply(5) = {}",
117 fn_composed.apply(5)
118 ); // 5 * 2 + 1 = 11
119 println!();
120
121 // 9. Multi-conditional transformation
122 println!("9. Multi-conditional transformation:");
123 let abs = |x: i32| x.abs();
124 let double = |x: i32| x * 2;
125 let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
126 println!(" abs.when(x < 0).or_else(double):");
127 println!(" transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
128 println!(" transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
129 println!(" transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
130 println!();
131
132 println!("=== Example completed ===");
133}More examples
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}Source§impl<T, R> BoxTransformer<T, R>
impl<T, R> BoxTransformer<T, R>
Sourcepub fn constant(value: R) -> BoxTransformer<T, R>where
R: Clone + 'static,
pub fn constant(value: R) -> BoxTransformer<T, R>where
R: Clone + 'static,
Creates a constant transformer
§Examples
/// rust /// use qubit_function::{BoxTransformer, Transformer}; /// /// let constant = BoxTransformer::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///
Examples found in repository?
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}Trait Implementations§
Source§impl<T, R> Debug for BoxTransformer<T, R>
impl<T, R> Debug for BoxTransformer<T, R>
Source§impl<T, R> Display for BoxTransformer<T, R>
impl<T, R> Display for BoxTransformer<T, R>
Source§impl<T, R> Transformer<T, R> for BoxTransformer<T, R>
impl<T, R> Transformer<T, R> for BoxTransformer<T, R>
Source§fn apply(&self, input: T) -> R
fn apply(&self, input: T) -> R
Source§fn into_box(self) -> BoxTransformer<T, R>
fn into_box(self) -> BoxTransformer<T, R>
Source§fn into_rc(self) -> RcTransformer<T, R>where
Self: 'static,
fn into_rc(self) -> RcTransformer<T, R>where
Self: 'static,
Source§fn into_once(self) -> BoxTransformerOnce<T, R>where
Self: 'static,
fn into_once(self) -> BoxTransformerOnce<T, R>where
Self: 'static,
BoxTransformerOnce. Read more