Skip to main content

ArcBiTransformer

Struct ArcBiTransformer 

Source
pub struct ArcBiTransformer<T, U, R> { /* private fields */ }
Expand description

ArcBiTransformer - thread-safe bi-transformer wrapper

A thread-safe, clonable bi-transformer wrapper suitable for multi-threaded scenarios. Can be called multiple times and shared across threads.

§Features

  • Based on: Arc<dyn Fn(T, U) -> R + Send + Sync>
  • Ownership: Shared ownership via reference counting
  • Reusability: Can be called multiple times (each call consumes its inputs)
  • Thread Safety: Thread-safe (Send + Sync required)
  • Clonable: Cheap cloning via Arc::clone

§Author

Haixing Hu

Implementations§

Source§

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

Source

pub fn new<F>(f: F) -> Self
where F: Fn(T, U) -> R + Send + Sync + 'static,

Creates a new bi-transformer.

Wraps the provided closure in the appropriate smart pointer type for this bi-transformer implementation.

Examples found in repository?
examples/transformers/bi_transformer_demo.rs (line 35)
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}
More examples
Hide additional examples
examples/transformers/bi_transformer_and_then_demo.rs (line 46)
24fn main() {
25    println!("=== BiTransformer and_then Method Demo ===\n");
26
27    // 1. BoxBiTransformer::and_then - Basic usage
28    println!("1. BoxBiTransformer::and_then - Basic usage");
29    let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
30    let double = |x: i32| x * 2;
31    let composed = add.and_then(double);
32    println!("   (3 + 5) * 2 = {}", composed.apply(3, 5));
33    println!();
34
35    // 2. BoxBiTransformer::and_then - Chained calls
36    println!("2. BoxBiTransformer::and_then - Chained calls");
37    let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
38    let add_ten = |x: i32| x + 10;
39    let to_string = |x: i32| format!("Result: {}", x);
40    let pipeline = multiply.and_then(add_ten).and_then(to_string);
41    println!("   (6 * 7) + 10 = {}", pipeline.apply(6, 7));
42    println!();
43
44    // 3. ArcBiTransformer::and_then - Shared ownership
45    println!("3. ArcBiTransformer::and_then - Shared ownership");
46    let add_arc = ArcBiTransformer::new(|x: i32, y: i32| x + y);
47    let triple = |x: i32| x * 3;
48    let composed_arc = add_arc.and_then(triple);
49
50    // Original bi-transformer is still available
51    println!("   Original: 20 + 22 = {}", add_arc.apply(20, 22));
52    println!("   Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
53    println!();
54
55    // 4. ArcBiTransformer::and_then - Cloneable
56    println!("4. ArcBiTransformer::and_then - Cloneable");
57    let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
58    let abs = |x: i32| x.abs();
59    let composed_abs = subtract.and_then(abs);
60    let cloned = composed_abs.clone();
61
62    println!("   Original: |10 - 15| = {}", composed_abs.apply(10, 15));
63    println!("   Cloned: |15 - 10| = {}", cloned.apply(15, 10));
64    println!();
65
66    // 5. RcBiTransformer::and_then - Single-threaded sharing
67    println!("5. RcBiTransformer::and_then - Single-threaded sharing");
68    let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
69    let square = |x: i32| x * x;
70    let composed_rc = divide.and_then(square);
71
72    println!("   Original: 20 / 4 = {}", divide.apply(20, 4));
73    println!("   Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
74    println!();
75
76    // 6. Type conversion example
77    println!("6. Type conversion example");
78    let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
79    let to_uppercase = |s: String| s.to_uppercase();
80    let get_length = |s: String| s.len();
81
82    let uppercase_pipeline = concat.and_then(to_uppercase);
83    println!(
84        "   \"hello\" + \"world\" -> uppercase: {}",
85        uppercase_pipeline.apply("hello".to_string(), "world".to_string())
86    );
87
88    let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
89    let length_pipeline = concat2.and_then(get_length);
90    println!(
91        "   \"hello\" + \"world\" -> length: {}",
92        length_pipeline.apply("hello".to_string(), "world".to_string())
93    );
94    println!();
95
96    // 7. Real application: Calculator
97    println!("7. Real application: Calculator");
98    let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
99    let round = |x: f64| x.round();
100    let to_int = |x: f64| x as i32;
101
102    let calculator = calculate.and_then(round).and_then(to_int);
103    println!(
104        "   3.7 + 4.8 -> round -> integer: {}",
105        calculator.apply(3.7, 4.8)
106    );
107    println!();
108
109    // 8. Error handling example
110    println!("8. Error handling example");
111    let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| -> Result<i32, String> {
112        if y == 0 {
113            Err("Division by zero is not allowed".to_string())
114        } else {
115            Ok(x / y)
116        }
117    });
118
119    let format_result = |res: Result<i32, String>| match res {
120        Ok(v) => format!("Success: {}", v),
121        Err(e) => format!("Error: {}", e),
122    };
123
124    let safe_calculator = safe_divide.and_then(format_result);
125    println!("   10 / 2 = {}", safe_calculator.apply(10, 2));
126    println!("   10 / 0 = {}", safe_calculator.apply(10, 0));
127    println!();
128
129    // 9. Complex data structures
130    println!("9. Complex data structures");
131    #[derive(Debug)]
132    struct Point {
133        x: i32,
134        y: i32,
135    }
136
137    let create_point = BoxBiTransformer::new(|x: i32, y: i32| Point { x, y });
138    let distance_from_origin = |p: Point| ((p.x * p.x + p.y * p.y) as f64).sqrt();
139    let format_distance = |d: f64| format!("{:.2}", d);
140
141    let point_processor = create_point
142        .and_then(distance_from_origin)
143        .and_then(format_distance);
144    println!(
145        "   Distance from point(3, 4) to origin: {}",
146        point_processor.apply(3, 4)
147    );
148    println!();
149
150    // 10. Combined usage with when
151    println!("10. Combined usage with when");
152    let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
153    let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
154
155    let conditional = add_when
156        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
157        .or_else(multiply_when);
158
159    let double_result = |x: i32| x * 2;
160    let final_transformer = conditional.and_then(double_result);
161
162    println!(
163        "   Add positive numbers then double: (5 + 3) * 2 = {}",
164        final_transformer.apply(5, 3)
165    );
166    println!(
167        "   Multiply negative numbers then double: (-5 * 3) * 2 = {}",
168        final_transformer.apply(-5, 3)
169    );
170
171    println!("\n=== Demo completed ===");
172}
Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: Fn(T, U) -> R + Send + Sync + 'static,

Creates a new named bi-transformer.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn(T, U) -> R + Send + Sync + 'static,

Creates a new named bi-transformer with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this bi-transformer.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this bi-transformer.

§Parameters
  • name - The name to set for this bi-transformer
Source

pub fn clear_name(&mut self)

Clears the name of this bi-transformer.

Source

pub fn when<P>(&self, predicate: P) -> ArcConditionalBiTransformer<T, U, R>
where T: 'static, U: 'static, R: 'static, P: BiPredicate<T, U> + Send + Sync + 'static,

Source

pub fn and_then<S, F>(&self, after: F) -> ArcBiTransformer<T, U, S>
where T: 'static, U: 'static, R: 'static, S: 'static, F: Transformer<R, S> + Send + Sync + 'static,

Examples found in repository?
examples/transformers/bi_transformer_and_then_demo.rs (line 48)
24fn main() {
25    println!("=== BiTransformer and_then Method Demo ===\n");
26
27    // 1. BoxBiTransformer::and_then - Basic usage
28    println!("1. BoxBiTransformer::and_then - Basic usage");
29    let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
30    let double = |x: i32| x * 2;
31    let composed = add.and_then(double);
32    println!("   (3 + 5) * 2 = {}", composed.apply(3, 5));
33    println!();
34
35    // 2. BoxBiTransformer::and_then - Chained calls
36    println!("2. BoxBiTransformer::and_then - Chained calls");
37    let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
38    let add_ten = |x: i32| x + 10;
39    let to_string = |x: i32| format!("Result: {}", x);
40    let pipeline = multiply.and_then(add_ten).and_then(to_string);
41    println!("   (6 * 7) + 10 = {}", pipeline.apply(6, 7));
42    println!();
43
44    // 3. ArcBiTransformer::and_then - Shared ownership
45    println!("3. ArcBiTransformer::and_then - Shared ownership");
46    let add_arc = ArcBiTransformer::new(|x: i32, y: i32| x + y);
47    let triple = |x: i32| x * 3;
48    let composed_arc = add_arc.and_then(triple);
49
50    // Original bi-transformer is still available
51    println!("   Original: 20 + 22 = {}", add_arc.apply(20, 22));
52    println!("   Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
53    println!();
54
55    // 4. ArcBiTransformer::and_then - Cloneable
56    println!("4. ArcBiTransformer::and_then - Cloneable");
57    let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
58    let abs = |x: i32| x.abs();
59    let composed_abs = subtract.and_then(abs);
60    let cloned = composed_abs.clone();
61
62    println!("   Original: |10 - 15| = {}", composed_abs.apply(10, 15));
63    println!("   Cloned: |15 - 10| = {}", cloned.apply(15, 10));
64    println!();
65
66    // 5. RcBiTransformer::and_then - Single-threaded sharing
67    println!("5. RcBiTransformer::and_then - Single-threaded sharing");
68    let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
69    let square = |x: i32| x * x;
70    let composed_rc = divide.and_then(square);
71
72    println!("   Original: 20 / 4 = {}", divide.apply(20, 4));
73    println!("   Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
74    println!();
75
76    // 6. Type conversion example
77    println!("6. Type conversion example");
78    let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
79    let to_uppercase = |s: String| s.to_uppercase();
80    let get_length = |s: String| s.len();
81
82    let uppercase_pipeline = concat.and_then(to_uppercase);
83    println!(
84        "   \"hello\" + \"world\" -> uppercase: {}",
85        uppercase_pipeline.apply("hello".to_string(), "world".to_string())
86    );
87
88    let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
89    let length_pipeline = concat2.and_then(get_length);
90    println!(
91        "   \"hello\" + \"world\" -> length: {}",
92        length_pipeline.apply("hello".to_string(), "world".to_string())
93    );
94    println!();
95
96    // 7. Real application: Calculator
97    println!("7. Real application: Calculator");
98    let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
99    let round = |x: f64| x.round();
100    let to_int = |x: f64| x as i32;
101
102    let calculator = calculate.and_then(round).and_then(to_int);
103    println!(
104        "   3.7 + 4.8 -> round -> integer: {}",
105        calculator.apply(3.7, 4.8)
106    );
107    println!();
108
109    // 8. Error handling example
110    println!("8. Error handling example");
111    let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| -> Result<i32, String> {
112        if y == 0 {
113            Err("Division by zero is not allowed".to_string())
114        } else {
115            Ok(x / y)
116        }
117    });
118
119    let format_result = |res: Result<i32, String>| match res {
120        Ok(v) => format!("Success: {}", v),
121        Err(e) => format!("Error: {}", e),
122    };
123
124    let safe_calculator = safe_divide.and_then(format_result);
125    println!("   10 / 2 = {}", safe_calculator.apply(10, 2));
126    println!("   10 / 0 = {}", safe_calculator.apply(10, 0));
127    println!();
128
129    // 9. Complex data structures
130    println!("9. Complex data structures");
131    #[derive(Debug)]
132    struct Point {
133        x: i32,
134        y: i32,
135    }
136
137    let create_point = BoxBiTransformer::new(|x: i32, y: i32| Point { x, y });
138    let distance_from_origin = |p: Point| ((p.x * p.x + p.y * p.y) as f64).sqrt();
139    let format_distance = |d: f64| format!("{:.2}", d);
140
141    let point_processor = create_point
142        .and_then(distance_from_origin)
143        .and_then(format_distance);
144    println!(
145        "   Distance from point(3, 4) to origin: {}",
146        point_processor.apply(3, 4)
147    );
148    println!();
149
150    // 10. Combined usage with when
151    println!("10. Combined usage with when");
152    let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
153    let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
154
155    let conditional = add_when
156        .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
157        .or_else(multiply_when);
158
159    let double_result = |x: i32| x * 2;
160    let final_transformer = conditional.and_then(double_result);
161
162    println!(
163        "   Add positive numbers then double: (5 + 3) * 2 = {}",
164        final_transformer.apply(5, 3)
165    );
166    println!(
167        "   Multiply negative numbers then double: (-5 * 3) * 2 = {}",
168        final_transformer.apply(-5, 3)
169    );
170
171    println!("\n=== Demo completed ===");
172}
Source§

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

Source

pub fn constant(value: R) -> ArcBiTransformer<T, U, R>
where R: Clone + Send + Sync + 'static,

Creates a constant bi-transformer

§Examples

/// rust /// use qubit_function::{ArcBiTransformer, BiTransformer}; /// /// let constant = ArcBiTransformer::constant("hello"); /// assert_eq!(constant.apply(123, 456), "hello"); ///

Trait Implementations§

Source§

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

Source§

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

Transforms two input values to produce an output value Read more
Source§

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

Converts to BoxBiTransformer Read more
Source§

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

Converts to RcBiTransformer Read more
Source§

fn into_arc(self) -> ArcBiTransformer<T, U, R>

Converts to ArcBiTransformer Read more
Source§

fn into_fn(self) -> impl Fn(T, U) -> R

Converts bi-transformer to a closure Read more
Source§

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

Convert to BiTransformerOnce Read more
Source§

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

Non-consuming conversion to BoxBiTransformer using &self. Read more
Source§

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

Non-consuming conversion to RcBiTransformer using &self. Read more
Source§

fn to_arc(&self) -> ArcBiTransformer<T, U, R>

Non-consuming conversion to ArcBiTransformer using &self. Read more
Source§

fn to_fn(&self) -> impl Fn(T, U) -> R

Non-consuming conversion to a boxed function using &self. Read more
Source§

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

Convert to BiTransformerOnce without consuming self Read more
Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<T, U, R> !RefUnwindSafe for ArcBiTransformer<T, U, R>

§

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

§

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

§

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

§

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

§

impl<T, U, R> !UnwindSafe for ArcBiTransformer<T, U, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<F, T> BinaryOperator<T> for F
where F: BiTransformer<T, T, T>,