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().expect("thread should not panic");
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!(" ArcTransformer::to_box(): {}", boxed_once_borrowed.apply(21));
44
45 // Test to_fn - borrows self
46 let fn_once_borrowed = arc_double.to_fn();
47 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
48
49 // Original transformer still usable after to_xxx methods
50 println!(" Original ArcTransformer still works: {}", arc_double.apply(21));
51
52 println!();
53
54 // ============================================================================
55 // RcTransformer TransformerOnce specialized methods
56 // ============================================================================
57
58 println!("2. RcTransformer TransformerOnce specialized methods:");
59
60 let rc_triple = RcTransformer::new(|x: i32| x * 3);
61
62 // Test into_box - consumes self
63 let boxed_once = rc_triple.clone().into_box();
64 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
65
66 // Test into_fn - consumes self
67 let fn_once = rc_triple.clone().into_fn();
68 println!(" RcTransformer::into_fn(): {}", fn_once(14));
69
70 // Test to_box - borrows self
71 let boxed_once_borrowed = rc_triple.to_box();
72 println!(" RcTransformer::to_box(): {}", boxed_once_borrowed.apply(14));
73
74 // Test to_fn - borrows self
75 let fn_once_borrowed = rc_triple.to_fn();
76 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
77
78 // Original transformer still usable after to_xxx methods
79 println!(" Original RcTransformer still works: {}", rc_triple.apply(14));
80
81 println!();
82
83 // ============================================================================
84 // Comparison with default implementations
85 // ============================================================================
86
87 println!("3. Performance comparison (specialized vs default):");
88
89 let arc_square = ArcTransformer::new(|x: i32| x * x);
90
91 // Using specialized method (more efficient)
92 let specialized_box = arc_square.clone().into_box();
93 println!(" Specialized into_box: {}", specialized_box.apply(5));
94
95 // Using default implementation (less efficient)
96 let default_box = arc_square.clone().into_box();
97 println!(" Default into_box: {}", default_box.apply(5));
98
99 println!();
100
101 // ============================================================================
102 // Thread safety demonstration for ArcTransformer
103 // ============================================================================
104
105 println!("4. Thread safety with ArcTransformer:");
106
107 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
108
109 // Clone for thread safety
110 let arc_clone = arc_shared.clone();
111
112 // Use in different thread context (simulated)
113 let handle = std::thread::spawn(move || {
114 let boxed = arc_clone.into_box();
115 boxed.apply(50)
116 });
117
118 let result = handle.join().expect("thread should not panic");
119 println!(" Thread-safe ArcTransformer result: {}", result);
120
121 // Original still usable
122 println!(" Original ArcTransformer still works: {}", arc_shared.apply(50));
123
124 println!();
125
126 // ============================================================================
127 // String transformation example
128 // ============================================================================
129
130 println!("5. String transformation with specialized methods:");
131
132 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
133
134 // Test with string input
135 let test_string = "hello world".to_string();
136
137 // Using specialized methods
138 let boxed_upper = arc_uppercase.clone().into_box();
139 let result = boxed_upper.apply(test_string.clone());
140 println!(" String transformation: '{}' -> '{}'", test_string, result);
141
142 // Using to_xxx methods (borrowing)
143 let fn_upper = arc_uppercase.to_fn();
144 let result2 = fn_upper(test_string.clone());
145 println!(
146 " String transformation (borrowed): '{}' -> '{}'",
147 test_string, result2
148 );
149
150 // Original still usable
151 println!(
152 " Original ArcTransformer still works: '{}'",
153 arc_uppercase.apply(test_string)
154 );
155
156 println!("\n=== Demo completed successfully! ===");
157}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().expect("thread should not panic");
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!(" ArcTransformer::to_box(): {}", boxed_once_borrowed.apply(21));
44
45 // Test to_fn - borrows self
46 let fn_once_borrowed = arc_double.to_fn();
47 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
48
49 // Original transformer still usable after to_xxx methods
50 println!(" Original ArcTransformer still works: {}", arc_double.apply(21));
51
52 println!();
53
54 // ============================================================================
55 // RcTransformer TransformerOnce specialized methods
56 // ============================================================================
57
58 println!("2. RcTransformer TransformerOnce specialized methods:");
59
60 let rc_triple = RcTransformer::new(|x: i32| x * 3);
61
62 // Test into_box - consumes self
63 let boxed_once = rc_triple.clone().into_box();
64 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
65
66 // Test into_fn - consumes self
67 let fn_once = rc_triple.clone().into_fn();
68 println!(" RcTransformer::into_fn(): {}", fn_once(14));
69
70 // Test to_box - borrows self
71 let boxed_once_borrowed = rc_triple.to_box();
72 println!(" RcTransformer::to_box(): {}", boxed_once_borrowed.apply(14));
73
74 // Test to_fn - borrows self
75 let fn_once_borrowed = rc_triple.to_fn();
76 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
77
78 // Original transformer still usable after to_xxx methods
79 println!(" Original RcTransformer still works: {}", rc_triple.apply(14));
80
81 println!();
82
83 // ============================================================================
84 // Comparison with default implementations
85 // ============================================================================
86
87 println!("3. Performance comparison (specialized vs default):");
88
89 let arc_square = ArcTransformer::new(|x: i32| x * x);
90
91 // Using specialized method (more efficient)
92 let specialized_box = arc_square.clone().into_box();
93 println!(" Specialized into_box: {}", specialized_box.apply(5));
94
95 // Using default implementation (less efficient)
96 let default_box = arc_square.clone().into_box();
97 println!(" Default into_box: {}", default_box.apply(5));
98
99 println!();
100
101 // ============================================================================
102 // Thread safety demonstration for ArcTransformer
103 // ============================================================================
104
105 println!("4. Thread safety with ArcTransformer:");
106
107 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
108
109 // Clone for thread safety
110 let arc_clone = arc_shared.clone();
111
112 // Use in different thread context (simulated)
113 let handle = std::thread::spawn(move || {
114 let boxed = arc_clone.into_box();
115 boxed.apply(50)
116 });
117
118 let result = handle.join().expect("thread should not panic");
119 println!(" Thread-safe ArcTransformer result: {}", result);
120
121 // Original still usable
122 println!(" Original ArcTransformer still works: {}", arc_shared.apply(50));
123
124 println!();
125
126 // ============================================================================
127 // String transformation example
128 // ============================================================================
129
130 println!("5. String transformation with specialized methods:");
131
132 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
133
134 // Test with string input
135 let test_string = "hello world".to_string();
136
137 // Using specialized methods
138 let boxed_upper = arc_uppercase.clone().into_box();
139 let result = boxed_upper.apply(test_string.clone());
140 println!(" String transformation: '{}' -> '{}'", test_string, result);
141
142 // Using to_xxx methods (borrowing)
143 let fn_upper = arc_uppercase.to_fn();
144 let result2 = fn_upper(test_string.clone());
145 println!(
146 " String transformation (borrowed): '{}' -> '{}'",
147 test_string, result2
148 );
149
150 // Original still usable
151 println!(
152 " Original ArcTransformer still works: '{}'",
153 arc_uppercase.apply(test_string)
154 );
155
156 println!("\n=== Demo completed successfully! ===");
157}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!(" ArcTransformer::to_box(): {}", boxed_once_borrowed.apply(21));
44
45 // Test to_fn - borrows self
46 let fn_once_borrowed = arc_double.to_fn();
47 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
48
49 // Original transformer still usable after to_xxx methods
50 println!(" Original ArcTransformer still works: {}", arc_double.apply(21));
51
52 println!();
53
54 // ============================================================================
55 // RcTransformer TransformerOnce specialized methods
56 // ============================================================================
57
58 println!("2. RcTransformer TransformerOnce specialized methods:");
59
60 let rc_triple = RcTransformer::new(|x: i32| x * 3);
61
62 // Test into_box - consumes self
63 let boxed_once = rc_triple.clone().into_box();
64 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
65
66 // Test into_fn - consumes self
67 let fn_once = rc_triple.clone().into_fn();
68 println!(" RcTransformer::into_fn(): {}", fn_once(14));
69
70 // Test to_box - borrows self
71 let boxed_once_borrowed = rc_triple.to_box();
72 println!(" RcTransformer::to_box(): {}", boxed_once_borrowed.apply(14));
73
74 // Test to_fn - borrows self
75 let fn_once_borrowed = rc_triple.to_fn();
76 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
77
78 // Original transformer still usable after to_xxx methods
79 println!(" Original RcTransformer still works: {}", rc_triple.apply(14));
80
81 println!();
82
83 // ============================================================================
84 // Comparison with default implementations
85 // ============================================================================
86
87 println!("3. Performance comparison (specialized vs default):");
88
89 let arc_square = ArcTransformer::new(|x: i32| x * x);
90
91 // Using specialized method (more efficient)
92 let specialized_box = arc_square.clone().into_box();
93 println!(" Specialized into_box: {}", specialized_box.apply(5));
94
95 // Using default implementation (less efficient)
96 let default_box = arc_square.clone().into_box();
97 println!(" Default into_box: {}", default_box.apply(5));
98
99 println!();
100
101 // ============================================================================
102 // Thread safety demonstration for ArcTransformer
103 // ============================================================================
104
105 println!("4. Thread safety with ArcTransformer:");
106
107 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
108
109 // Clone for thread safety
110 let arc_clone = arc_shared.clone();
111
112 // Use in different thread context (simulated)
113 let handle = std::thread::spawn(move || {
114 let boxed = arc_clone.into_box();
115 boxed.apply(50)
116 });
117
118 let result = handle.join().expect("thread should not panic");
119 println!(" Thread-safe ArcTransformer result: {}", result);
120
121 // Original still usable
122 println!(" Original ArcTransformer still works: {}", arc_shared.apply(50));
123
124 println!();
125
126 // ============================================================================
127 // String transformation example
128 // ============================================================================
129
130 println!("5. String transformation with specialized methods:");
131
132 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
133
134 // Test with string input
135 let test_string = "hello world".to_string();
136
137 // Using specialized methods
138 let boxed_upper = arc_uppercase.clone().into_box();
139 let result = boxed_upper.apply(test_string.clone());
140 println!(" String transformation: '{}' -> '{}'", test_string, result);
141
142 // Using to_xxx methods (borrowing)
143 let fn_upper = arc_uppercase.to_fn();
144 let result2 = fn_upper(test_string.clone());
145 println!(
146 " String transformation (borrowed): '{}' -> '{}'",
147 test_string, result2
148 );
149
150 // Original still usable
151 println!(
152 " Original ArcTransformer still works: '{}'",
153 arc_uppercase.apply(test_string)
154 );
155
156 println!("\n=== Demo completed successfully! ===");
157}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!(" ArcTransformer::to_box(): {}", boxed_once_borrowed.apply(21));
44
45 // Test to_fn - borrows self
46 let fn_once_borrowed = arc_double.to_fn();
47 println!(" ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
48
49 // Original transformer still usable after to_xxx methods
50 println!(" Original ArcTransformer still works: {}", arc_double.apply(21));
51
52 println!();
53
54 // ============================================================================
55 // RcTransformer TransformerOnce specialized methods
56 // ============================================================================
57
58 println!("2. RcTransformer TransformerOnce specialized methods:");
59
60 let rc_triple = RcTransformer::new(|x: i32| x * 3);
61
62 // Test into_box - consumes self
63 let boxed_once = rc_triple.clone().into_box();
64 println!(" RcTransformer::into_box(): {}", boxed_once.apply(14));
65
66 // Test into_fn - consumes self
67 let fn_once = rc_triple.clone().into_fn();
68 println!(" RcTransformer::into_fn(): {}", fn_once(14));
69
70 // Test to_box - borrows self
71 let boxed_once_borrowed = rc_triple.to_box();
72 println!(" RcTransformer::to_box(): {}", boxed_once_borrowed.apply(14));
73
74 // Test to_fn - borrows self
75 let fn_once_borrowed = rc_triple.to_fn();
76 println!(" RcTransformer::to_fn(): {}", fn_once_borrowed(14));
77
78 // Original transformer still usable after to_xxx methods
79 println!(" Original RcTransformer still works: {}", rc_triple.apply(14));
80
81 println!();
82
83 // ============================================================================
84 // Comparison with default implementations
85 // ============================================================================
86
87 println!("3. Performance comparison (specialized vs default):");
88
89 let arc_square = ArcTransformer::new(|x: i32| x * x);
90
91 // Using specialized method (more efficient)
92 let specialized_box = arc_square.clone().into_box();
93 println!(" Specialized into_box: {}", specialized_box.apply(5));
94
95 // Using default implementation (less efficient)
96 let default_box = arc_square.clone().into_box();
97 println!(" Default into_box: {}", default_box.apply(5));
98
99 println!();
100
101 // ============================================================================
102 // Thread safety demonstration for ArcTransformer
103 // ============================================================================
104
105 println!("4. Thread safety with ArcTransformer:");
106
107 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
108
109 // Clone for thread safety
110 let arc_clone = arc_shared.clone();
111
112 // Use in different thread context (simulated)
113 let handle = std::thread::spawn(move || {
114 let boxed = arc_clone.into_box();
115 boxed.apply(50)
116 });
117
118 let result = handle.join().expect("thread should not panic");
119 println!(" Thread-safe ArcTransformer result: {}", result);
120
121 // Original still usable
122 println!(" Original ArcTransformer still works: {}", arc_shared.apply(50));
123
124 println!();
125
126 // ============================================================================
127 // String transformation example
128 // ============================================================================
129
130 println!("5. String transformation with specialized methods:");
131
132 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
133
134 // Test with string input
135 let test_string = "hello world".to_string();
136
137 // Using specialized methods
138 let boxed_upper = arc_uppercase.clone().into_box();
139 let result = boxed_upper.apply(test_string.clone());
140 println!(" String transformation: '{}' -> '{}'", test_string, result);
141
142 // Using to_xxx methods (borrowing)
143 let fn_upper = arc_uppercase.to_fn();
144 let result2 = fn_upper(test_string.clone());
145 println!(
146 " String transformation (borrowed): '{}' -> '{}'",
147 test_string, result2
148 );
149
150 // Original still usable
151 println!(
152 " Original ArcTransformer still works: '{}'",
153 arc_uppercase.apply(test_string)
154 );
155
156 println!("\n=== Demo completed successfully! ===");
157}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>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".