pub trait TransformerOnce<T, R> {
// Required method
fn apply_once(self, input: T) -> R;
// Provided methods
fn into_box_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_fn_once(self) -> impl FnOnce(T) -> R
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn to_box_once(&self) -> BoxTransformerOnce<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_fn_once(&self) -> impl FnOnce(T) -> R
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
}Expand description
TransformerOnce trait - consuming transformation that takes ownership
Defines the behavior of a consuming transformer: converting a value of
type T to a value of type R by taking ownership of both self and the
input. This trait is analogous to FnOnce(T) -> R.
§Type Parameters
T- The type of the input value (consumed)R- The type of the output value
§Author
Hu Haixing
Required Methods§
Sourcefn apply_once(self, input: T) -> R
fn apply_once(self, input: T) -> R
Provided Methods§
Sourcefn into_box_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_box_once(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts to BoxTransformerOnce
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns BoxTransformerOnce<T, R>
§Examples
use prism3_function::TransformerOnce;
let double = |x: i32| x * 2;
let boxed = double.into_box_once();
assert_eq!(boxed.apply_once(21), 42);Examples found in repository?
19fn test_transformer_once() {
20 // Test function pointer
21 fn parse(s: String) -> i32 {
22 s.parse().unwrap_or(0)
23 }
24 assert_eq!(parse.apply_once("42".to_string()), 42);
25 println!("✓ Function pointer test passed: parse.apply_once(\"42\") = 42");
26
27 // Test closure that consumes ownership
28 let owned_value = String::from("hello");
29 let consume = |s: String| format!("{} world", s);
30 assert_eq!(consume.apply_once(owned_value), "hello world");
31 println!("✓ Closure that consumes ownership test passed");
32
33 // Test conversion to BoxTransformerOnce
34 let transform = |s: String| s.to_uppercase();
35 let boxed = transform.into_box_once();
36 assert_eq!(boxed.apply_once("hello".to_string()), "HELLO");
37 println!("✓ into_box_once() test passed");
38
39 // Test into_fn
40 let transform2 = |s: String| s.len();
41 let func = transform2.into_fn_once();
42 assert_eq!(func("hello".to_string()), 5);
43 println!("✓ into_fn_once() test passed");
44}More examples
22fn main() {
23 println!("=== TransformerOnce Demo ===\n");
24
25 // BoxTransformer TransformerOnce demonstration
26 println!("1. BoxTransformer TransformerOnce demonstration:");
27 let double = BoxTransformer::new(|x: i32| x * 2);
28 let result = double.apply_once(21);
29 println!(" double.apply_once(21) = {}", result);
30
31 // Convert to BoxTransformerOnce
32 let double = BoxTransformer::new(|x: i32| x * 2);
33 let boxed = double.into_box_once();
34 let result = boxed.apply_once(21);
35 println!(" double.into_box_once().apply_once(21) = {}", result);
36
37 // Convert to function
38 let double = BoxTransformer::new(|x: i32| x * 2);
39 let func = double.into_fn_once();
40 let result = func(21);
41 println!(" double.into_fn_once()(21) = {}", result);
42
43 println!();
44
45 // RcTransformer TransformerOnce demonstration
46 println!("2. RcTransformer TransformerOnce demonstration:");
47 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
48 let result = uppercase.apply_once("hello".to_string());
49 println!(" uppercase.apply_once(\"hello\") = {}", result);
50
51 // Use after cloning
52 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
53 let uppercase_clone = uppercase.clone();
54 let result1 = uppercase.apply_once("world".to_string());
55 let result2 = uppercase_clone.apply_once("rust".to_string());
56 println!(" uppercase.apply_once(\"world\") = {}", result1);
57 println!(" uppercase_clone.apply_once(\"rust\") = {}", result2);
58
59 println!();
60
61 // ArcTransformer TransformerOnce demonstration
62 println!("3. ArcTransformer TransformerOnce demonstration:");
63 let parse_and_double = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0) * 2);
64 let result = parse_and_double.apply_once("21".to_string());
65 println!(" parse_and_double.apply_once(\"21\") = {}", result);
66
67 // Thread safety demonstration
68 println!("4. ArcTransformer thread safety demonstration:");
69 let double = ArcTransformer::new(|x: i32| x * 2);
70 let double_arc = Arc::new(double);
71 let _double_clone = Arc::clone(&double_arc);
72
73 let handle = thread::spawn(move || {
74 // Create a new transformer in the thread to demonstrate thread safety
75 let new_double = ArcTransformer::new(|x: i32| x * 2);
76 new_double.apply_once(21)
77 });
78
79 let result = handle.join().unwrap();
80 println!(
81 " Executed in thread: new_double.apply_once(21) = {}",
82 result
83 );
84
85 println!("\n=== Demo completed ===");
86}20fn main() {
21 println!("=== TransformerOnce Specialized Methods Demo ===\n");
22
23 // ============================================================================
24 // ArcTransformer TransformerOnce specialized methods
25 // ============================================================================
26
27 println!("1. ArcTransformer TransformerOnce specialized methods:");
28
29 let arc_double = ArcTransformer::new(|x: i32| x * 2);
30
31 // Test into_box_once - consumes self
32 let boxed_once = arc_double.clone().into_box_once();
33 println!(
34 " ArcTransformer::into_box_once(): {}",
35 boxed_once.apply_once(21)
36 );
37
38 // Test into_fn_once - consumes self
39 let fn_once = arc_double.clone().into_fn_once();
40 println!(" ArcTransformer::into_fn_once(): {}", fn_once(21));
41
42 // Test to_box_once - borrows self
43 let boxed_once_borrowed = arc_double.to_box_once();
44 println!(
45 " ArcTransformer::to_box_once(): {}",
46 boxed_once_borrowed.apply_once(21)
47 );
48
49 // Test to_fn_once - borrows self
50 let fn_once_borrowed = arc_double.to_fn_once();
51 println!(" ArcTransformer::to_fn_once(): {}", fn_once_borrowed(21));
52
53 // Original transformer still usable after to_xxx methods
54 println!(
55 " Original ArcTransformer still works: {}",
56 arc_double.apply(21)
57 );
58
59 println!();
60
61 // ============================================================================
62 // RcTransformer TransformerOnce specialized methods
63 // ============================================================================
64
65 println!("2. RcTransformer TransformerOnce specialized methods:");
66
67 let rc_triple = RcTransformer::new(|x: i32| x * 3);
68
69 // Test into_box_once - consumes self
70 let boxed_once = rc_triple.clone().into_box_once();
71 println!(
72 " RcTransformer::into_box_once(): {}",
73 boxed_once.apply_once(14)
74 );
75
76 // Test into_fn_once - consumes self
77 let fn_once = rc_triple.clone().into_fn_once();
78 println!(" RcTransformer::into_fn_once(): {}", fn_once(14));
79
80 // Test to_box_once - borrows self
81 let boxed_once_borrowed = rc_triple.to_box_once();
82 println!(
83 " RcTransformer::to_box_once(): {}",
84 boxed_once_borrowed.apply_once(14)
85 );
86
87 // Test to_fn_once - borrows self
88 let fn_once_borrowed = rc_triple.to_fn_once();
89 println!(" RcTransformer::to_fn_once(): {}", fn_once_borrowed(14));
90
91 // Original transformer still usable after to_xxx methods
92 println!(
93 " Original RcTransformer still works: {}",
94 rc_triple.apply(14)
95 );
96
97 println!();
98
99 // ============================================================================
100 // Comparison with default implementations
101 // ============================================================================
102
103 println!("3. Performance comparison (specialized vs default):");
104
105 let arc_square = ArcTransformer::new(|x: i32| x * x);
106
107 // Using specialized method (more efficient)
108 let specialized_box = arc_square.clone().into_box_once();
109 println!(
110 " Specialized into_box_once: {}",
111 specialized_box.apply_once(5)
112 );
113
114 // Using default implementation (less efficient)
115 let default_box = arc_square.clone().into_box_once();
116 println!(" Default into_box_once: {}", default_box.apply_once(5));
117
118 println!();
119
120 // ============================================================================
121 // Thread safety demonstration for ArcTransformer
122 // ============================================================================
123
124 println!("4. Thread safety with ArcTransformer:");
125
126 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
127
128 // Clone for thread safety
129 let arc_clone = arc_shared.clone();
130
131 // Use in different thread context (simulated)
132 let handle = std::thread::spawn(move || {
133 let boxed = arc_clone.into_box_once();
134 boxed.apply_once(50)
135 });
136
137 let result = handle.join().unwrap();
138 println!(" Thread-safe ArcTransformer result: {}", result);
139
140 // Original still usable
141 println!(
142 " Original ArcTransformer still works: {}",
143 arc_shared.apply(50)
144 );
145
146 println!();
147
148 // ============================================================================
149 // String transformation example
150 // ============================================================================
151
152 println!("5. String transformation with specialized methods:");
153
154 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
155
156 // Test with string input
157 let test_string = "hello world".to_string();
158
159 // Using specialized methods
160 let boxed_upper = arc_uppercase.clone().into_box_once();
161 let result = boxed_upper.apply_once(test_string.clone());
162 println!(
163 " String transformation: '{}' -> '{}'",
164 test_string, result
165 );
166
167 // Using to_xxx methods (borrowing)
168 let fn_upper = arc_uppercase.to_fn_once();
169 let result2 = fn_upper(test_string.clone());
170 println!(
171 " String transformation (borrowed): '{}' -> '{}'",
172 test_string, result2
173 );
174
175 // Original still usable
176 println!(
177 " Original ArcTransformer still works: '{}'",
178 arc_uppercase.apply(test_string)
179 );
180
181 println!("\n=== Demo completed successfully! ===");
182}Sourcefn into_fn_once(self) -> impl FnOnce(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn_once(self) -> impl FnOnce(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts transformer to a closure
⚠️ Consumes self: The original transformer becomes unavailable
after calling this method.
§Returns
Returns a closure that implements FnOnce(T) -> R
§Examples
use prism3_function::TransformerOnce;
let double = |x: i32| x * 2;
let func = double.into_fn_once();
assert_eq!(func(21), 42);Examples found in repository?
19fn test_transformer_once() {
20 // Test function pointer
21 fn parse(s: String) -> i32 {
22 s.parse().unwrap_or(0)
23 }
24 assert_eq!(parse.apply_once("42".to_string()), 42);
25 println!("✓ Function pointer test passed: parse.apply_once(\"42\") = 42");
26
27 // Test closure that consumes ownership
28 let owned_value = String::from("hello");
29 let consume = |s: String| format!("{} world", s);
30 assert_eq!(consume.apply_once(owned_value), "hello world");
31 println!("✓ Closure that consumes ownership test passed");
32
33 // Test conversion to BoxTransformerOnce
34 let transform = |s: String| s.to_uppercase();
35 let boxed = transform.into_box_once();
36 assert_eq!(boxed.apply_once("hello".to_string()), "HELLO");
37 println!("✓ into_box_once() test passed");
38
39 // Test into_fn
40 let transform2 = |s: String| s.len();
41 let func = transform2.into_fn_once();
42 assert_eq!(func("hello".to_string()), 5);
43 println!("✓ into_fn_once() test passed");
44}More examples
22fn main() {
23 println!("=== TransformerOnce Demo ===\n");
24
25 // BoxTransformer TransformerOnce demonstration
26 println!("1. BoxTransformer TransformerOnce demonstration:");
27 let double = BoxTransformer::new(|x: i32| x * 2);
28 let result = double.apply_once(21);
29 println!(" double.apply_once(21) = {}", result);
30
31 // Convert to BoxTransformerOnce
32 let double = BoxTransformer::new(|x: i32| x * 2);
33 let boxed = double.into_box_once();
34 let result = boxed.apply_once(21);
35 println!(" double.into_box_once().apply_once(21) = {}", result);
36
37 // Convert to function
38 let double = BoxTransformer::new(|x: i32| x * 2);
39 let func = double.into_fn_once();
40 let result = func(21);
41 println!(" double.into_fn_once()(21) = {}", result);
42
43 println!();
44
45 // RcTransformer TransformerOnce demonstration
46 println!("2. RcTransformer TransformerOnce demonstration:");
47 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
48 let result = uppercase.apply_once("hello".to_string());
49 println!(" uppercase.apply_once(\"hello\") = {}", result);
50
51 // Use after cloning
52 let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
53 let uppercase_clone = uppercase.clone();
54 let result1 = uppercase.apply_once("world".to_string());
55 let result2 = uppercase_clone.apply_once("rust".to_string());
56 println!(" uppercase.apply_once(\"world\") = {}", result1);
57 println!(" uppercase_clone.apply_once(\"rust\") = {}", result2);
58
59 println!();
60
61 // ArcTransformer TransformerOnce demonstration
62 println!("3. ArcTransformer TransformerOnce demonstration:");
63 let parse_and_double = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0) * 2);
64 let result = parse_and_double.apply_once("21".to_string());
65 println!(" parse_and_double.apply_once(\"21\") = {}", result);
66
67 // Thread safety demonstration
68 println!("4. ArcTransformer thread safety demonstration:");
69 let double = ArcTransformer::new(|x: i32| x * 2);
70 let double_arc = Arc::new(double);
71 let _double_clone = Arc::clone(&double_arc);
72
73 let handle = thread::spawn(move || {
74 // Create a new transformer in the thread to demonstrate thread safety
75 let new_double = ArcTransformer::new(|x: i32| x * 2);
76 new_double.apply_once(21)
77 });
78
79 let result = handle.join().unwrap();
80 println!(
81 " Executed in thread: new_double.apply_once(21) = {}",
82 result
83 );
84
85 println!("\n=== Demo completed ===");
86}20fn main() {
21 println!("=== TransformerOnce Specialized Methods Demo ===\n");
22
23 // ============================================================================
24 // ArcTransformer TransformerOnce specialized methods
25 // ============================================================================
26
27 println!("1. ArcTransformer TransformerOnce specialized methods:");
28
29 let arc_double = ArcTransformer::new(|x: i32| x * 2);
30
31 // Test into_box_once - consumes self
32 let boxed_once = arc_double.clone().into_box_once();
33 println!(
34 " ArcTransformer::into_box_once(): {}",
35 boxed_once.apply_once(21)
36 );
37
38 // Test into_fn_once - consumes self
39 let fn_once = arc_double.clone().into_fn_once();
40 println!(" ArcTransformer::into_fn_once(): {}", fn_once(21));
41
42 // Test to_box_once - borrows self
43 let boxed_once_borrowed = arc_double.to_box_once();
44 println!(
45 " ArcTransformer::to_box_once(): {}",
46 boxed_once_borrowed.apply_once(21)
47 );
48
49 // Test to_fn_once - borrows self
50 let fn_once_borrowed = arc_double.to_fn_once();
51 println!(" ArcTransformer::to_fn_once(): {}", fn_once_borrowed(21));
52
53 // Original transformer still usable after to_xxx methods
54 println!(
55 " Original ArcTransformer still works: {}",
56 arc_double.apply(21)
57 );
58
59 println!();
60
61 // ============================================================================
62 // RcTransformer TransformerOnce specialized methods
63 // ============================================================================
64
65 println!("2. RcTransformer TransformerOnce specialized methods:");
66
67 let rc_triple = RcTransformer::new(|x: i32| x * 3);
68
69 // Test into_box_once - consumes self
70 let boxed_once = rc_triple.clone().into_box_once();
71 println!(
72 " RcTransformer::into_box_once(): {}",
73 boxed_once.apply_once(14)
74 );
75
76 // Test into_fn_once - consumes self
77 let fn_once = rc_triple.clone().into_fn_once();
78 println!(" RcTransformer::into_fn_once(): {}", fn_once(14));
79
80 // Test to_box_once - borrows self
81 let boxed_once_borrowed = rc_triple.to_box_once();
82 println!(
83 " RcTransformer::to_box_once(): {}",
84 boxed_once_borrowed.apply_once(14)
85 );
86
87 // Test to_fn_once - borrows self
88 let fn_once_borrowed = rc_triple.to_fn_once();
89 println!(" RcTransformer::to_fn_once(): {}", fn_once_borrowed(14));
90
91 // Original transformer still usable after to_xxx methods
92 println!(
93 " Original RcTransformer still works: {}",
94 rc_triple.apply(14)
95 );
96
97 println!();
98
99 // ============================================================================
100 // Comparison with default implementations
101 // ============================================================================
102
103 println!("3. Performance comparison (specialized vs default):");
104
105 let arc_square = ArcTransformer::new(|x: i32| x * x);
106
107 // Using specialized method (more efficient)
108 let specialized_box = arc_square.clone().into_box_once();
109 println!(
110 " Specialized into_box_once: {}",
111 specialized_box.apply_once(5)
112 );
113
114 // Using default implementation (less efficient)
115 let default_box = arc_square.clone().into_box_once();
116 println!(" Default into_box_once: {}", default_box.apply_once(5));
117
118 println!();
119
120 // ============================================================================
121 // Thread safety demonstration for ArcTransformer
122 // ============================================================================
123
124 println!("4. Thread safety with ArcTransformer:");
125
126 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
127
128 // Clone for thread safety
129 let arc_clone = arc_shared.clone();
130
131 // Use in different thread context (simulated)
132 let handle = std::thread::spawn(move || {
133 let boxed = arc_clone.into_box_once();
134 boxed.apply_once(50)
135 });
136
137 let result = handle.join().unwrap();
138 println!(" Thread-safe ArcTransformer result: {}", result);
139
140 // Original still usable
141 println!(
142 " Original ArcTransformer still works: {}",
143 arc_shared.apply(50)
144 );
145
146 println!();
147
148 // ============================================================================
149 // String transformation example
150 // ============================================================================
151
152 println!("5. String transformation with specialized methods:");
153
154 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
155
156 // Test with string input
157 let test_string = "hello world".to_string();
158
159 // Using specialized methods
160 let boxed_upper = arc_uppercase.clone().into_box_once();
161 let result = boxed_upper.apply_once(test_string.clone());
162 println!(
163 " String transformation: '{}' -> '{}'",
164 test_string, result
165 );
166
167 // Using to_xxx methods (borrowing)
168 let fn_upper = arc_uppercase.to_fn_once();
169 let result2 = fn_upper(test_string.clone());
170 println!(
171 " String transformation (borrowed): '{}' -> '{}'",
172 test_string, result2
173 );
174
175 // Original still usable
176 println!(
177 " Original ArcTransformer still works: '{}'",
178 arc_uppercase.apply(test_string)
179 );
180
181 println!("\n=== Demo completed successfully! ===");
182}Sourcefn to_box_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_box_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to BoxTransformerOnce without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new BoxTransformerOnce that
captures a clone. Types implementing Clone can override this method
to provide more efficient conversions.
§Returns
Returns BoxTransformerOnce<T, R>
§Examples
use prism3_function::TransformerOnce;
let double = |x: i32| x * 2;
let boxed = double.to_box_once();
assert_eq!(boxed.apply_once(21), 42);Examples found in repository?
20fn main() {
21 println!("=== TransformerOnce Specialized Methods Demo ===\n");
22
23 // ============================================================================
24 // ArcTransformer TransformerOnce specialized methods
25 // ============================================================================
26
27 println!("1. ArcTransformer TransformerOnce specialized methods:");
28
29 let arc_double = ArcTransformer::new(|x: i32| x * 2);
30
31 // Test into_box_once - consumes self
32 let boxed_once = arc_double.clone().into_box_once();
33 println!(
34 " ArcTransformer::into_box_once(): {}",
35 boxed_once.apply_once(21)
36 );
37
38 // Test into_fn_once - consumes self
39 let fn_once = arc_double.clone().into_fn_once();
40 println!(" ArcTransformer::into_fn_once(): {}", fn_once(21));
41
42 // Test to_box_once - borrows self
43 let boxed_once_borrowed = arc_double.to_box_once();
44 println!(
45 " ArcTransformer::to_box_once(): {}",
46 boxed_once_borrowed.apply_once(21)
47 );
48
49 // Test to_fn_once - borrows self
50 let fn_once_borrowed = arc_double.to_fn_once();
51 println!(" ArcTransformer::to_fn_once(): {}", fn_once_borrowed(21));
52
53 // Original transformer still usable after to_xxx methods
54 println!(
55 " Original ArcTransformer still works: {}",
56 arc_double.apply(21)
57 );
58
59 println!();
60
61 // ============================================================================
62 // RcTransformer TransformerOnce specialized methods
63 // ============================================================================
64
65 println!("2. RcTransformer TransformerOnce specialized methods:");
66
67 let rc_triple = RcTransformer::new(|x: i32| x * 3);
68
69 // Test into_box_once - consumes self
70 let boxed_once = rc_triple.clone().into_box_once();
71 println!(
72 " RcTransformer::into_box_once(): {}",
73 boxed_once.apply_once(14)
74 );
75
76 // Test into_fn_once - consumes self
77 let fn_once = rc_triple.clone().into_fn_once();
78 println!(" RcTransformer::into_fn_once(): {}", fn_once(14));
79
80 // Test to_box_once - borrows self
81 let boxed_once_borrowed = rc_triple.to_box_once();
82 println!(
83 " RcTransformer::to_box_once(): {}",
84 boxed_once_borrowed.apply_once(14)
85 );
86
87 // Test to_fn_once - borrows self
88 let fn_once_borrowed = rc_triple.to_fn_once();
89 println!(" RcTransformer::to_fn_once(): {}", fn_once_borrowed(14));
90
91 // Original transformer still usable after to_xxx methods
92 println!(
93 " Original RcTransformer still works: {}",
94 rc_triple.apply(14)
95 );
96
97 println!();
98
99 // ============================================================================
100 // Comparison with default implementations
101 // ============================================================================
102
103 println!("3. Performance comparison (specialized vs default):");
104
105 let arc_square = ArcTransformer::new(|x: i32| x * x);
106
107 // Using specialized method (more efficient)
108 let specialized_box = arc_square.clone().into_box_once();
109 println!(
110 " Specialized into_box_once: {}",
111 specialized_box.apply_once(5)
112 );
113
114 // Using default implementation (less efficient)
115 let default_box = arc_square.clone().into_box_once();
116 println!(" Default into_box_once: {}", default_box.apply_once(5));
117
118 println!();
119
120 // ============================================================================
121 // Thread safety demonstration for ArcTransformer
122 // ============================================================================
123
124 println!("4. Thread safety with ArcTransformer:");
125
126 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
127
128 // Clone for thread safety
129 let arc_clone = arc_shared.clone();
130
131 // Use in different thread context (simulated)
132 let handle = std::thread::spawn(move || {
133 let boxed = arc_clone.into_box_once();
134 boxed.apply_once(50)
135 });
136
137 let result = handle.join().unwrap();
138 println!(" Thread-safe ArcTransformer result: {}", result);
139
140 // Original still usable
141 println!(
142 " Original ArcTransformer still works: {}",
143 arc_shared.apply(50)
144 );
145
146 println!();
147
148 // ============================================================================
149 // String transformation example
150 // ============================================================================
151
152 println!("5. String transformation with specialized methods:");
153
154 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
155
156 // Test with string input
157 let test_string = "hello world".to_string();
158
159 // Using specialized methods
160 let boxed_upper = arc_uppercase.clone().into_box_once();
161 let result = boxed_upper.apply_once(test_string.clone());
162 println!(
163 " String transformation: '{}' -> '{}'",
164 test_string, result
165 );
166
167 // Using to_xxx methods (borrowing)
168 let fn_upper = arc_uppercase.to_fn_once();
169 let result2 = fn_upper(test_string.clone());
170 println!(
171 " String transformation (borrowed): '{}' -> '{}'",
172 test_string, result2
173 );
174
175 // Original still usable
176 println!(
177 " Original ArcTransformer still works: '{}'",
178 arc_uppercase.apply(test_string)
179 );
180
181 println!("\n=== Demo completed successfully! ===");
182}Sourcefn to_fn_once(&self) -> impl FnOnce(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_fn_once(&self) -> impl FnOnce(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: '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 FnOnce(T) -> R
§Examples
use prism3_function::TransformerOnce;
let double = |x: i32| x * 2;
let func = double.to_fn_once();
assert_eq!(func(21), 42);Examples found in repository?
20fn main() {
21 println!("=== TransformerOnce Specialized Methods Demo ===\n");
22
23 // ============================================================================
24 // ArcTransformer TransformerOnce specialized methods
25 // ============================================================================
26
27 println!("1. ArcTransformer TransformerOnce specialized methods:");
28
29 let arc_double = ArcTransformer::new(|x: i32| x * 2);
30
31 // Test into_box_once - consumes self
32 let boxed_once = arc_double.clone().into_box_once();
33 println!(
34 " ArcTransformer::into_box_once(): {}",
35 boxed_once.apply_once(21)
36 );
37
38 // Test into_fn_once - consumes self
39 let fn_once = arc_double.clone().into_fn_once();
40 println!(" ArcTransformer::into_fn_once(): {}", fn_once(21));
41
42 // Test to_box_once - borrows self
43 let boxed_once_borrowed = arc_double.to_box_once();
44 println!(
45 " ArcTransformer::to_box_once(): {}",
46 boxed_once_borrowed.apply_once(21)
47 );
48
49 // Test to_fn_once - borrows self
50 let fn_once_borrowed = arc_double.to_fn_once();
51 println!(" ArcTransformer::to_fn_once(): {}", fn_once_borrowed(21));
52
53 // Original transformer still usable after to_xxx methods
54 println!(
55 " Original ArcTransformer still works: {}",
56 arc_double.apply(21)
57 );
58
59 println!();
60
61 // ============================================================================
62 // RcTransformer TransformerOnce specialized methods
63 // ============================================================================
64
65 println!("2. RcTransformer TransformerOnce specialized methods:");
66
67 let rc_triple = RcTransformer::new(|x: i32| x * 3);
68
69 // Test into_box_once - consumes self
70 let boxed_once = rc_triple.clone().into_box_once();
71 println!(
72 " RcTransformer::into_box_once(): {}",
73 boxed_once.apply_once(14)
74 );
75
76 // Test into_fn_once - consumes self
77 let fn_once = rc_triple.clone().into_fn_once();
78 println!(" RcTransformer::into_fn_once(): {}", fn_once(14));
79
80 // Test to_box_once - borrows self
81 let boxed_once_borrowed = rc_triple.to_box_once();
82 println!(
83 " RcTransformer::to_box_once(): {}",
84 boxed_once_borrowed.apply_once(14)
85 );
86
87 // Test to_fn_once - borrows self
88 let fn_once_borrowed = rc_triple.to_fn_once();
89 println!(" RcTransformer::to_fn_once(): {}", fn_once_borrowed(14));
90
91 // Original transformer still usable after to_xxx methods
92 println!(
93 " Original RcTransformer still works: {}",
94 rc_triple.apply(14)
95 );
96
97 println!();
98
99 // ============================================================================
100 // Comparison with default implementations
101 // ============================================================================
102
103 println!("3. Performance comparison (specialized vs default):");
104
105 let arc_square = ArcTransformer::new(|x: i32| x * x);
106
107 // Using specialized method (more efficient)
108 let specialized_box = arc_square.clone().into_box_once();
109 println!(
110 " Specialized into_box_once: {}",
111 specialized_box.apply_once(5)
112 );
113
114 // Using default implementation (less efficient)
115 let default_box = arc_square.clone().into_box_once();
116 println!(" Default into_box_once: {}", default_box.apply_once(5));
117
118 println!();
119
120 // ============================================================================
121 // Thread safety demonstration for ArcTransformer
122 // ============================================================================
123
124 println!("4. Thread safety with ArcTransformer:");
125
126 let arc_shared = ArcTransformer::new(|x: i32| x + 100);
127
128 // Clone for thread safety
129 let arc_clone = arc_shared.clone();
130
131 // Use in different thread context (simulated)
132 let handle = std::thread::spawn(move || {
133 let boxed = arc_clone.into_box_once();
134 boxed.apply_once(50)
135 });
136
137 let result = handle.join().unwrap();
138 println!(" Thread-safe ArcTransformer result: {}", result);
139
140 // Original still usable
141 println!(
142 " Original ArcTransformer still works: {}",
143 arc_shared.apply(50)
144 );
145
146 println!();
147
148 // ============================================================================
149 // String transformation example
150 // ============================================================================
151
152 println!("5. String transformation with specialized methods:");
153
154 let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
155
156 // Test with string input
157 let test_string = "hello world".to_string();
158
159 // Using specialized methods
160 let boxed_upper = arc_uppercase.clone().into_box_once();
161 let result = boxed_upper.apply_once(test_string.clone());
162 println!(
163 " String transformation: '{}' -> '{}'",
164 test_string, result
165 );
166
167 // Using to_xxx methods (borrowing)
168 let fn_upper = arc_uppercase.to_fn_once();
169 let result2 = fn_upper(test_string.clone());
170 println!(
171 " String transformation (borrowed): '{}' -> '{}'",
172 test_string, result2
173 );
174
175 // Original still usable
176 println!(
177 " Original ArcTransformer still works: '{}'",
178 arc_uppercase.apply(test_string)
179 );
180
181 println!("\n=== Demo completed successfully! ===");
182}Implementors§
impl<F, T, R> TransformerOnce<T, R> for Fwhere
F: FnOnce(T) -> R,
T: 'static,
R: 'static,
Implement TransformerOnce<T, R> for any type that implements FnOnce(T) -> R
This allows once-callable closures and function pointers to be used directly with our TransformerOnce trait without wrapping.
§Examples
use prism3_function::TransformerOnce;
fn parse(s: String) -> i32 {
s.parse().unwrap_or(0)
}
assert_eq!(parse.apply_once("42".to_string()), 42);
let owned_value = String::from("hello");
let consume = |s: String| {
format!("{} world", s)
};
assert_eq!(consume.apply_once(owned_value), "hello world");§Author
Hu Haixing