Skip to main content

BiTransformer

Trait BiTransformer 

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

    // Provided methods
    fn into_box(self) -> BoxBiTransformer<T, U, R>
       where Self: Sized + 'static { ... }
    fn into_rc(self) -> RcBiTransformer<T, U, R>
       where Self: Sized + 'static { ... }
    fn into_arc(self) -> ArcBiTransformer<T, U, R>
       where Self: Sized + Send + Sync + 'static { ... }
    fn into_fn(self) -> impl Fn(T, U) -> R
       where Self: Sized + 'static { ... }
    fn into_once(self) -> BoxBiTransformerOnce<T, U, R>
       where Self: Sized + 'static { ... }
    fn to_box(&self) -> BoxBiTransformer<T, U, R>
       where Self: Sized + Clone + 'static { ... }
    fn to_rc(&self) -> RcBiTransformer<T, U, R>
       where Self: Sized + Clone + 'static { ... }
    fn to_arc(&self) -> ArcBiTransformer<T, U, R>
       where Self: Sized + Clone + Send + Sync + 'static { ... }
    fn to_fn(&self) -> impl Fn(T, U) -> R
       where Self: Sized + Clone + 'static { ... }
    fn to_once(&self) -> BoxBiTransformerOnce<T, U, R>
       where Self: Clone + 'static { ... }
}
Expand description

BiTransformer trait - transforms two values to produce a result

Defines the behavior of a bi-transformation: converting two values of types T and U to a value of type R by consuming the inputs. This is analogous to Fn(T, U) -> R in Rust’s standard library.

§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§

Source

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

Transforms two input values to produce an output value

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

The transformed output value

Provided Methods§

Source

fn into_box(self) -> BoxBiTransformer<T, U, R>
where Self: Sized + 'static,

Converts to BoxBiTransformer

⚠️ Consumes self: The original bi-transformer becomes unavailable after calling this method.

§Default Implementation

The default implementation wraps self in a Box and creates a BoxBiTransformer. Types can override this method to provide more efficient conversions.

§Returns

Returns BoxBiTransformer<T, U, R>

Source

fn into_rc(self) -> RcBiTransformer<T, U, R>
where Self: Sized + 'static,

Converts to RcBiTransformer

⚠️ Consumes self: The original bi-transformer becomes unavailable after calling this method.

§Default Implementation

The default implementation wraps self in an Rc and creates an RcBiTransformer. Types can override this method to provide more efficient conversions.

§Returns

Returns RcBiTransformer<T, U, R>

