pub struct BoxConditionalBiTransformerOnce<T, U, R> { /* private fields */ }Expand description
BoxConditionalBiTransformerOnce struct
A conditional consuming bi-transformer that only executes when a bi-predicate
is satisfied. Uses BoxBiTransformerOnce and BoxBiPredicate for single
ownership semantics.
This type is typically created by calling BoxBiTransformerOnce::when() and
is designed to work with the or_else() method to create if-then-else logic.
§Features
- Single Ownership: Not cloneable, consumes
selfon use - One-time Use: Can only be called once
- Conditional Execution: Only transforms when bi-predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
§With or_else Branch
use qubit_function::{BiTransformerOnce, BoxBiTransformerOnce};
let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply);
assert_eq!(conditional.apply(5, 3), 8); // when branch executed
let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
let conditional2 = add2.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply2);
assert_eq!(conditional2.apply(-5, 3), -15); // or_else branch executedImplementations§
Source§impl<T, U, R> BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> BoxConditionalBiTransformerOnce<T, U, R>
Sourcepub fn or_else<F>(self, else_transformer: F) -> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
F: BiTransformerOnce<T, U, R> + 'static,
pub fn or_else<F>(self, else_transformer: F) -> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
F: BiTransformerOnce<T, U, R> + 'static,
Adds an else branch
Executes the original transformer when the condition is satisfied,
otherwise executes else_transformer.
§Parameters
else_transformer- The transformer for the else branch
§Returns
Returns a new bi-transformer with if-then-else logic
Examples found in repository?
examples/transformers/bi_transformer_once_demo.rs (line 48)
17fn main() {
18 println!("=== BiTransformerOnce Examples ===\n");
19
20 // Example 1: Basic usage with closure
21 println!("1. Basic usage with closure:");
22 let add = |x: i32, y: i32| x + y;
23 let result = add.apply(20, 22);
24 println!(" 20 + 22 = {}", result);
25
26 // Example 2: BoxBiTransformerOnce with new
27 println!("\n2. BoxBiTransformerOnce with new:");
28 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
29 println!(" 6 * 7 = {}", multiply.apply(6, 7));
30
31 // Example 3: Constant transformer
32 println!("\n3. Constant transformer:");
33 let constant = BoxBiTransformerOnce::constant("hello");
34 println!(" constant(123, 456) = {}", constant.apply(123, 456));
35
36 // Example 4: Consuming owned values
37 println!("\n4. Consuming owned values:");
38 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
39 let s1 = String::from("hello");
40 let s2 = String::from("world");
41 let result = concat.apply(s1, s2);
42 println!(" concat('hello', 'world') = {}", result);
43
44 // Example 5: Conditional transformation with when/or_else
45 println!("\n5. Conditional transformation (positive numbers):");
46 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
47 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
48 let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply);
49 println!(" conditional(5, 3) = {} (add)", conditional.apply(5, 3));
50
51 println!("\n6. Conditional transformation (negative numbers):");
52 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
53 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
54 let conditional2 = add2.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply2);
55 println!(" conditional(-5, 3) = {} (multiply)", conditional2.apply(-5, 3));
56
57 // Example 7: Conditional with closure in or_else
58 println!("\n7. Conditional with closure in or_else:");
59 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
60 let conditional3 = add3
61 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
62 .or_else(|x: i32, y: i32| x * y);
63 println!(" conditional(4, 6) = {}", conditional3.apply(4, 6));
64
65 // Example 8: Merging vectors
66 println!("\n8. Merging vectors:");
67 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
68 x.extend(y);
69 x
70 });
71 let v1 = vec![1, 2, 3];
72 let v2 = vec![4, 5, 6];
73 let result = merge.apply(v1, v2);
74 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
75
76 // Example 9: Complex transformation with calculation
77 println!("\n9. Complex transformation with calculation:");
78 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
79 let sum = x + y;
80 let product = x * y;
81 (sum, product)
82 });
83 let (sum, product) = calculate.apply(5, 3);
84 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
85
86 // Example 10: String manipulation
87 println!("\n10. String manipulation:");
88 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
89 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
90 });
91 println!(
92 " process('Hello', 'WORLD') = {}",
93 process.apply("Hello".to_string(), "WORLD".to_string())
94 );
95
96 // Example 11: Converting to function
97 println!("\n11. Converting to function:");
98 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
99 let f = add4.into_fn();
100 println!(" f(10, 20) = {}", f(10, 20));
101
102 // Example 12: Converting to box (zero-cost)
103 println!("\n12. Converting to box (zero-cost):");
104 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105 let boxed = add5.into_box();
106 println!(" boxed(15, 25) = {}", boxed.apply(15, 25));
107
108 println!("\n=== All examples completed successfully! ===");
109}Trait Implementations§
Source§impl<T, U, R> Debug for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> Debug for BoxConditionalBiTransformerOnce<T, U, R>
Auto Trait Implementations§
impl<T, U, R> Freeze for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> !RefUnwindSafe for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> !Send for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> !Sync for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> Unpin for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> UnsafeUnpin for BoxConditionalBiTransformerOnce<T, U, R>
impl<T, U, R> !UnwindSafe for BoxConditionalBiTransformerOnce<T, U, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more