pub trait Transformer<T, R> {
// Required method
fn apply(&self, input: T) -> R;
// Provided methods
fn into_box(self) -> BoxTransformer<T, R>
where Self: Sized + 'static { ... }
fn into_rc(self) -> RcTransformer<T, R>
where Self: Sized + 'static { ... }
fn into_arc(self) -> ArcTransformer<T, R>
where Self: Sized + Send + Sync + 'static { ... }
fn into_fn(self) -> impl Fn(T) -> R
where Self: Sized + 'static { ... }
fn into_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static { ... }
fn to_box(&self) -> BoxTransformer<T, R>
where Self: Clone + 'static { ... }
fn to_rc(&self) -> RcTransformer<T, R>
where Self: Clone + 'static { ... }
fn to_arc(&self) -> ArcTransformer<T, R>
where Self: Clone + Send + Sync + 'static { ... }
fn to_fn(&self) -> impl Fn(T) -> R
where Self: Clone + 'static { ... }
fn to_once(&self) -> BoxTransformerOnce<T, R>
where Self: Clone + 'static { ... }
}Expand description
Transformer trait - transforms values from type T to type R
Defines the behavior of a transformation: converting a value of type T
to a value of type R by consuming the input. This is analogous to
Fn(T) -> R in Rust’s standard library.
§Type Parameters
T- The type of the input value (consumed)R- The type of the output value
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxTransformer<T, R>where
Self: Sized + 'static,
fn into_box(self) -> BoxTransformer<T, R>where
Self: Sized + 'static,
Converts to BoxTransformer
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation wraps self in a Box and creates a
BoxTransformer. Types can override this method to provide more
efficient conversions.
§Returns
Returns BoxTransformer<T, R>
Examples found in repository?
20fn test_transformer() {
21 // Test function pointer
22 fn double(x: i32) -> i32 {
23 x * 2
24 }
25 assert_eq!(double.apply(21), 42);
26 println!("✓ Function pointer test passed: double.apply(21) = 42");
27
28 // Test closure
29 let triple = |x: i32| x * 3;
30 assert_eq!(triple.apply(14), 42);
31 println!("✓ Closure test passed: triple.apply(14) = 42");
32
33 // Test conversion to BoxTransformer
34 let quad = |x: i32| x * 4;
35 let boxed = Transformer::into_box(quad);
36 assert_eq!(boxed.apply(10), 40);
37 println!("✓ into_box() test passed");
38
39 // Test conversion to RcTransformer
40 let times_five = |x: i32| x * 5;
41 let rc = Transformer::into_rc(times_five);
42 assert_eq!(rc.apply(8), 40);
43 println!("✓ into_rc() test passed");
44
45 // Test conversion to ArcTransformer
46 let times_six = |x: i32| x * 6;
47 let arc = Transformer::into_arc(times_six);
48 assert_eq!(arc.apply(7), 42);
49 println!("✓ into_arc() test passed");
50}More examples
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}22fn main() {
23 println!("=== TransformerOnce Specialized Methods Demo ===\n");
24
25 // ============================================================================
26 // ArcTransformer TransformerOnce specialized methods
27 // ============================================================================
28
29 println!("1. ArcTransformer TransformerOnce specialized methods:");
30
31 let arc_double = ArcTransformer::new(|x: i32| x * 2);
32
33 // Test into_box - consumes self
34 let boxed_once = arc_double.clone().into_box();
35 println!(" ArcTransformer::into_box(): {}", boxed_once.apply(21));
36
37 // Test into_fn - consumes self
38 let fn_once = arc_double.clone().into_fn();
39 println!(" ArcTransformer::into_fn(): {}", fn_once(21));
40
41 // Test to_box - borrows self
42 let boxed_once_borrowed = arc_double.to_box();
43 println!(
44 " ArcTransformer::to_box(): {}",
45 boxed_once_borrowed.apply(21)
46 );
47
48 // Test to_fn - borrows self
49 let fn_once_borrowed = arc_double.to_fn();
50 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
51
52 // Original transformer still usable after to_xxx methods
53 println!(
54 " Original ArcTransformer still works: {}",
55 arc_double.apply(21)
56 );
57
58 println!();
59
60 // ============================================================================
61 // RcTransformer TransformerOnce specialized methods
62 // ============================================================================
63
64 println!("2. RcTransformer TransformerOnce specialized methods:");
65
66 let rc_triple = RcTransformer::new(|x: i32| x * 3);
67
68 // Test into_box - consumes self
69 let boxed_once = rc_triple.clone().into_box();
70 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
71
72 // Test into_fn - consumes self
73 let fn_once = rc_triple.clone().into_fn();
74 println!(" RcTransformer::into_fn(): {}", fn_once(14));
75
76 // Test to_box - borrows self
77 let boxed_once_borrowed = rc_triple.to_box();
78 println!(
79 " RcTransformer::to_box(): {}",
80 boxed_once_borrowed.apply(14)
81 );
82
83 // Test to_fn - borrows self
84 let fn_once_borrowed = rc_triple.to_fn();
85 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
86
87 // Original transformer still usable after to_xxx methods
88 println!(
89 " Original RcTransformer still works: {}",
90 rc_triple.apply(14)
91 );
92
93 println!();
94
95 // ============================================================================
96 // Comparison with default implementations
97 // ============================================================================
98
99 println!("3. Performance comparison (specialized vs default):");
100
101 let arc_square = ArcTransformer::new(|x: i32| x * x);
102
103 // Using specialized method (more efficient)
104 let specialized_box = arc_square.clone().into_box();
105 println!(" Specialized into_box: {}", specialized_box.apply(5));
106
107 // Using default implementation (less efficient)
108 let default_box = arc_square.clone().into_box();
109 println!(" Default into_box: {}", default_box.apply(5));
110
111 println!();
112
113 // ============================================================================
114 // Thread safety demonstration for ArcTransformer
115 // ============================================================================
116
117 println!("4. Thread safety with ArcTransformer:");
118
119 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
120
121 // Clone for thread safety
122 let arc_clone = arc_shared.clone();
123
124 // Use in different thread context (simulated)
125 let handle = std::thread::spawn(move || {
126 let boxed = arc_clone.into_box();
127 boxed.apply(50)
128 });
129
130 let result = handle.join().unwrap();
131 println!(" Thread-safe ArcTransformer result: {}", result);
132
133 // Original still usable
134 println!(
135 " Original ArcTransformer still works: {}",
136 arc_shared.apply(50)
137 );
138
139 println!();
140
141 // ============================================================================
142 // String transformation example
143 // ============================================================================
144
145 println!("5. String transformation with specialized methods:");
146
147 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
148
149 // Test with string input
150 let test_string = "hello world".to_string();
151
152 // Using specialized methods
153 let boxed_upper = arc_uppercase.clone().into_box();
154 let result = boxed_upper.apply(test_string.clone());
155 println!(
156 " String transformation: '{}' -> '{}'",
157 test_string, result
158 );
159
160 // Using to_xxx methods (borrowing)
161 let fn_upper = arc_uppercase.to_fn();
162 let result2 = fn_upper(test_string.clone());
163 println!(
164 " String transformation (borrowed): '{}' -> '{}'",
165 test_string, result2
166 );
167
168 // Original still usable
169 println!(
170 " Original ArcTransformer still works: '{}'",
171 arc_uppercase.apply(test_string)
172 );
173
174 println!("\n=== Demo completed successfully! ===");
175}Sourcefn into_rc(self) -> RcTransformer<T, R>where
Self: Sized + 'static,
fn into_rc(self) -> RcTransformer<T, R>where
Self: Sized + 'static,
Converts to RcTransformer
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation wraps self in an Rc and creates an
RcTransformer. Types can override this method to provide more
efficient conversions.
§Returns
Returns RcTransformer<T, R>
Examples found in repository?
20fn test_transformer() {
21 // Test function pointer
22 fn double(x: i32) -> i32 {
23 x * 2
24 }
25 assert_eq!(double.apply(21), 42);
26 println!("✓ Function pointer test passed: double.apply(21) = 42");
27
28 // Test closure
29 let triple = |x: i32| x * 3;
30 assert_eq!(triple.apply(14), 42);
31 println!("✓ Closure test passed: triple.apply(14) = 42");
32
33 // Test conversion to BoxTransformer
34 let quad = |x: i32| x * 4;
35 let boxed = Transformer::into_box(quad);
36 assert_eq!(boxed.apply(10), 40);
37 println!("✓ into_box() test passed");
38
39 // Test conversion to RcTransformer
40 let times_five = |x: i32| x * 5;
41 let rc = Transformer::into_rc(times_five);
42 assert_eq!(rc.apply(8), 40);
43 println!("✓ into_rc() test passed");
44
45 // Test conversion to ArcTransformer
46 let times_six = |x: i32| x * 6;
47 let arc = Transformer::into_arc(times_six);
48 assert_eq!(arc.apply(7), 42);
49 println!("✓ into_arc() test passed");
50}Sourcefn into_arc(self) -> ArcTransformer<T, R>
fn into_arc(self) -> ArcTransformer<T, R>
Converts to ArcTransformer
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation wraps self in an Arc and creates
an ArcTransformer. Types can override this method to provide
more efficient conversions.
§Returns
Returns ArcTransformer<T, R>
Examples found in repository?
20fn test_transformer() {
21 // Test function pointer
22 fn double(x: i32) -> i32 {
23 x * 2
24 }
25 assert_eq!(double.apply(21), 42);
26 println!("✓ Function pointer test passed: double.apply(21) = 42");
27
28 // Test closure
29 let triple = |x: i32| x * 3;
30 assert_eq!(triple.apply(14), 42);
31 println!("✓ Closure test passed: triple.apply(14) = 42");
32
33 // Test conversion to BoxTransformer
34 let quad = |x: i32| x * 4;
35 let boxed = Transformer::into_box(quad);
36 assert_eq!(boxed.apply(10), 40);
37 println!("✓ into_box() test passed");
38
39 // Test conversion to RcTransformer
40 let times_five = |x: i32| x * 5;
41 let rc = Transformer::into_rc(times_five);
42 assert_eq!(rc.apply(8), 40);
43 println!("✓ into_rc() test passed");
44
45 // Test conversion to ArcTransformer
46 let times_six = |x: i32| x * 6;
47 let arc = Transformer::into_arc(times_six);
48 assert_eq!(arc.apply(7), 42);
49 println!("✓ into_arc() test passed");
50}Sourcefn into_fn(self) -> impl Fn(T) -> Rwhere
Self: Sized + 'static,
fn into_fn(self) -> impl Fn(T) -> Rwhere
Self: Sized + 'static,
Converts transformer to a closure
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation creates a closure that captures self
and calls its transform method. Types can override this method
to provide more efficient conversions.
§Returns
Returns a closure that implements Fn(T) -> R
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
22fn main() {
23 println!("=== TransformerOnce Specialized Methods Demo ===\n");
24
25 // ============================================================================
26 // ArcTransformer TransformerOnce specialized methods
27 // ============================================================================
28
29 println!("1. ArcTransformer TransformerOnce specialized methods:");
30
31 let arc_double = ArcTransformer::new(|x: i32| x * 2);
32
33 // Test into_box - consumes self
34 let boxed_once = arc_double.clone().into_box();
35 println!(" ArcTransformer::into_box(): {}", boxed_once.apply(21));
36
37 // Test into_fn - consumes self
38 let fn_once = arc_double.clone().into_fn();
39 println!(" ArcTransformer::into_fn(): {}", fn_once(21));
40
41 // Test to_box - borrows self
42 let boxed_once_borrowed = arc_double.to_box();
43 println!(
44 " ArcTransformer::to_box(): {}",
45 boxed_once_borrowed.apply(21)
46 );
47
48 // Test to_fn - borrows self
49 let fn_once_borrowed = arc_double.to_fn();
50 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
51
52 // Original transformer still usable after to_xxx methods
53 println!(
54 " Original ArcTransformer still works: {}",
55 arc_double.apply(21)
56 );
57
58 println!();
59
60 // ============================================================================
61 // RcTransformer TransformerOnce specialized methods
62 // ============================================================================
63
64 println!("2. RcTransformer TransformerOnce specialized methods:");
65
66 let rc_triple = RcTransformer::new(|x: i32| x * 3);
67
68 // Test into_box - consumes self
69 let boxed_once = rc_triple.clone().into_box();
70 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
71
72 // Test into_fn - consumes self
73 let fn_once = rc_triple.clone().into_fn();
74 println!(" RcTransformer::into_fn(): {}", fn_once(14));
75
76 // Test to_box - borrows self
77 let boxed_once_borrowed = rc_triple.to_box();
78 println!(
79 " RcTransformer::to_box(): {}",
80 boxed_once_borrowed.apply(14)
81 );
82
83 // Test to_fn - borrows self
84 let fn_once_borrowed = rc_triple.to_fn();
85 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
86
87 // Original transformer still usable after to_xxx methods
88 println!(
89 " Original RcTransformer still works: {}",
90 rc_triple.apply(14)
91 );
92
93 println!();
94
95 // ============================================================================
96 // Comparison with default implementations
97 // ============================================================================
98
99 println!("3. Performance comparison (specialized vs default):");
100
101 let arc_square = ArcTransformer::new(|x: i32| x * x);
102
103 // Using specialized method (more efficient)
104 let specialized_box = arc_square.clone().into_box();
105 println!(" Specialized into_box: {}", specialized_box.apply(5));
106
107 // Using default implementation (less efficient)
108 let default_box = arc_square.clone().into_box();
109 println!(" Default into_box: {}", default_box.apply(5));
110
111 println!();
112
113 // ============================================================================
114 // Thread safety demonstration for ArcTransformer
115 // ============================================================================
116
117 println!("4. Thread safety with ArcTransformer:");
118
119 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
120
121 // Clone for thread safety
122 let arc_clone = arc_shared.clone();
123
124 // Use in different thread context (simulated)
125 let handle = std::thread::spawn(move || {
126 let boxed = arc_clone.into_box();
127 boxed.apply(50)
128 });
129
130 let result = handle.join().unwrap();
131 println!(" Thread-safe ArcTransformer result: {}", result);
132
133 // Original still usable
134 println!(
135 " Original ArcTransformer still works: {}",
136 arc_shared.apply(50)
137 );
138
139 println!();
140
141 // ============================================================================
142 // String transformation example
143 // ============================================================================
144
145 println!("5. String transformation with specialized methods:");
146
147 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
148
149 // Test with string input
150 let test_string = "hello world".to_string();
151
152 // Using specialized methods
153 let boxed_upper = arc_uppercase.clone().into_box();
154 let result = boxed_upper.apply(test_string.clone());
155 println!(
156 " String transformation: '{}' -> '{}'",
157 test_string, result
158 );
159
160 // Using to_xxx methods (borrowing)
161 let fn_upper = arc_uppercase.to_fn();
162 let result2 = fn_upper(test_string.clone());
163 println!(
164 " String transformation (borrowed): '{}' -> '{}'",
165 test_string, result2
166 );
167
168 // Original still usable
169 println!(
170 " Original ArcTransformer still works: '{}'",
171 arc_uppercase.apply(test_string)
172 );
173
174 println!("\n=== Demo completed successfully! ===");
175}Sourcefn into_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
fn into_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
Converts to BoxTransformerOnce.
This method has a default implementation that wraps the
transformer in a BoxTransformerOnce. Custom implementations
can override this method for optimization purposes.
§Returns
A new BoxTransformerOnce<T, R> instance
§Examples
use qubit_function::{Transformer, TransformerOnce};
let closure = |x: i32| x * 2;
let once = closure.into_once();
assert_eq!(once.apply(5), 10);Sourcefn to_box(&self) -> BoxTransformer<T, R>where
Self: Clone + 'static,
fn to_box(&self) -> BoxTransformer<T, R>where
Self: Clone + 'static,
Converts to BoxTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new BoxTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns BoxTransformer<T, R>
§Examples
use qubit_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let boxed = double.to_box();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(boxed.apply(21), 42);Examples found in repository?
22fn main() {
23 println!("=== TransformerOnce Specialized Methods Demo ===\n");
24
25 // ============================================================================
26 // ArcTransformer TransformerOnce specialized methods
27 // ============================================================================
28
29 println!("1. ArcTransformer TransformerOnce specialized methods:");
30
31 let arc_double = ArcTransformer::new(|x: i32| x * 2);
32
33 // Test into_box - consumes self
34 let boxed_once = arc_double.clone().into_box();
35 println!(" ArcTransformer::into_box(): {}", boxed_once.apply(21));
36
37 // Test into_fn - consumes self
38 let fn_once = arc_double.clone().into_fn();
39 println!(" ArcTransformer::into_fn(): {}", fn_once(21));
40
41 // Test to_box - borrows self
42 let boxed_once_borrowed = arc_double.to_box();
43 println!(
44 " ArcTransformer::to_box(): {}",
45 boxed_once_borrowed.apply(21)
46 );
47
48 // Test to_fn - borrows self
49 let fn_once_borrowed = arc_double.to_fn();
50 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
51
52 // Original transformer still usable after to_xxx methods
53 println!(
54 " Original ArcTransformer still works: {}",
55 arc_double.apply(21)
56 );
57
58 println!();
59
60 // ============================================================================
61 // RcTransformer TransformerOnce specialized methods
62 // ============================================================================
63
64 println!("2. RcTransformer TransformerOnce specialized methods:");
65
66 let rc_triple = RcTransformer::new(|x: i32| x * 3);
67
68 // Test into_box - consumes self
69 let boxed_once = rc_triple.clone().into_box();
70 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
71
72 // Test into_fn - consumes self
73 let fn_once = rc_triple.clone().into_fn();
74 println!(" RcTransformer::into_fn(): {}", fn_once(14));
75
76 // Test to_box - borrows self
77 let boxed_once_borrowed = rc_triple.to_box();
78 println!(
79 " RcTransformer::to_box(): {}",
80 boxed_once_borrowed.apply(14)
81 );
82
83 // Test to_fn - borrows self
84 let fn_once_borrowed = rc_triple.to_fn();
85 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
86
87 // Original transformer still usable after to_xxx methods
88 println!(
89 " Original RcTransformer still works: {}",
90 rc_triple.apply(14)
91 );
92
93 println!();
94
95 // ============================================================================
96 // Comparison with default implementations
97 // ============================================================================
98
99 println!("3. Performance comparison (specialized vs default):");
100
101 let arc_square = ArcTransformer::new(|x: i32| x * x);
102
103 // Using specialized method (more efficient)
104 let specialized_box = arc_square.clone().into_box();
105 println!(" Specialized into_box: {}", specialized_box.apply(5));
106
107 // Using default implementation (less efficient)
108 let default_box = arc_square.clone().into_box();
109 println!(" Default into_box: {}", default_box.apply(5));
110
111 println!();
112
113 // ============================================================================
114 // Thread safety demonstration for ArcTransformer
115 // ============================================================================
116
117 println!("4. Thread safety with ArcTransformer:");
118
119 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
120
121 // Clone for thread safety
122 let arc_clone = arc_shared.clone();
123
124 // Use in different thread context (simulated)
125 let handle = std::thread::spawn(move || {
126 let boxed = arc_clone.into_box();
127 boxed.apply(50)
128 });
129
130 let result = handle.join().unwrap();
131 println!(" Thread-safe ArcTransformer result: {}", result);
132
133 // Original still usable
134 println!(
135 " Original ArcTransformer still works: {}",
136 arc_shared.apply(50)
137 );
138
139 println!();
140
141 // ============================================================================
142 // String transformation example
143 // ============================================================================
144
145 println!("5. String transformation with specialized methods:");
146
147 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
148
149 // Test with string input
150 let test_string = "hello world".to_string();
151
152 // Using specialized methods
153 let boxed_upper = arc_uppercase.clone().into_box();
154 let result = boxed_upper.apply(test_string.clone());
155 println!(
156 " String transformation: '{}' -> '{}'",
157 test_string, result
158 );
159
160 // Using to_xxx methods (borrowing)
161 let fn_upper = arc_uppercase.to_fn();
162 let result2 = fn_upper(test_string.clone());
163 println!(
164 " String transformation (borrowed): '{}' -> '{}'",
165 test_string, result2
166 );
167
168 // Original still usable
169 println!(
170 " Original ArcTransformer still works: '{}'",
171 arc_uppercase.apply(test_string)
172 );
173
174 println!("\n=== Demo completed successfully! ===");
175}Sourcefn to_rc(&self) -> RcTransformer<T, R>where
Self: Clone + 'static,
fn to_rc(&self) -> RcTransformer<T, R>where
Self: Clone + 'static,
Converts to RcTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new RcTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns RcTransformer<T, R>
§Examples
use qubit_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let rc = double.to_rc();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(rc.apply(21), 42);Sourcefn to_arc(&self) -> ArcTransformer<T, R>
fn to_arc(&self) -> ArcTransformer<T, R>
Converts to ArcTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new ArcTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns ArcTransformer<T, R>
§Examples
use qubit_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let arc = double.to_arc();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(arc.apply(21), 42);Sourcefn to_fn(&self) -> impl Fn(T) -> Rwhere
Self: Clone + 'static,
fn to_fn(&self) -> impl Fn(T) -> Rwhere
Self: Clone + 'static,
Converts transformer to a closure without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a closure that captures a
clone of self and calls its transform method. Types can
override this method to provide more efficient conversions.
§Returns
Returns a closure that implements Fn(T) -> R
§Examples
use qubit_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let closure = double.to_fn();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(closure(21), 42);Examples found in repository?
22fn main() {
23 println!("=== TransformerOnce Specialized Methods Demo ===\n");
24
25 // ============================================================================
26 // ArcTransformer TransformerOnce specialized methods
27 // ============================================================================
28
29 println!("1. ArcTransformer TransformerOnce specialized methods:");
30
31 let arc_double = ArcTransformer::new(|x: i32| x * 2);
32
33 // Test into_box - consumes self
34 let boxed_once = arc_double.clone().into_box();
35 println!(" ArcTransformer::into_box(): {}", boxed_once.apply(21));
36
37 // Test into_fn - consumes self
38 let fn_once = arc_double.clone().into_fn();
39 println!(" ArcTransformer::into_fn(): {}", fn_once(21));
40
41 // Test to_box - borrows self
42 let boxed_once_borrowed = arc_double.to_box();
43 println!(
44 " ArcTransformer::to_box(): {}",
45 boxed_once_borrowed.apply(21)
46 );
47
48 // Test to_fn - borrows self
49 let fn_once_borrowed = arc_double.to_fn();
50 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
51
52 // Original transformer still usable after to_xxx methods
53 println!(
54 " Original ArcTransformer still works: {}",
55 arc_double.apply(21)
56 );
57
58 println!();
59
60 // ============================================================================
61 // RcTransformer TransformerOnce specialized methods
62 // ============================================================================
63
64 println!("2. RcTransformer TransformerOnce specialized methods:");
65
66 let rc_triple = RcTransformer::new(|x: i32| x * 3);
67
68 // Test into_box - consumes self
69 let boxed_once = rc_triple.clone().into_box();
70 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
71
72 // Test into_fn - consumes self
73 let fn_once = rc_triple.clone().into_fn();
74 println!(" RcTransformer::into_fn(): {}", fn_once(14));
75
76 // Test to_box - borrows self
77 let boxed_once_borrowed = rc_triple.to_box();
78 println!(
79 " RcTransformer::to_box(): {}",
80 boxed_once_borrowed.apply(14)
81 );
82
83 // Test to_fn - borrows self
84 let fn_once_borrowed = rc_triple.to_fn();
85 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
86
87 // Original transformer still usable after to_xxx methods
88 println!(
89 " Original RcTransformer still works: {}",
90 rc_triple.apply(14)
91 );
92
93 println!();
94
95 // ============================================================================
96 // Comparison with default implementations
97 // ============================================================================
98
99 println!("3. Performance comparison (specialized vs default):");
100
101 let arc_square = ArcTransformer::new(|x: i32| x * x);
102
103 // Using specialized method (more efficient)
104 let specialized_box = arc_square.clone().into_box();
105 println!(" Specialized into_box: {}", specialized_box.apply(5));
106
107 // Using default implementation (less efficient)
108 let default_box = arc_square.clone().into_box();
109 println!(" Default into_box: {}", default_box.apply(5));
110
111 println!();
112
113 // ============================================================================
114 // Thread safety demonstration for ArcTransformer
115 // ============================================================================
116
117 println!("4. Thread safety with ArcTransformer:");
118
119 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
120
121 // Clone for thread safety
122 let arc_clone = arc_shared.clone();
123
124 // Use in different thread context (simulated)
125 let handle = std::thread::spawn(move || {
126 let boxed = arc_clone.into_box();
127 boxed.apply(50)
128 });
129
130 let result = handle.join().unwrap();
131 println!(" Thread-safe ArcTransformer result: {}", result);
132
133 // Original still usable
134 println!(
135 " Original ArcTransformer still works: {}",
136 arc_shared.apply(50)
137 );
138
139 println!();
140
141 // ============================================================================
142 // String transformation example
143 // ============================================================================
144
145 println!("5. String transformation with specialized methods:");
146
147 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
148
149 // Test with string input
150 let test_string = "hello world".to_string();
151
152 // Using specialized methods
153 let boxed_upper = arc_uppercase.clone().into_box();
154 let result = boxed_upper.apply(test_string.clone());
155 println!(
156 " String transformation: '{}' -> '{}'",
157 test_string, result
158 );
159
160 // Using to_xxx methods (borrowing)
161 let fn_upper = arc_uppercase.to_fn();
162 let result2 = fn_upper(test_string.clone());
163 println!(
164 " String transformation (borrowed): '{}' -> '{}'",
165 test_string, result2
166 );
167
168 // Original still usable
169 println!(
170 " Original ArcTransformer still works: '{}'",
171 arc_uppercase.apply(test_string)
172 );
173
174 println!("\n=== Demo completed successfully! ===");
175}Sourcefn to_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
fn to_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
Converts to BoxTransformerOnce without consuming self
⚠️ Requires Clone: This method requires Self to implement Clone.
Clones the current transformer and converts the clone to a one-time transformer.
§Returns
Returns a BoxTransformerOnce<T, R>