BiTransformerOnce

Trait BiTransformerOnce 

Source
pub trait BiTransformerOnce<T, U, R> {
    // Required method
    fn apply_once(self, first: T, second: U) -> R;

    // Provided methods
    fn into_box_once(self) -> BoxBiTransformerOnce<T, U, R>
       where Self: Sized + 'static,
             T: 'static,
             U: 'static,
             R: 'static { ... }
    fn into_fn_once(self) -> impl FnOnce(T, U) -> R
       where Self: Sized + 'static,
             T: 'static,
             U: 'static,
             R: 'static { ... }
    fn to_box_once(&self) -> BoxBiTransformerOnce<T, U, R>
       where Self: Clone + 'static,
             T: 'static,
             U: 'static,
             R: 'static { ... }
    fn to_fn_once(&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§

Source

fn apply_once(self, first: T, second: U) -> R

Transforms two input values, consuming self and both inputs

§Parameters
  • first - The first input value (consumed)
  • second - The second input value (consumed)
§Returns

The transformed output value

Provided Methods§

Source

fn into_box_once(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?
examples/bi_transformer_once_demo.rs (line 111)
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_once(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_once(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_once(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_once(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!(
48        "   conditional(5, 3) = {} (add)",
49        conditional.apply_once(5, 3)
50    );
51
52    println!("\n6. Conditional transformation (negative numbers):");
53    let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
54    let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
55    let conditional2 = add2
56        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
57        .or_else(multiply2);
58    println!(
59        "   conditional(-5, 3) = {} (multiply)",
60        conditional2.apply_once(-5, 3)
61    );
62
63    // Example 7: Conditional with closure in or_else
64    println!("\n7. Conditional with closure in or_else:");
65    let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
66    let conditional3 = add3
67        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68        .or_else(|x: i32, y: i32| x * y);
69    println!("   conditional(4, 6) = {}", conditional3.apply_once(4, 6));
70
71    // Example 8: Merging vectors
72    println!("\n8. Merging vectors:");
73    let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
74        x.extend(y);
75        x
76    });
77    let v1 = vec![1, 2, 3];
78    let v2 = vec![4, 5, 6];
79    let result = merge.apply_once(v1, v2);
80    println!("   merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
81
82    // Example 9: Complex transformation with calculation
83    println!("\n9. Complex transformation with calculation:");
84    let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
85        let sum = x + y;
86        let product = x * y;
87        (sum, product)
88    });
89    let (sum, product) = calculate.apply_once(5, 3);
90    println!("   calculate(5, 3) = (sum: {}, product: {})", sum, product);
91
92    // Example 10: String manipulation
93    println!("\n10. String manipulation:");
94    let process = BoxBiTransformerOnce::new(|x: String, y: String| {
95        format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
96    });
97    println!(
98        "   process('Hello', 'WORLD') = {}",
99        process.apply_once("Hello".to_string(), "WORLD".to_string())
100    );
101
102    // Example 11: Converting to function
103    println!("\n11. Converting to function:");
104    let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105    let f = add4.into_fn_once();
106    println!("   f(10, 20) = {}", f(10, 20));
107
108    // Example 12: Converting to box (zero-cost)
109    println!("\n12. Converting to box (zero-cost):");
110    let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
111    let boxed = add5.into_box_once();
112    println!("   boxed(15, 25) = {}", boxed.apply_once(15, 25));
113
114    println!("\n=== All examples completed successfully! ===");
115}
Source

fn into_fn_once(self) -> impl FnOnce(T, U) -> R
where 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?
examples/bi_transformer_once_demo.rs (line 105)
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_once(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_once(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_once(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_once(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!(
48        "   conditional(5, 3) = {} (add)",
49        conditional.apply_once(5, 3)
50    );
51
52    println!("\n6. Conditional transformation (negative numbers):");
53    let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
54    let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
55    let conditional2 = add2
56        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
57        .or_else(multiply2);
58    println!(
59        "   conditional(-5, 3) = {} (multiply)",
60        conditional2.apply_once(-5, 3)
61    );
62
63    // Example 7: Conditional with closure in or_else
64    println!("\n7. Conditional with closure in or_else:");
65    let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
66    let conditional3 = add3
67        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68        .or_else(|x: i32, y: i32| x * y);
69    println!("   conditional(4, 6) = {}", conditional3.apply_once(4, 6));
70
71    // Example 8: Merging vectors
72    println!("\n8. Merging vectors:");
73    let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
74        x.extend(y);
75        x
76    });
77    let v1 = vec![1, 2, 3];
78    let v2 = vec![4, 5, 6];
79    let result = merge.apply_once(v1, v2);
80    println!("   merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
81
82    // Example 9: Complex transformation with calculation
83    println!("\n9. Complex transformation with calculation:");
84    let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
85        let sum = x + y;
86        let product = x * y;
87        (sum, product)
88    });
89    let (sum, product) = calculate.apply_once(5, 3);
90    println!("   calculate(5, 3) = (sum: {}, product: {})", sum, product);
91
92    // Example 10: String manipulation
93    println!("\n10. String manipulation:");
94    let process = BoxBiTransformerOnce::new(|x: String, y: String| {
95        format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
96    });
97    println!(
98        "   process('Hello', 'WORLD') = {}",
99        process.apply_once("Hello".to_string(), "WORLD".to_string())
100    );
101
102    // Example 11: Converting to function
103    println!("\n11. Converting to function:");
104    let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105    let f = add4.into_fn_once();
106    println!("   f(10, 20) = {}", f(10, 20));
107
108    // Example 12: Converting to box (zero-cost)
109    println!("\n12. Converting to box (zero-cost):");
110    let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
111    let boxed = add5.into_box_once();
112    println!("   boxed(15, 25) = {}", boxed.apply_once(15, 25));
113
114    println!("\n=== All examples completed successfully! ===");
115}
Source

fn to_box_once(&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_once();
assert_eq!(func(20, 22), 42);
Source

fn to_fn_once(&self) -> impl FnOnce(T, U) -> R
where 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_once();
assert_eq!(func(20, 22), 42);

Implementors§

Source§

impl<F, T, U, R> BiTransformerOnce<T, U, R> for F
where 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_once(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_once(owned_x, owned_y), "hello world");

§Author

Hu Haixing

Source§

impl<T, U, R> BiTransformerOnce<T, U, R> for ArcBiTransformer<T, U, R>
where T: 'static, U: 'static, R: 'static,

Source§

impl<T, U, R> BiTransformerOnce<T, U, R> for BoxBiTransformer<T, U, R>
where T: 'static, U: 'static, R: 'static,

Source§

impl<T, U, R> BiTransformerOnce<T, U, R> for RcBiTransformer<T, U, R>
where T: 'static, U: 'static, R: 'static,

Source§

impl<T, U, R> BiTransformerOnce<T, U, R> for BoxBiTransformerOnce<T, U, R>