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,
T: 'static,
U: 'static,
R: 'static { ... }
fn into_rc(self) -> RcBiTransformer<T, U, R>
where Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn into_arc(self) -> ArcBiTransformer<T, U, R>
where Self: Sized + Send + Sync + 'static,
T: Send + Sync + 'static,
U: Send + Sync + 'static,
R: Send + Sync + 'static { ... }
fn into_fn(self) -> impl Fn(T, U) -> R
where Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn to_box(&self) -> BoxBiTransformer<T, U, R>
where Self: Sized + Clone + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn to_rc(&self) -> RcBiTransformer<T, U, R>
where Self: Sized + Clone + 'static,
T: 'static,
U: 'static,
R: 'static { ... }
fn to_arc(&self) -> ArcBiTransformer<T, U, R>
where Self: Sized + Clone + Send + Sync + 'static,
T: Send + Sync + 'static,
U: Send + Sync + 'static,
R: Send + Sync + 'static { ... }
fn to_fn(&self) -> impl Fn(T, U) -> R
where Self: Sized + Clone + 'static,
T: 'static,
U: 'static,
R: '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
Hu Haixing
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxBiTransformer<T, U, R>where
Self: Sized + 'static,
T: 'static,
U: 'static,
R: 'static,
fn into_box(self) -> BoxBiTransformer<T, U, R>where
Self: Sized + 'static,
T: 'static,
U: 'static,
R: '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,
T: 'static,
U: 'static,
R: 'static,
fn into_rc(self) -> RcBiTransformer<T, U, R>where
Self: Sized + 'static,
T: 'static,
U: 'static,
R: '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?
12fn main() {
13 println!("=== BiTransformer Demo ===\n");
14
15 // 1. BoxBiTransformer - Single ownership
16 println!("1. BoxBiTransformer - Single ownership");
17 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
18 println!(" add.apply(20, 22) = {}", add.apply(20, 22));
19
20 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
21 println!(" multiply.apply(6, 7) = {}", multiply.apply(6, 7));
22
23 // Constant bi-transformer
24 let constant = BoxBiTransformer::constant("hello");
25 println!(" constant.apply(1, 2) = {}", constant.apply(1, 2));
26 println!();
27
28 // 2. ArcBiTransformer - Thread-safe, cloneable
29 println!("2. ArcBiTransformer - Thread-safe, cloneable");
30 let arc_add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
31 let arc_add_clone = arc_add.clone();
32
33 println!(" arc_add.apply(10, 15) = {}", arc_add.apply(10, 15));
34 println!(
35 " arc_add_clone.apply(5, 8) = {}",
36 arc_add_clone.apply(5, 8)
37 );
38 println!();
39
40 // 3. RcBiTransformer - Single-threaded, cloneable
41 println!("3. RcBiTransformer - Single-threaded, cloneable");
42 let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
43 let rc_multiply_clone = rc_multiply.clone();
44
45 println!(" rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
46 println!(
47 " rc_multiply_clone.apply(5, 6) = {}",
48 rc_multiply_clone.apply(5, 6)
49 );
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 =
73 BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
74 println!(
75 " format.apply(\"Alice\", 30) = {}",
76 format.apply("Alice".to_string(), 30)
77 );
78 println!();
79
80 // 6. Closure as BiTransformer
81 println!("6. Closure as BiTransformer");
82 let subtract = |x: i32, y: i32| x - y;
83 println!(" subtract.apply(42, 10) = {}", subtract.apply(42, 10));
84 println!();
85
86 // 7. Conversion between types
87 println!("7. Conversion between types");
88 let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
89 let rc_add = box_add.into_rc();
90 println!(" Converted BoxBiTransformer to RcBiTransformer");
91 println!(" rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
92 println!();
93
94 // 8. Safe division with Option
95 println!("8. Safe division with Option");
96 let safe_divide =
97 BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
98 println!(
99 " safe_divide.apply(42, 2) = {:?}",
100 safe_divide.apply(42, 2)
101 );
102 println!(
103 " safe_divide.apply(42, 0) = {:?}",
104 safe_divide.apply(42, 0)
105 );
106 println!();
107
108 // 9. String concatenation
109 println!("9. String concatenation");
110 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
111 println!(
112 " concat.apply(\"Hello\", \"World\") = {}",
113 concat.apply("Hello".to_string(), "World".to_string())
114 );
115 println!();
116
117 println!("=== Demo Complete ===");
118}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,
T: 'static,
U: 'static,
R: 'static,
fn into_fn(self) -> impl Fn(T, U) -> Rwhere
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.
§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 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.
Implementors§
impl<F, T, U, R> BiTransformer<T, U, R> for Fwhere
F: Fn(T, U) -> R,
T: 'static,
U: 'static,
R: 'static,
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 prism3_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
Hu Haixing