pub trait BiTransformerOnce<T, U, R> {
// Required method
fn apply(self, first: T, second: U) -> R;
// Provided methods
fn into_box(self) -> BoxBiTransformerOnce<T, U, R>
where Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn into_fn(self) -> impl FnOnce(T, U) -> R
where Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn to_box(&self) -> BoxBiTransformerOnce<T, U, R>
where Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn to_fn(&self) -> impl FnOnce(T, U) -> R
where Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
}Expand description
BiTransformerOnce trait - consuming bi-transformation that takes ownership
Defines the behavior of a consuming bi-transformer: converting two values of
types T and U to a value of type R by taking ownership of self and
both inputs. This trait is analogous to FnOnce(T, U) -> R.
§Type Parameters
T- The type of the first input value (consumed)U- The type of the second input value (consumed)R- The type of the output value
§Author
Hu Haixing
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxBiTransformerOnce<T, U, R>where
Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static,
fn into_box(self) -> BoxBiTransformerOnce<T, U, R>where
Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static,
Converts to BoxBiTransformerOnce
⚠️ Consumes self: The original bi-transformer becomes unavailable
after calling this method.
§Returns
Returns BoxBiTransformerOnce<T, U, R>
Examples found in repository?
13fn main() {
14 println!("=== BiTransformerOnce Examples ===\n");
15
16 // Example 1: Basic usage with closure
17 println!("1. Basic usage with closure:");
18 let add = |x: i32, y: i32| x + y;
19 let result = add.apply(20, 22);
20 println!(" 20 + 22 = {}", result);
21
22 // Example 2: BoxBiTransformerOnce with new
23 println!("\n2. BoxBiTransformerOnce with new:");
24 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
25 println!(" 6 * 7 = {}", multiply.apply(6, 7));
26
27 // Example 3: Constant transformer
28 println!("\n3. Constant transformer:");
29 let constant = BoxBiTransformerOnce::constant("hello");
30 println!(" constant(123, 456) = {}", constant.apply(123, 456));
31
32 // Example 4: Consuming owned values
33 println!("\n4. Consuming owned values:");
34 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
35 let s1 = String::from("hello");
36 let s2 = String::from("world");
37 let result = concat.apply(s1, s2);
38 println!(" concat('hello', 'world') = {}", result);
39
40 // Example 5: Conditional transformation with when/or_else
41 println!("\n5. Conditional transformation (positive numbers):");
42 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
43 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
44 let conditional = add
45 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
46 .or_else(multiply);
47 println!(" conditional(5, 3) = {} (add)", conditional.apply(5, 3));
48
49 println!("\n6. Conditional transformation (negative numbers):");
50 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
51 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
52 let conditional2 = add2
53 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
54 .or_else(multiply2);
55 println!(
56 " conditional(-5, 3) = {} (multiply)",
57 conditional2.apply(-5, 3)
58 );
59
60 // Example 7: Conditional with closure in or_else
61 println!("\n7. Conditional with closure in or_else:");
62 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
63 let conditional3 = add3
64 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
65 .or_else(|x: i32, y: i32| x * y);
66 println!(" conditional(4, 6) = {}", conditional3.apply(4, 6));
67
68 // Example 8: Merging vectors
69 println!("\n8. Merging vectors:");
70 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
71 x.extend(y);
72 x
73 });
74 let v1 = vec![1, 2, 3];
75 let v2 = vec![4, 5, 6];
76 let result = merge.apply(v1, v2);
77 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
78
79 // Example 9: Complex transformation with calculation
80 println!("\n9. Complex transformation with calculation:");
81 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
82 let sum = x + y;
83 let product = x * y;
84 (sum, product)
85 });
86 let (sum, product) = calculate.apply(5, 3);
87 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
88
89 // Example 10: String manipulation
90 println!("\n10. String manipulation:");
91 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
92 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
93 });
94 println!(
95 " process('Hello', 'WORLD') = {}",
96 process.apply("Hello".to_string(), "WORLD".to_string())
97 );
98
99 // Example 11: Converting to function
100 println!("\n11. Converting to function:");
101 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
102 let f = add4.into_fn();
103 println!(" f(10, 20) = {}", f(10, 20));
104
105 // Example 12: Converting to box (zero-cost)
106 println!("\n12. Converting to box (zero-cost):");
107 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
108 let boxed = add5.into_box();
109 println!(" boxed(15, 25) = {}", boxed.apply(15, 25));
110
111 println!("\n=== All examples completed successfully! ===");
112}Sourcefn into_fn(self) -> impl FnOnce(T, U) -> Rwhere
Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static,
fn into_fn(self) -> impl FnOnce(T, U) -> Rwhere
Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static,
Converts bi-transformer to a closure
⚠️ Consumes self: The original bi-transformer becomes unavailable
after calling this method.
§Returns
Returns a closure that implements FnOnce(T, U) -> R
Examples found in repository?
13fn main() {
14 println!("=== BiTransformerOnce Examples ===\n");
15
16 // Example 1: Basic usage with closure
17 println!("1. Basic usage with closure:");
18 let add = |x: i32, y: i32| x + y;
19 let result = add.apply(20, 22);
20 println!(" 20 + 22 = {}", result);
21
22 // Example 2: BoxBiTransformerOnce with new
23 println!("\n2. BoxBiTransformerOnce with new:");
24 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
25 println!(" 6 * 7 = {}", multiply.apply(6, 7));
26
27 // Example 3: Constant transformer
28 println!("\n3. Constant transformer:");
29 let constant = BoxBiTransformerOnce::constant("hello");
30 println!(" constant(123, 456) = {}", constant.apply(123, 456));
31
32 // Example 4: Consuming owned values
33 println!("\n4. Consuming owned values:");
34 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
35 let s1 = String::from("hello");
36 let s2 = String::from("world");
37 let result = concat.apply(s1, s2);
38 println!(" concat('hello', 'world') = {}", result);
39
40 // Example 5: Conditional transformation with when/or_else
41 println!("\n5. Conditional transformation (positive numbers):");
42 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
43 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
44 let conditional = add
45 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
46 .or_else(multiply);
47 println!(" conditional(5, 3) = {} (add)", conditional.apply(5, 3));
48
49 println!("\n6. Conditional transformation (negative numbers):");
50 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
51 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
52 let conditional2 = add2
53 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
54 .or_else(multiply2);
55 println!(
56 " conditional(-5, 3) = {} (multiply)",
57 conditional2.apply(-5, 3)
58 );
59
60 // Example 7: Conditional with closure in or_else
61 println!("\n7. Conditional with closure in or_else:");
62 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
63 let conditional3 = add3
64 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
65 .or_else(|x: i32, y: i32| x * y);
66 println!(" conditional(4, 6) = {}", conditional3.apply(4, 6));
67
68 // Example 8: Merging vectors
69 println!("\n8. Merging vectors:");
70 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
71 x.extend(y);
72 x
73 });
74 let v1 = vec![1, 2, 3];
75 let v2 = vec![4, 5, 6];
76 let result = merge.apply(v1, v2);
77 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
78
79 // Example 9: Complex transformation with calculation
80 println!("\n9. Complex transformation with calculation:");
81 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
82 let sum = x + y;
83 let product = x * y;
84 (sum, product)
85 });
86 let (sum, product) = calculate.apply(5, 3);
87 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
88
89 // Example 10: String manipulation
90 println!("\n10. String manipulation:");
91 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
92 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
93 });
94 println!(
95 " process('Hello', 'WORLD') = {}",
96 process.apply("Hello".to_string(), "WORLD".to_string())
97 );
98
99 // Example 11: Converting to function
100 println!("\n11. Converting to function:");
101 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
102 let f = add4.into_fn();
103 println!(" f(10, 20) = {}", f(10, 20));
104
105 // Example 12: Converting to box (zero-cost)
106 println!("\n12. Converting to box (zero-cost):");
107 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
108 let boxed = add5.into_box();
109 println!(" boxed(15, 25) = {}", boxed.apply(15, 25));
110
111 println!("\n=== All examples completed successfully! ===");
112}Sourcefn to_box(&self) -> BoxBiTransformerOnce<T, U, R>where
Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static,
fn to_box(&self) -> BoxBiTransformerOnce<T, U, R>where
Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static,
Converts bi-transformer to a boxed function pointer
📌 Borrows &self: The original bi-transformer remains usable
after calling this method.
§Returns
Returns a boxed function pointer that implements FnOnce(T, U) -> R
§Examples
use prism3_function::BiTransformerOnce;
let add = |x: i32, y: i32| x + y;
let func = add.to_fn();
assert_eq!(func(20, 22), 42);Sourcefn to_fn(&self) -> impl FnOnce(T, U) -> Rwhere
Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static,
fn to_fn(&self) -> impl FnOnce(T, U) -> Rwhere
Self: Clone + 'static,
T: 'static,
U: 'static,
R: 'static,
Converts bi-transformer to a closure
📌 Borrows &self: The original bi-transformer remains usable
after calling this method.
§Returns
Returns a closure that implements FnOnce(T, U) -> R
§Examples
use prism3_function::BiTransformerOnce;
let add = |x: i32, y: i32| x + y;
let func = add.to_fn();
assert_eq!(func(20, 22), 42);Implementors§
impl<F, T, U, R> BiTransformerOnce<T, U, R> for Fwhere
F: FnOnce(T, U) -> R,
T: 'static,
U: 'static,
R: 'static,
Implement BiTransformerOnce<T, U, R> for any type that implements FnOnce(T, U) -> R
This allows once-callable closures and function pointers to be used directly with our BiTransformerOnce trait without wrapping.
§Examples
use prism3_function::BiTransformerOnce;
fn add(x: i32, y: i32) -> i32 {
x + y
}
assert_eq!(add.apply(20, 22), 42);
let owned_x = String::from("hello");
let owned_y = String::from("world");
let concat = |x: String, y: String| {
format!("{} {}", x, y)
};
assert_eq!(concat.apply(owned_x, owned_y), "hello world");§Author
Hu Haixing