Examples found in repository?
examples/transformers/bi_transformer_demo.rs (line 95)
18fn main() {
19    println!("=== BiTransformer Demo ===\n");
20
21    // 1. BoxBiTransformer - Single ownership
22    println!("1. BoxBiTransformer - Single ownership");
23    let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
24    println!("   add.apply(20, 22) = {}", add.apply(20, 22));
25
26    let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
27    println!("   multiply.apply(6, 7) = {}", multiply.apply(6, 7));
28
29    // Constant bi-transformer
30    let constant = BoxBiTransformer::constant("hello");
31    println!("   constant.apply(1, 2) = {}", constant.apply(1, 2));
32    println!();
33
34    // 2. ArcBiTransformer - Thread-safe, cloneable
35    println!("2. ArcBiTransformer - Thread-safe, cloneable");
36    let arc_add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
37    let arc_add_clone = arc_add.clone();
38
39    println!("   arc_add.apply(10, 15) = {}", arc_add.apply(10, 15));
40    println!(
41        "   arc_add_clone.apply(5, 8) = {}",
42        arc_add_clone.apply(5, 8)
43    );
44    println!();
45
46    // 3. RcBiTransformer - Single-threaded, cloneable
47    println!("3. RcBiTransformer - Single-threaded, cloneable");
48    let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
49    let rc_multiply_clone = rc_multiply.clone();
50
51    println!("   rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
52    println!(
53        "   rc_multiply_clone.apply(5, 6) = {}",
54        rc_multiply_clone.apply(5, 6)
55    );
56    println!();
57
58    // 4. Conditional BiTransformer
59    println!("4. Conditional BiTransformer");
60    let add_if_positive = BoxBiTransformer::new(|x: i32, y: i32| x + y);
61    let multiply_otherwise = BoxBiTransformer::new(|x: i32, y: i32| x * y);
62    let conditional = add_if_positive
63        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
64        .or_else(multiply_otherwise);
65
66    println!(
67        "   conditional.apply(5, 3) = {} (both positive, add)",
68        conditional.apply(5, 3)
69    );
70    println!(
71        "   conditional.apply(-5, 3) = {} (not both positive, multiply)",
72        conditional.apply(-5, 3)
73    );
74    println!();
75
76    // 5. Working with different types
77    println!("5. Working with different types");
78    let format =
79        BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
80    println!(
81        "   format.apply(\"Alice\", 30) = {}",
82        format.apply("Alice".to_string(), 30)
83    );
84    println!();
85
86    // 6. Closure as BiTransformer
87    println!("6. Closure as BiTransformer");
88    let subtract = |x: i32, y: i32| x - y;
89    println!("   subtract.apply(42, 10) = {}", subtract.apply(42, 10));
90    println!();
91
92    // 7. Conversion between types
93    println!("7. Conversion between types");
94    let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
95    let rc_add = box_add.into_rc();
96    println!("   Converted BoxBiTransformer to RcBiTransformer");
97    println!("   rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
98    println!();
99
100    // 8. Safe division with Option
101    println!("8. Safe division with Option");
102    let safe_divide =
103        BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
104    println!(
105        "   safe_divide.apply(42, 2) = {:?}",
106        safe_divide.apply(42, 2)
107    );
108    println!(
109        "   safe_divide.apply(42, 0) = {:?}",
110        safe_divide.apply(42, 0)
111    );
112    println!();
113
114    // 9. String concatenation
115    println!("9. String concatenation");
116    let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
117    println!(
118        "   concat.apply(\"Hello\", \"World\") = {}",
119        concat.apply("Hello".to_string(), "World".to_string())
120    );
121    println!();
122
123    println!("=== Demo Complete ===");
124}
Source

fn into_arc(self) -> ArcBiTransformer<T, U, R>
where Self: Sized + Send + Sync + 'static,

Converts to ArcBiTransformer

⚠️ Consumes self: The original bi-transformer becomes unavailable after calling this method.

§Default Implementation

The default implementation wraps self in an Arc and creates an ArcBiTransformer. Types can override this method to provide more efficient conversions.

§Returns

Returns ArcBiTransformer<T, U, R>

Source

fn into_fn(self) -> impl Fn(T, U) -> R
where Self: Sized + 'static,

Converts bi-transformer to a closure

⚠️ Consumes self: The original bi-transformer becomes unavailable after calling this method.

§Default Implementation

The default implementation creates a closure that captures self and calls its apply method. Types can override this method to provide more efficient conversions.

§Returns

Returns a closure that implements Fn(T, U) -> R

Source

fn into_once(self) -> BoxBiTransformerOnce<T, U, R>
where Self: Sized + 'static,

Convert to BiTransformerOnce

⚠️ Consumes self: The original bi-transformer will be unavailable after calling this method.

Converts a reusable bi-transformer to a one-time bi-transformer that consumes itself on use. This enables passing BiTransformer to functions that require BiTransformerOnce.

§Returns

Returns a BoxBiTransformerOnce<T, U, R>

Source

fn to_box(&self) -> BoxBiTransformer<T, U, R>
where Self: Sized + Clone + 'static,

Non-consuming conversion to BoxBiTransformer using &self.

Default implementation clones self and delegates to into_box.

Source

fn to_rc(&self) -> RcBiTransformer<T, U, R>
where Self: Sized + Clone + 'static,

Non-consuming conversion to RcBiTransformer using &self.

Default implementation clones self and delegates to into_rc.

Source

fn to_arc(&self) -> ArcBiTransformer<T, U, R>
where Self: Sized + Clone + Send + Sync + 'static,

Non-consuming conversion to ArcBiTransformer using &self.

Default implementation clones self and delegates to into_arc.

Source

fn to_fn(&self) -> impl Fn(T, U) -> R
where Self: Sized + Clone + 'static,

Non-consuming conversion to a boxed function using &self.

Returns a Box<dyn Fn(T, U) -> R> that clones self and calls apply inside the boxed closure.

Source

fn to_once(&self) -> BoxBiTransformerOnce<T, U, R>
where Self: Clone + 'static,

Convert to BiTransformerOnce without consuming self

⚠️ Requires Clone: This method requires Self to implement Clone. Clones the current bi-transformer and converts the clone to a one-time bi-transformer.

§Returns

Returns a BoxBiTransformerOnce<T, U, R>

Implementors§

Source§

impl<F, T, U, R> BiTransformer<T, U, R> for F
where F: Fn(T, U) -> R,

Implement BiTransformer<T, U, R> for any type that implements Fn(T, U) -> R

This allows closures and function pointers to be used directly with our BiTransformer trait without wrapping.

§Examples

use qubit_function::BiTransformer;

fn add(x: i32, y: i32) -> i32 { x + y }

assert_eq!(add.apply(20, 22), 42);

let multiply = |x: i32, y: i32| x * y;
assert_eq!(multiply.apply(6, 7), 42);
Source§

impl<T, U, R> BiTransformer<T, U, R> for ArcBiTransformer<T, U, R>

Source§

impl<T, U, R> BiTransformer<T, U, R> for BoxBiTransformer<T, U, R>

Source§

impl<T, U, R> BiTransformer<T, U, R> for RcBiTransformer<T, U, R>