Skip to main content

BoxConditionalTransformerOnce

Struct BoxConditionalTransformerOnce 

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

BoxConditionalTransformerOnce struct

A conditional consuming transformer that only executes when a predicate is satisfied. Uses BoxTransformerOnce and BoxPredicate for single ownership semantics.

This type is typically created by calling BoxTransformerOnce::when() and is designed to work with the or_else() method to create if-then-else logic.

§Features

  • Single Ownership: Not cloneable, consumes self on use
  • One-time Use: Can only be called once
  • Conditional Execution: Only transforms when predicate returns true
  • Chainable: Can add or_else branch to create if-then-else logic

§Examples

§With or_else Branch

use qubit_function::{TransformerOnce, BoxTransformerOnce};

let double = BoxTransformerOnce::new(|x: i32| x * 2);
let negate = BoxTransformerOnce::new(|x: i32| -x);
let conditional = double.when(|x: &i32| *x > 0).or_else(negate);
assert_eq!(conditional.apply(5), 10); // when branch executed

let double2 = BoxTransformerOnce::new(|x: i32| x * 2);
let negate2 = BoxTransformerOnce::new(|x: i32| -x);
let conditional2 = double2.when(|x: &i32| *x > 0).or_else(negate2);
assert_eq!(conditional2.apply(-5), 5); // or_else branch executed

§Author

Haixing Hu

Implementations§

Source§

impl<T, R> BoxConditionalTransformerOnce<T, R>

Source

pub fn or_else<F>(self, else_transformer: F) -> BoxTransformerOnce<T, R>
where T: 'static, R: 'static, F: TransformerOnce<T, 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 transformer with if-then-else logic

Examples found in repository?
examples/transformers/fn_transformer_once_ops_demo.rs (line 59)
19fn main() {
20    println!("=== FnTransformerOnceOps Example ===\n");
21
22    // 1. Basic and_then composition
23    println!("1. Basic and_then composition:");
24    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
25    let double = |x: i32| x * 2;
26    let composed = parse.and_then(double);
27    println!(
28        "   parse.and_then(double).apply(\"21\") = {}",
29        composed.apply("21".to_string())
30    );
31    println!();
32
33    // 2. Chained and_then composition
34    println!("2. Chained and_then composition:");
35    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
36    let add_one = |x: i32| x + 1;
37    let double = |x: i32| x * 2;
38    let chained = parse.and_then(add_one).and_then(double);
39    println!(
40        "   parse.and_then(add_one).and_then(double).apply(\"5\") = {}",
41        chained.apply("5".to_string())
42    ); // (5 + 1) * 2 = 12
43    println!();
44
45    // 3. More and_then composition
46    println!("3. More and_then composition:");
47    let double = |x: i32| x * 2;
48    let to_string = |x: i32| x.to_string();
49    let composed = double.and_then(to_string);
50    println!(
51        "   double.and_then(to_string).apply(21) = {}",
52        composed.apply(21)
53    ); // (21 * 2).to_string() = "42"
54    println!();
55
56    // 4. Conditional transformation when
57    println!("4. Conditional transformation when:");
58    let double = |x: i32| x * 2;
59    let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
60    println!("   double.when(x > 0).or_else(negate):");
61    println!("     transform(5) = {}", conditional.apply(5)); // 10
62
63    let double2 = |x: i32| x * 2;
64    let conditional2 = double2.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
65    println!("     transform(-5) = {}", conditional2.apply(-5)); // 5
66    println!();
67
68    // 5. Complex composition
69    println!("5. Complex composition:");
70    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
71    let double = |x: i32| x * 2;
72    let triple = |x: i32| x * 3;
73    let to_string = |x: i32| x.to_string();
74
75    let complex = parse
76        .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
77        .and_then(to_string);
78
79    println!("   parse.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
80    println!("     transform(\"3\") = {}", complex.apply("3".to_string())); // 3 <= 5, so 3 * 3 = 9
81
82    let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
83    let double2 = |x: i32| x * 2;
84    let triple2 = |x: i32| x * 3;
85    let to_string2 = |x: i32| x.to_string();
86    let complex2 = parse2
87        .and_then(double2.when(|x: &i32| *x > 5).or_else(triple2))
88        .and_then(to_string2);
89    println!(
90        "     transform(\"10\") = {}",
91        complex2.apply("10".to_string())
92    ); // 10 > 5, so 10 * 2 = 20
93    println!();
94
95    // 6. Type conversion
96    println!("6. Type conversion:");
97    let to_string = |x: i32| x.to_string();
98    let get_length = |s: String| s.len();
99    let length_transformer = to_string.and_then(get_length);
100    println!(
101        "   to_string.and_then(get_length).apply(12345) = {}",
102        length_transformer.apply(12345)
103    ); // 5
104    println!();
105
106    // 7. Closures that capture environment
107    println!("7. Closures that capture environment:");
108    let multiplier = 3;
109    let multiply = move |x: i32| x * multiplier;
110    let add_ten = |x: i32| x + 10;
111    let with_capture = multiply.and_then(add_ten);
112    println!(
113        "   multiply(3).and_then(add_ten).apply(5) = {}",
114        with_capture.apply(5)
115    ); // 5 * 3 + 10 = 25
116    println!();
117
118    // 8. Function pointers
119    println!("8. Function pointers:");
120    fn parse_fn(s: String) -> i32 {
121        s.parse().unwrap_or(0)
122    }
123    fn double_fn(x: i32) -> i32 {
124        x * 2
125    }
126    let fn_composed = parse_fn.and_then(double_fn);
127    println!(
128        "   parse_fn.and_then(double_fn).apply(\"21\") = {}",
129        fn_composed.apply("21".to_string())
130    ); // 42
131    println!();
132
133    // 9. String operations that consume ownership
134    println!("9. String operations that consume ownership:");
135    let owned = String::from("hello");
136    let append = move |s: String| format!("{} {}", s, owned);
137    let uppercase = |s: String| s.to_uppercase();
138    let composed = append.and_then(uppercase);
139    println!(
140        "   append.and_then(uppercase).apply(\"world\") = {}",
141        composed.apply("world".to_string())
142    ); // "WORLD HELLO"
143    println!();
144
145    // 10. Parsing and validation
146    println!("10. Parsing and validation:");
147    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
148    let validate = |x: i32| if x > 0 { x } else { 1 };
149    let composed = parse.and_then(validate);
150    println!(
151        "   parse.and_then(validate).apply(\"42\") = {}",
152        composed.apply("42".to_string())
153    ); // 42
154
155    let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
156    let validate2 = |x: i32| if x > 0 { x } else { 1 };
157    let composed2 = parse2.and_then(validate2);
158    println!(
159        "   parse.and_then(validate).apply(\"-5\") = {}",
160        composed2.apply("-5".to_string())
161    ); // 1
162    println!();
163
164    println!("=== Example completed ===");
165}

Trait Implementations§

Source§

impl<T, R> Debug for BoxConditionalTransformerOnce<T, R>

Source§

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

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

impl<T, R> Display for BoxConditionalTransformerOnce<T, R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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> 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.