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().expect("thread should not panic");
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!("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}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!("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}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!(" double.and_then(to_string).apply(21) = {}", composed.apply(21));
29 println!();
30
31 // 2. Chained and_then composition
32 println!("2. Chained and_then composition:");
33 let add_one = |x: i32| x + 1;
34 let double = |x: i32| x * 2;
35 let to_string = |x: i32| x.to_string();
36 let chained = add_one.and_then(double).and_then(to_string);
37 println!(
38 " add_one.and_then(double).and_then(to_string).apply(5) = {}",
39 chained.apply(5)
40 ); // (5 + 1) * 2 = 12
41 println!();
42
43 // 3. compose reverse composition
44 println!("3. compose reverse composition:");
45 let double = |x: i32| x * 2;
46 let add_one = |x: i32| x + 1;
47 let composed = double.compose(add_one);
48 println!(" double.compose(add_one).apply(5) = {}", composed.apply(5)); // (5 + 1) * 2 = 12
49 println!();
50
51 // 4. Conditional transformation when
52 println!("4. Conditional transformation when:");
53 let double = |x: i32| x * 2;
54 let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
55 println!(" double.when(x > 0).or_else(negate):");
56 println!(" transform(5) = {}", conditional.apply(5)); // 10
57 println!(" transform(-5) = {}", conditional.apply(-5)); // 5
58 println!();
59
60 // 5. Complex composition
61 println!("5. Complex composition:");
62 let add_one = |x: i32| x + 1;
63 let double = |x: i32| x * 2;
64 let triple = |x: i32| x * 3;
65 let to_string = |x: i32| x.to_string();
66
67 let complex = add_one
68 .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
69 .and_then(to_string);
70
71 println!(" add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
72 println!(" transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
73 println!(" transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
74 println!(" transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
75 println!();
76
77 // 6. Type conversion
78 println!("6. Type conversion:");
79 let to_string = |x: i32| x.to_string();
80 let get_length = |s: String| s.len();
81 let length_transformer = to_string.and_then(get_length);
82 println!(
83 " to_string.and_then(get_length).apply(12345) = {}",
84 length_transformer.apply(12345)
85 ); // 5
86 println!();
87
88 // 7. Closures that capture environment
89 println!("7. Closures that capture environment:");
90 let multiplier = 3;
91 let multiply = move |x: i32| x * multiplier;
92 let add_ten = |x: i32| x + 10;
93 let with_capture = multiply.and_then(add_ten);
94 println!(" multiply(3).and_then(add_ten).apply(5) = {}", with_capture.apply(5)); // 5 * 3 + 10 = 25
95 println!();
96
97 // 8. Function pointers
98 println!("8. Function pointers:");
99 fn double_fn(x: i32) -> i32 {
100 x * 2
101 }
102 fn add_one_fn(x: i32) -> i32 {
103 x + 1
104 }
105 let fn_composed = double_fn.and_then(add_one_fn);
106 println!(" double_fn.and_then(add_one_fn).apply(5) = {}", fn_composed.apply(5)); // 5 * 2 + 1 = 11
107 println!();
108
109 // 9. Multi-conditional transformation
110 println!("9. Multi-conditional transformation:");
111 let abs = |x: i32| x.abs();
112 let double = |x: i32| x * 2;
113 let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
114 println!(" abs.when(x < 0).or_else(double):");
115 println!(" transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
116 println!(" transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
117 println!(" transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
118 println!();
119
120 println!("=== Example completed ===");
121}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!("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}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!("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}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