pub struct ArcBiTransformer<T, U, R> { /* private fields */ }Expand description
ArcBiTransformer - thread-safe bi-transformer wrapper
A thread-safe, clonable bi-transformer wrapper suitable for multi-threaded scenarios. Can be called multiple times and shared across threads.
§Features
- Based on:
Arc<dyn Fn(T, U) -> R + Send + Sync> - Ownership: Shared ownership via reference counting
- Reusability: Can be called multiple times (each call consumes its inputs)
- Thread Safety: Thread-safe (
Send + Syncrequired) - Clonable: Cheap cloning via
Arc::clone
§Author
Hu Haixing
Implementations§
Source§impl<T, U, R> ArcBiTransformer<T, U, R>
impl<T, U, R> ArcBiTransformer<T, U, R>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new ArcBiTransformer
§Parameters
f- The closure or function to wrap (must be Send + Sync)
§Examples
use prism3_function::{ArcBiTransformer, BiTransformer};
let add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
assert_eq!(add.apply(20, 22), 42);Examples found in repository?
12fn main() {
13 println!("=== BiTransformer Demo ===\n");
14
15 // 1. BoxBiTransformer - Single ownership
16 println!("1. BoxBiTransformer - Single ownership");
17 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
18 println!(" add.apply(20, 22) = {}", add.apply(20, 22));
19
20 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
21 println!(" multiply.apply(6, 7) = {}", multiply.apply(6, 7));
22
23 // Constant bi-transformer
24 let constant = BoxBiTransformer::constant("hello");
25 println!(" constant.apply(1, 2) = {}", constant.apply(1, 2));
26 println!();
27
28 // 2. ArcBiTransformer - Thread-safe, cloneable
29 println!("2. ArcBiTransformer - Thread-safe, cloneable");
30 let arc_add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
31 let arc_add_clone = arc_add.clone();
32
33 println!(" arc_add.apply(10, 15) = {}", arc_add.apply(10, 15));
34 println!(
35 " arc_add_clone.apply(5, 8) = {}",
36 arc_add_clone.apply(5, 8)
37 );
38 println!();
39
40 // 3. RcBiTransformer - Single-threaded, cloneable
41 println!("3. RcBiTransformer - Single-threaded, cloneable");
42 let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
43 let rc_multiply_clone = rc_multiply.clone();
44
45 println!(" rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
46 println!(
47 " rc_multiply_clone.apply(5, 6) = {}",
48 rc_multiply_clone.apply(5, 6)
49 );
50 println!();
51
52 // 4. Conditional BiTransformer
53 println!("4. Conditional BiTransformer");
54 let add_if_positive = BoxBiTransformer::new(|x: i32, y: i32| x + y);
55 let multiply_otherwise = BoxBiTransformer::new(|x: i32, y: i32| x * y);
56 let conditional = add_if_positive
57 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
58 .or_else(multiply_otherwise);
59
60 println!(
61 " conditional.apply(5, 3) = {} (both positive, add)",
62 conditional.apply(5, 3)
63 );
64 println!(
65 " conditional.apply(-5, 3) = {} (not both positive, multiply)",
66 conditional.apply(-5, 3)
67 );
68 println!();
69
70 // 5. Working with different types
71 println!("5. Working with different types");
72 let format =
73 BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
74 println!(
75 " format.apply(\"Alice\", 30) = {}",
76 format.apply("Alice".to_string(), 30)
77 );
78 println!();
79
80 // 6. Closure as BiTransformer
81 println!("6. Closure as BiTransformer");
82 let subtract = |x: i32, y: i32| x - y;
83 println!(" subtract.apply(42, 10) = {}", subtract.apply(42, 10));
84 println!();
85
86 // 7. Conversion between types
87 println!("7. Conversion between types");
88 let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
89 let rc_add = box_add.into_rc();
90 println!(" Converted BoxBiTransformer to RcBiTransformer");
91 println!(" rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
92 println!();
93
94 // 8. Safe division with Option
95 println!("8. Safe division with Option");
96 let safe_divide =
97 BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
98 println!(
99 " safe_divide.apply(42, 2) = {:?}",
100 safe_divide.apply(42, 2)
101 );
102 println!(
103 " safe_divide.apply(42, 0) = {:?}",
104 safe_divide.apply(42, 0)
105 );
106 println!();
107
108 // 9. String concatenation
109 println!("9. String concatenation");
110 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
111 println!(
112 " concat.apply(\"Hello\", \"World\") = {}",
113 concat.apply("Hello".to_string(), "World".to_string())
114 );
115 println!();
116
117 println!("=== Demo Complete ===");
118}More examples
19fn main() {
20 println!("=== BiTransformer and_then Method Demo ===\n");
21
22 // 1. BoxBiTransformer::and_then - Basic usage
23 println!("1. BoxBiTransformer::and_then - Basic usage");
24 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
25 let double = |x: i32| x * 2;
26 let composed = add.and_then(double);
27 println!(" (3 + 5) * 2 = {}", composed.apply(3, 5));
28 println!();
29
30 // 2. BoxBiTransformer::and_then - Chained calls
31 println!("2. BoxBiTransformer::and_then - Chained calls");
32 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
33 let add_ten = |x: i32| x + 10;
34 let to_string = |x: i32| format!("Result: {}", x);
35 let pipeline = multiply.and_then(add_ten).and_then(to_string);
36 println!(" (6 * 7) + 10 = {}", pipeline.apply(6, 7));
37 println!();
38
39 // 3. ArcBiTransformer::and_then - Shared ownership
40 println!("3. ArcBiTransformer::and_then - Shared ownership");
41 let add_arc = ArcBiTransformer::new(|x: i32, y: i32| x + y);
42 let triple = |x: i32| x * 3;
43 let composed_arc = add_arc.and_then(triple);
44
45 // Original bi-transformer is still available
46 println!(" Original: 20 + 22 = {}", add_arc.apply(20, 22));
47 println!(" Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
48 println!();
49
50 // 4. ArcBiTransformer::and_then - Cloneable
51 println!("4. ArcBiTransformer::and_then - Cloneable");
52 let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
53 let abs = |x: i32| x.abs();
54 let composed_abs = subtract.and_then(abs);
55 let cloned = composed_abs.clone();
56
57 println!(" Original: |10 - 15| = {}", composed_abs.apply(10, 15));
58 println!(" Cloned: |15 - 10| = {}", cloned.apply(15, 10));
59 println!();
60
61 // 5. RcBiTransformer::and_then - Single-threaded sharing
62 println!("5. RcBiTransformer::and_then - Single-threaded sharing");
63 let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
64 let square = |x: i32| x * x;
65 let composed_rc = divide.and_then(square);
66
67 println!(" Original: 20 / 4 = {}", divide.apply(20, 4));
68 println!(" Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
69 println!();
70
71 // 6. Type conversion example
72 println!("6. Type conversion example");
73 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
74 let to_uppercase = |s: String| s.to_uppercase();
75 let get_length = |s: String| s.len();
76
77 let uppercase_pipeline = concat.and_then(to_uppercase);
78 println!(
79 " \"hello\" + \"world\" -> uppercase: {}",
80 uppercase_pipeline.apply("hello".to_string(), "world".to_string())
81 );
82
83 let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
84 let length_pipeline = concat2.and_then(get_length);
85 println!(
86 " \"hello\" + \"world\" -> length: {}",
87 length_pipeline.apply("hello".to_string(), "world".to_string())
88 );
89 println!();
90
91 // 7. Real application: Calculator
92 println!("7. Real application: Calculator");
93 let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
94 let round = |x: f64| x.round();
95 let to_int = |x: f64| x as i32;
96
97 let calculator = calculate.and_then(round).and_then(to_int);
98 println!(
99 " 3.7 + 4.8 -> round -> integer: {}",
100 calculator.apply(3.7, 4.8)
101 );
102 println!();
103
104 // 8. Error handling example
105 println!("8. Error handling example");
106 let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| -> Result<i32, String> {
107 if y == 0 {
108 Err("Division by zero is not allowed".to_string())
109 } else {
110 Ok(x / y)
111 }
112 });
113
114 let format_result = |res: Result<i32, String>| match res {
115 Ok(v) => format!("Success: {}", v),
116 Err(e) => format!("Error: {}", e),
117 };
118
119 let safe_calculator = safe_divide.and_then(format_result);
120 println!(" 10 / 2 = {}", safe_calculator.apply(10, 2));
121 println!(" 10 / 0 = {}", safe_calculator.apply(10, 0));
122 println!();
123
124 // 9. Complex data structures
125 println!("9. Complex data structures");
126 #[derive(Debug)]
127 struct Point {
128 x: i32,
129 y: i32,
130 }
131
132 let create_point = BoxBiTransformer::new(|x: i32, y: i32| Point { x, y });
133 let distance_from_origin = |p: Point| ((p.x * p.x + p.y * p.y) as f64).sqrt();
134 let format_distance = |d: f64| format!("{:.2}", d);
135
136 let point_processor = create_point
137 .and_then(distance_from_origin)
138 .and_then(format_distance);
139 println!(
140 " Distance from point(3, 4) to origin: {}",
141 point_processor.apply(3, 4)
142 );
143 println!();
144
145 // 10. Combined usage with when
146 println!("10. Combined usage with when");
147 let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
148 let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
149
150 let conditional = add_when
151 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
152 .or_else(multiply_when);
153
154 let double_result = |x: i32| x * 2;
155 let final_transformer = conditional.and_then(double_result);
156
157 println!(
158 " Add positive numbers then double: (5 + 3) * 2 = {}",
159 final_transformer.apply(5, 3)
160 );
161 println!(
162 " Multiply negative numbers then double: (-5 * 3) * 2 = {}",
163 final_transformer.apply(-5, 3)
164 );
165
166 println!("\n=== Demo completed ===");
167}Sourcepub fn and_then<S, F>(&self, after: F) -> ArcBiTransformer<T, U, S>
pub fn and_then<S, F>(&self, after: F) -> ArcBiTransformer<T, U, S>
Chain composition - applies self first, then after
Creates a new bi-transformer that applies this bi-transformer first, then applies the after transformer to the result. Uses &self, so original bi-transformer remains usable.
§Type Parameters
S- The output type of the after transformerF- The type of the after transformer (must implement Transformer<R, S>)
§Parameters
after- The transformer to apply after self. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original transformer, clone it first (if it implementsClone). Must beSend + Sync, can be:- A closure:
|x: R| -> S(must beSend + Sync) - A function pointer:
fn(R) -> S - A
BoxTransformer<R, S> - An
RcTransformer<R, S> - An
ArcTransformer<R, S>(will be moved) - Any type implementing
Transformer<R, S> + Send + Sync
- A closure:
§Returns
A new ArcBiTransformer<T, U, S> representing the composition
§Examples
§Direct value passing (ownership transfer)
use prism3_function::{BiTransformer, ArcBiTransformer, ArcTransformer};
let add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
let double = ArcTransformer::new(|x: i32| x * 2);
// double is moved here
let composed = add.and_then(double);
// Original add bi-transformer still usable (uses &self)
assert_eq!(add.apply(20, 22), 42);
assert_eq!(composed.apply(3, 5), 16); // (3 + 5) * 2
// double.apply(10); // Would not compile - moved§Preserving original with clone
use prism3_function::{BiTransformer, ArcBiTransformer, ArcTransformer};
let add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
let double = ArcTransformer::new(|x: i32| x * 2);
// Clone to preserve original
let composed = add.and_then(double.clone());
assert_eq!(composed.apply(3, 5), 16); // (3 + 5) * 2
// Both originals still usable
assert_eq!(add.apply(20, 22), 42);
assert_eq!(double.apply(10), 20);Examples found in repository?
19fn main() {
20 println!("=== BiTransformer and_then Method Demo ===\n");
21
22 // 1. BoxBiTransformer::and_then - Basic usage
23 println!("1. BoxBiTransformer::and_then - Basic usage");
24 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
25 let double = |x: i32| x * 2;
26 let composed = add.and_then(double);
27 println!(" (3 + 5) * 2 = {}", composed.apply(3, 5));
28 println!();
29
30 // 2. BoxBiTransformer::and_then - Chained calls
31 println!("2. BoxBiTransformer::and_then - Chained calls");
32 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
33 let add_ten = |x: i32| x + 10;
34 let to_string = |x: i32| format!("Result: {}", x);
35 let pipeline = multiply.and_then(add_ten).and_then(to_string);
36 println!(" (6 * 7) + 10 = {}", pipeline.apply(6, 7));
37 println!();
38
39 // 3. ArcBiTransformer::and_then - Shared ownership
40 println!("3. ArcBiTransformer::and_then - Shared ownership");
41 let add_arc = ArcBiTransformer::new(|x: i32, y: i32| x + y);
42 let triple = |x: i32| x * 3;
43 let composed_arc = add_arc.and_then(triple);
44
45 // Original bi-transformer is still available
46 println!(" Original: 20 + 22 = {}", add_arc.apply(20, 22));
47 println!(" Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
48 println!();
49
50 // 4. ArcBiTransformer::and_then - Cloneable
51 println!("4. ArcBiTransformer::and_then - Cloneable");
52 let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
53 let abs = |x: i32| x.abs();
54 let composed_abs = subtract.and_then(abs);
55 let cloned = composed_abs.clone();
56
57 println!(" Original: |10 - 15| = {}", composed_abs.apply(10, 15));
58 println!(" Cloned: |15 - 10| = {}", cloned.apply(15, 10));
59 println!();
60
61 // 5. RcBiTransformer::and_then - Single-threaded sharing
62 println!("5. RcBiTransformer::and_then - Single-threaded sharing");
63 let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
64 let square = |x: i32| x * x;
65 let composed_rc = divide.and_then(square);
66
67 println!(" Original: 20 / 4 = {}", divide.apply(20, 4));
68 println!(" Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
69 println!();
70
71 // 6. Type conversion example
72 println!("6. Type conversion example");
73 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
74 let to_uppercase = |s: String| s.to_uppercase();
75 let get_length = |s: String| s.len();
76
77 let uppercase_pipeline = concat.and_then(to_uppercase);
78 println!(
79 " \"hello\" + \"world\" -> uppercase: {}",
80 uppercase_pipeline.apply("hello".to_string(), "world".to_string())
81 );
82
83 let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
84 let length_pipeline = concat2.and_then(get_length);
85 println!(
86 " \"hello\" + \"world\" -> length: {}",
87 length_pipeline.apply("hello".to_string(), "world".to_string())
88 );
89 println!();
90
91 // 7. Real application: Calculator
92 println!("7. Real application: Calculator");
93 let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
94 let round = |x: f64| x.round();
95 let to_int = |x: f64| x as i32;
96
97 let calculator = calculate.and_then(round).and_then(to_int);
98 println!(
99 " 3.7 + 4.8 -> round -> integer: {}",
100 calculator.apply(3.7, 4.8)
101 );
102 println!();
103
104 // 8. Error handling example
105 println!("8. Error handling example");
106 let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| -> Result<i32, String> {
107 if y == 0 {
108 Err("Division by zero is not allowed".to_string())
109 } else {
110 Ok(x / y)
111 }
112 });
113
114 let format_result = |res: Result<i32, String>| match res {
115 Ok(v) => format!("Success: {}", v),
116 Err(e) => format!("Error: {}", e),
117 };
118
119 let safe_calculator = safe_divide.and_then(format_result);
120 println!(" 10 / 2 = {}", safe_calculator.apply(10, 2));
121 println!(" 10 / 0 = {}", safe_calculator.apply(10, 0));
122 println!();
123
124 // 9. Complex data structures
125 println!("9. Complex data structures");
126 #[derive(Debug)]
127 struct Point {
128 x: i32,
129 y: i32,
130 }
131
132 let create_point = BoxBiTransformer::new(|x: i32, y: i32| Point { x, y });
133 let distance_from_origin = |p: Point| ((p.x * p.x + p.y * p.y) as f64).sqrt();
134 let format_distance = |d: f64| format!("{:.2}", d);
135
136 let point_processor = create_point
137 .and_then(distance_from_origin)
138 .and_then(format_distance);
139 println!(
140 " Distance from point(3, 4) to origin: {}",
141 point_processor.apply(3, 4)
142 );
143 println!();
144
145 // 10. Combined usage with when
146 println!("10. Combined usage with when");
147 let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
148 let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
149
150 let conditional = add_when
151 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
152 .or_else(multiply_when);
153
154 let double_result = |x: i32| x * 2;
155 let final_transformer = conditional.and_then(double_result);
156
157 println!(
158 " Add positive numbers then double: (5 + 3) * 2 = {}",
159 final_transformer.apply(5, 3)
160 );
161 println!(
162 " Multiply negative numbers then double: (-5 * 3) * 2 = {}",
163 final_transformer.apply(-5, 3)
164 );
165
166 println!("\n=== Demo completed ===");
167}Sourcepub fn when<P>(&self, predicate: P) -> ArcConditionalBiTransformer<T, U, R>
pub fn when<P>(&self, predicate: P) -> ArcConditionalBiTransformer<T, U, R>
Creates a conditional bi-transformer (thread-safe version)
Returns a bi-transformer that only executes when a bi-predicate is
satisfied. You must call or_else() to provide an alternative
bi-transformer.
§Parameters
predicate- The condition to check. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original bi-predicate, clone it first (if it implementsClone). Must beSend + Sync, can be:- A closure:
|x: &T, y: &U| -> bool(requiresSend + Sync) - A function pointer:
fn(&T, &U) -> bool - An
ArcBiPredicate<T, U> - Any type implementing
BiPredicate<T, U> + Send + Sync
- A closure:
§Returns
Returns ArcConditionalBiTransformer<T, U, R>
§Examples
§Basic usage with or_else
use prism3_function::{BiTransformer, ArcBiTransformer};
let add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
let multiply = ArcBiTransformer::new(|x: i32, y: i32| x * y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0)
.or_else(multiply);
let conditional_clone = conditional.clone();
assert_eq!(conditional.apply(5, 3), 8);
assert_eq!(conditional_clone.apply(-5, 3), -15);§Preserving bi-predicate with clone
use prism3_function::{BiTransformer, ArcBiTransformer, ArcBiPredicate};
let add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
let both_positive = ArcBiPredicate::new(|x: &i32, y: &i32|
*x > 0 && *y > 0);
// Clone to preserve original bi-predicate
let conditional = add.when(both_positive.clone())
.or_else(ArcBiTransformer::new(|x, y| x * y));
assert_eq!(conditional.apply(5, 3), 8);
// Original bi-predicate still usable
assert!(both_positive.test(&5, &3));