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 { ... }
fn into_fn(self) -> impl FnOnce(T, U) -> R
where Self: Sized + 'static { ... }
fn to_box(&self) -> BoxBiTransformerOnce<T, U, R>
where Self: Clone + 'static { ... }
fn to_fn(&self) -> impl FnOnce(T, U) -> R
where Self: Clone + '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
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxBiTransformerOnce<T, U, R>where
Self: Sized + 'static,
fn into_box(self) -> BoxBiTransformerOnce<T, U, R>where
Self: Sized + '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?
17fn main() {
18 println!("=== BiTransformerOnce Examples ===\n");
19
20 // Example 1: Basic usage with closure
21 println!("1. Basic usage with closure:");
22 let add = |x: i32, y: i32| x + y;
23 let result = add.apply(20, 22);
24 println!(" 20 + 22 = {}", result);
25
26 // Example 2: BoxBiTransformerOnce with new
27 println!("\n2. BoxBiTransformerOnce with new:");
28 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
29 println!(" 6 * 7 = {}", multiply.apply(6, 7));
30
31 // Example 3: Constant transformer
32 println!("\n3. Constant transformer:");
33 let constant = BoxBiTransformerOnce::constant("hello");
34 println!(" constant(123, 456) = {}", constant.apply(123, 456));
35
36 // Example 4: Consuming owned values
37 println!("\n4. Consuming owned values:");
38 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
39 let s1 = String::from("hello");
40 let s2 = String::from("world");
41 let result = concat.apply(s1, s2);
42 println!(" concat('hello', 'world') = {}", result);
43
44 // Example 5: Conditional transformation with when/or_else
45 println!("\n5. Conditional transformation (positive numbers):");
46 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
47 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
48 let conditional = add
49 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
50 .or_else(multiply);
51 println!(" conditional(5, 3) = {} (add)", conditional.apply(5, 3));
52
53 println!("\n6. Conditional transformation (negative numbers):");
54 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
55 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
56 let conditional2 = add2
57 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
58 .or_else(multiply2);
59 println!(
60 " conditional(-5, 3) = {} (multiply)",
61 conditional2.apply(-5, 3)
62 );
63
64 // Example 7: Conditional with closure in or_else
65 println!("\n7. Conditional with closure in or_else:");
66 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
67 let conditional3 = add3
68 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
69 .or_else(|x: i32, y: i32| x * y);
70 println!(" conditional(4, 6) = {}", conditional3.apply(4, 6));
71
72 // Example 8: Merging vectors
73 println!("\n8. Merging vectors:");
74 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
75 x.extend(y);
76 x
77 });
78 let v1 = vec![1, 2, 3];
79 let v2 = vec![4, 5, 6];
80 let result = merge.apply(v1, v2);
81 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
82
83 // Example 9: Complex transformation with calculation
84 println!("\n9. Complex transformation with calculation:");
85 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
86 let sum = x + y;
87 let product = x * y;
88 (sum, product)
89 });
90 let (sum, product) = calculate.apply(5, 3);
91 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
92
93 // Example 10: String manipulation
94 println!("\n10. String manipulation:");
95 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
96 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
97 });
98 println!(
99 " process('Hello', 'WORLD') = {}",
100 process.apply("Hello".to_string(), "WORLD".to_string())
101 );
102
103 // Example 11: Converting to function
104 println!("\n11. Converting to function:");
105 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
106 let f = add4.into_fn();
107 println!(" f(10, 20) = {}", f(10, 20));
108
109 // Example 12: Converting to box (zero-cost)
110 println!("\n12. Converting to box (zero-cost):");
111 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
112 let boxed = add5.into_box();
113 println!(" boxed(15, 25) = {}", boxed.apply(15, 25));
114
115 println!("\n=== All examples completed successfully! ===");
116}Sourcefn into_fn(self) -> impl FnOnce(T, U) -> Rwhere
Self: Sized + 'static,
fn into_fn(self) -> impl FnOnce(T, U) -> Rwhere
Self: Sized + '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?
17fn main() {
18 println!("=== BiTransformerOnce Examples ===\n");
19
20 // Example 1: Basic usage with closure
21 println!("1. Basic usage with closure:");
22 let add = |x: i32, y: i32| x + y;
23 let result = add.apply(20, 22);
24 println!(" 20 + 22 = {}", result);
25
26 // Example 2: BoxBiTransformerOnce with new
27 println!("\n2. BoxBiTransformerOnce with new:");
28 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
29 println!(" 6 * 7 = {}", multiply.apply(6, 7));
30
31 // Example 3: Constant transformer
32 println!("\n3. Constant transformer:");
33 let constant = BoxBiTransformerOnce::constant("hello");
34 println!(" constant(123, 456) = {}", constant.apply(123, 456));
35
36 // Example 4: Consuming owned values
37 println!("\n4. Consuming owned values:");
38 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
39 let s1 = String::from("hello");
40 let s2 = String::from("world");
41 let result = concat.apply(s1, s2);
42 println!(" concat('hello', 'world') = {}", result);
43
44 // Example 5: Conditional transformation with when/or_else
45 println!("\n5. Conditional transformation (positive numbers):");
46 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
47 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
48 let conditional = add
49 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
50 .or_else(multiply);
51 println!(" conditional(5, 3) = {} (add)", conditional.apply(5, 3));
52
53 println!("\n6. Conditional transformation (negative numbers):");
54 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
55 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
56 let conditional2 = add2
57 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
58 .or_else(multiply2);
59 println!(
60 " conditional(-5, 3) = {} (multiply)",
61 conditional2.apply(-5, 3)
62 );
63
64 // Example 7: Conditional with closure in or_else
65 println!("\n7. Conditional with closure in or_else:");
66 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
67 let conditional3 = add3
68 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
69 .or_else(|x: i32, y: i32| x * y);
70 println!(" conditional(4, 6) = {}", conditional3.apply(4, 6));
71
72 // Example 8: Merging vectors
73 println!("\n8. Merging vectors:");
74 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
75 x.extend(y);
76 x
77 });
78 let v1 = vec![1, 2, 3];
79 let v2 = vec![4, 5, 6];
80 let result = merge.apply(v1, v2);
81 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
82
83 // Example 9: Complex transformation with calculation
84 println!("\n9. Complex transformation with calculation:");
85 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
86 let sum = x + y;
87 let product = x * y;
88 (sum, product)
89 });
90 let (sum, product) = calculate.apply(5, 3);
91 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
92
93 // Example 10: String manipulation
94 println!("\n10. String manipulation:");
95 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
96 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
97 });
98 println!(
99 " process('Hello', 'WORLD') = {}",
100 process.apply("Hello".to_string(), "WORLD".to_string())
101 );
102
103 // Example 11: Converting to function
104 println!("\n11. Converting to function:");
105 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
106 let f = add4.into_fn();
107 println!(" f(10, 20) = {}", f(10, 20));
108
109 // Example 12: Converting to box (zero-cost)
110 println!("\n12. Converting to box (zero-cost):");
111 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
112 let boxed = add5.into_box();
113 println!(" boxed(15, 25) = {}", boxed.apply(15, 25));
114
115 println!("\n=== All examples completed successfully! ===");
116}Sourcefn to_box(&self) -> BoxBiTransformerOnce<T, U, R>where
Self: Clone + 'static,
fn to_box(&self) -> BoxBiTransformerOnce<T, U, R>where
Self: Clone + '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 qubit_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,
fn to_fn(&self) -> impl FnOnce(T, U) -> Rwhere
Self: Clone + '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 qubit_function::BiTransformerOnce;
let add = |x: i32, y: i32| x + y;
let func = add.to_fn();
assert_eq!(func(20, 22), 42);