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