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§
Provided Methods§
Sourcefn into_box(self) -> BoxBiTransformer<T, U, R>where
Self: Sized + 'static,
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>
Sourcefn into_rc(self) -> RcBiTransformer<T, U, R>where
Self: Sized + 'static,
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?
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!(" arc_add_clone.apply(5, 8) = {}", arc_add_clone.apply(5, 8));
41 println!();
42
43 // 3. RcBiTransformer - Single-threaded, cloneable
44 println!("3. RcBiTransformer - Single-threaded, cloneable");
45 let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
46 let rc_multiply_clone = rc_multiply.clone();
47
48 println!(" rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
49 println!(" rc_multiply_clone.apply(5, 6) = {}", rc_multiply_clone.apply(5, 6));
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 = BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
73 println!(
74 " format.apply(\"Alice\", 30) = {}",
75 format.apply("Alice".to_string(), 30)
76 );
77 println!();
78
79 // 6. Closure as BiTransformer
80 println!("6. Closure as BiTransformer");
81 let subtract = |x: i32, y: i32| x - y;
82 println!(" subtract.apply(42, 10) = {}", subtract.apply(42, 10));
83 println!();
84
85 // 7. Conversion between types
86 println!("7. Conversion between types");
87 let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
88 let rc_add = box_add.into_rc();
89 println!(" Converted BoxBiTransformer to RcBiTransformer");
90 println!(" rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
91 println!();
92
93 // 8. Safe division with Option
94 println!("8. Safe division with Option");
95 let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
96 println!(" safe_divide.apply(42, 2) = {:?}", safe_divide.apply(42, 2));
97 println!(" safe_divide.apply(42, 0) = {:?}", safe_divide.apply(42, 0));
98 println!();
99
100 // 9. String concatenation
101 println!("9. String concatenation");
102 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
103 println!(
104 " concat.apply(\"Hello\", \"World\") = {}",
105 concat.apply("Hello".to_string(), "World".to_string())
106 );
107 println!();
108
109 println!("=== Demo Complete ===");
110}Sourcefn into_arc(self) -> ArcBiTransformer<T, U, R>
fn into_arc(self) -> ArcBiTransformer<T, U, R>
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>
Sourcefn into_fn(self) -> impl Fn(T, U) -> Rwhere
Self: Sized + 'static,
fn into_fn(self) -> impl Fn(T, U) -> Rwhere
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
Sourcefn into_once(self) -> BoxBiTransformerOnce<T, U, R>where
Self: Sized + 'static,
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>
Sourcefn to_box(&self) -> BoxBiTransformer<T, U, R>
fn to_box(&self) -> BoxBiTransformer<T, U, R>
Non-consuming conversion to BoxBiTransformer using &self.
Default implementation clones self and delegates to into_box.
Sourcefn to_rc(&self) -> RcBiTransformer<T, U, R>
fn to_rc(&self) -> RcBiTransformer<T, U, R>
Non-consuming conversion to RcBiTransformer using &self.
Default implementation clones self and delegates to into_rc.
Sourcefn to_arc(&self) -> ArcBiTransformer<T, U, R>
fn to_arc(&self) -> ArcBiTransformer<T, U, R>
Non-consuming conversion to ArcBiTransformer using &self.
Default implementation clones self and delegates to into_arc.
Sourcefn to_fn(&self) -> impl Fn(T, U) -> R
fn to_fn(&self) -> impl Fn(T, U) -> R
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.
Sourcefn to_once(&self) -> BoxBiTransformerOnce<T, U, R>where
Self: Clone + 'static,
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>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl<F, T, U, R> BiTransformer<T, U, R> for Fwhere
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);