Skip to main content

BoxTransformerOnce

Struct BoxTransformerOnce 

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

BoxTransformerOnce - consuming transformer wrapper based on Box<dyn FnOnce>

A transformer wrapper that provides single ownership with one-time use semantics. Consumes both self and the input value.

§Features

  • Based on: Box<dyn FnOnce(T) -> R>
  • Ownership: Single ownership, cannot be cloned
  • Reusability: Can only be called once (consumes self and input)
  • Thread Safety: Not thread-safe (no Send + Sync requirement)

Implementations§

Source§

impl<T, R> BoxTransformerOnce<T, R>

Source

pub fn new<F>(f: F) -> Self
where F: FnOnce(T) -> R + 'static,

Creates a new transformer.

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

Source

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

Creates a new named 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: FnOnce(T) -> R + 'static,

Creates a new named 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 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 transformer.

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

pub fn clear_name(&mut self)

Clears the name of this transformer.

Source

pub fn identity() -> BoxTransformerOnce<T, T>

Creates an identity transformer.

Creates a transformer that returns the input value unchanged. Useful for default values or placeholder implementations.

§Returns

Returns a new transformer instance that returns the input unchanged.

Source

pub fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>
where T: 'static, R: 'static, P: Predicate<T> + 'static,

Creates a conditional transformer that executes based on predicate result.

§Parameters
  • predicate - The predicate to determine whether to execute the transformation operation
§Returns

Returns a conditional transformer that only executes when the predicate returns true.

§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::transformers::*;

let transformer = BoxTransformer::new({
    |value: i32| value * 2
});

let conditional = transformer.when(|value: &i32| *value > 0).or_else(|value: i32| value);
assert_eq!(conditional.apply(5), 10);  // transformed
assert_eq!(conditional.apply(-1), -1); // identity (unchanged)
Source

pub fn and_then<S, F>(self, after: F) -> BoxTransformerOnce<T, S>
where T: 'static, R: 'static, S: 'static, F: TransformerOnce<R, S> + 'static,

Chains execution with another transformer, executing the current transformer first, then the subsequent transformer.

§Parameters
  • after - The subsequent transformer to execute after the current transformer completes
§Returns

Returns a new transformer that executes the current transformer and the subsequent transformer in sequence.

§Examples
use qubit_function::transformers::*;

let transformer1 = BoxTransformer::new({
    |value: i32| value + 1
});

let transformer2 = BoxTransformer::new({
    |value: i32| value * 2
});

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

impl<T, R> BoxTransformerOnce<T, R>

Source

pub fn constant(value: R) -> BoxTransformerOnce<T, R>
where R: Clone + 'static,

Creates a constant transformer

§Examples

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

Trait Implementations§

Source§

impl<T, R> Debug for BoxTransformerOnce<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 BoxTransformerOnce<T, R>

Source§

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

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

impl<T, R> TransformerOnce<T, R> for BoxTransformerOnce<T, R>

Source§

fn apply(self, input: T) -> R

Transforms the input value, consuming both self and input Read more
Source§

fn into_box(self) -> BoxTransformerOnce<T, R>

Converts to BoxTransformerOnce Read more
Source§

fn into_fn(self) -> impl FnOnce(T) -> R

Converts transformer to a closure Read more

Auto Trait Implementations§

§

impl<T, R> Freeze for BoxTransformerOnce<T, R>

§

impl<T, R> !RefUnwindSafe for BoxTransformerOnce<T, R>

§

impl<T, R> !Send for BoxTransformerOnce<T, R>

§

impl<T, R> !Sync for BoxTransformerOnce<T, R>

§

impl<T, R> Unpin for BoxTransformerOnce<T, R>

§

impl<T, R> UnsafeUnpin for BoxTransformerOnce<T, R>

§

impl<T, R> !UnwindSafe for BoxTransformerOnce<T, 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> 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.
Source§

impl<F, T> UnaryOperatorOnce<T> for F
where F: TransformerOnce<T, T>,