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)

§Author

Hu Haixing

Implementations§

Source§

impl<T, R> BoxTransformerOnce<T, R>
where T: 'static, R: 'static,

Source

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

Creates a new BoxTransformerOnce

§Parameters
  • f - The closure or function to wrap
§Examples
use prism3_function::{BoxTransformerOnce, TransformerOnce};

let parse = BoxTransformerOnce::new(|s: String| {
    s.parse::<i32>().unwrap_or(0)
});

assert_eq!(parse.apply_once("42".to_string()), 42);
Source

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

Creates an identity transformer

§Examples
use prism3_function::{BoxTransformerOnce, TransformerOnce};

let identity = BoxTransformerOnce::<i32, i32>::identity();
assert_eq!(identity.apply_once(42), 42);
Source

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

Chain composition - applies self first, then after

§Type Parameters
  • S - The output type of the after transformer
  • G - The type of the after transformer (must implement TransformerOnce<R, S>)
§Parameters
  • after - The transformer to apply after self. Note: This parameter is passed by value and will transfer ownership. Since BoxTransformerOnce cannot be cloned, the parameter will be consumed. Can be:
    • A closure: |x: R| -> S
    • A function pointer: fn(R) -> S
    • A BoxTransformerOnce<R, S>
    • Any type implementing TransformerOnce<R, S>
§Returns

A new BoxTransformerOnce representing the composition

§Examples
use prism3_function::{BoxTransformerOnce, TransformerOnce};

let add_one = BoxTransformerOnce::new(|x: i32| x + 1);
let double = BoxTransformerOnce::new(|x: i32| x * 2);

// Both add_one and double are moved and consumed
let composed = add_one.and_then(double);
assert_eq!(composed.apply_once(5), 12); // (5 + 1) * 2
// add_one.apply_once(3); // Would not compile - moved
// double.apply_once(4);  // Would not compile - moved
Examples found in repository?
examples/fn_transformer_once_ops_demo.rs (line 35)
16fn main() {
17    println!("=== FnTransformerOnceOps Example ===\n");
18
19    // 1. Basic and_then composition
20    println!("1. Basic and_then composition:");
21    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
22    let double = |x: i32| x * 2;
23    let composed = parse.and_then(double);
24    println!(
25        "   parse.and_then(double).apply_once(\"21\") = {}",
26        composed.apply_once("21".to_string())
27    );
28    println!();
29
30    // 2. Chained and_then composition
31    println!("2. Chained and_then composition:");
32    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
33    let add_one = |x: i32| x + 1;
34    let double = |x: i32| x * 2;
35    let chained = parse.and_then(add_one).and_then(double);
36    println!(
37        "   parse.and_then(add_one).and_then(double).apply_once(\"5\") = {}",
38        chained.apply_once("5".to_string())
39    ); // (5 + 1) * 2 = 12
40    println!();
41
42    // 3. compose reverse composition
43    println!("3. compose reverse composition:");
44    let double = |x: i32| x * 2;
45    let to_string = |x: i32| x.to_string();
46    let composed = to_string.compose(double);
47    println!(
48        "   to_string.compose(double).apply_once(21) = {}",
49        composed.apply_once(21)
50    ); // (21 * 2).to_string() = "42"
51    println!();
52
53    // 4. Conditional transformation when
54    println!("4. Conditional transformation when:");
55    let double = |x: i32| x * 2;
56    let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
57    println!("   double.when(x > 0).or_else(negate):");
58    println!("     transform(5) = {}", conditional.apply_once(5)); // 10
59
60    let double2 = |x: i32| x * 2;
61    let conditional2 = double2.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
62    println!("     transform(-5) = {}", conditional2.apply_once(-5)); // 5
63    println!();
64
65    // 5. Complex composition
66    println!("5. Complex composition:");
67    let parse = |s: String| s.parse::<i32>().unwrap_or(0);
68    let double = |x: i32| x * 2;
69    let triple = |x: i32| x * 3;
70    let to_string = |x: i32| x.to_string();
71
72    let complex = parse
73        .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
74        .and_then(to_string);
75
76    println!("   parse.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
77    println!(
78        "     transform(\"3\") = {}",
79        complex.apply_once("3".to_string())
80    ); // 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_once("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_once(12345) = {}",
102        length_transformer.apply_once(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_once(5) = {}",
114        with_capture.apply_once(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_once(\"21\") = {}",
129        fn_composed.apply_once("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_once(\"world\") = {}",
141        composed.apply_once("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_once(\"42\") = {}",
152        composed.apply_once("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_once(\"-5\") = {}",
160        composed2.apply_once("-5".to_string())
161    ); // 1
162    println!();
163
164    println!("=== Example completed ===");
165}
Source

pub fn compose<S, G>(self, before: G) -> BoxTransformerOnce<S, R>
where S: 'static, G: TransformerOnce<S, T> + 'static,

Reverse composition - applies before first, then self

§Type Parameters
  • S - The input type of the before transformer
  • G - The type of the before transformer (must implement TransformerOnce<S, T>)
§Parameters
  • before - The transformer to apply before self. Note: This parameter is passed by value and will transfer ownership. Since BoxTransformerOnce cannot be cloned, the parameter will be consumed. Can be:
    • A closure: |x: S| -> T
    • A function pointer: fn(S) -> T
    • A BoxTransformerOnce<S, T>
    • Any type implementing TransformerOnce<S, T>
§Returns

A new BoxTransformerOnce representing the composition

§Examples
use prism3_function::{BoxTransformerOnce, TransformerOnce};

let double = BoxTransformerOnce::new(|x: i32| x * 2);
let add_one = BoxTransformerOnce::new(|x: i32| x + 1);

// Both double and add_one are moved and consumed
let composed = double.compose(add_one);
assert_eq!(composed.apply_once(5), 12); // (5 + 1) * 2
// double.apply_once(3); // Would not compile - moved
// add_one.apply_once(4); // Would not compile - moved
Source

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

Creates a conditional transformer

Returns a transformer that only executes when a predicate is satisfied. You must call or_else() to provide an alternative transformer.

§Parameters
  • predicate - The condition to check. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implements Clone). Can be:
    • A closure: |x: &T| -> bool
    • A function pointer: fn(&T) -> bool
    • A BoxPredicate<T>
    • An RcPredicate<T>
    • An ArcPredicate<T>
    • Any type implementing Predicate<T>
§Returns

Returns BoxConditionalTransformerOnce<T, R>

§Examples
§Basic usage with or_else
use prism3_function::{TransformerOnce, BoxTransformerOnce};

let double = BoxTransformerOnce::new(|x: i32| x * 2);
let identity = BoxTransformerOnce::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
assert_eq!(conditional.apply_once(5), 10);

let double2 = BoxTransformerOnce::new(|x: i32| x * 2);
let identity2 = BoxTransformerOnce::<i32, i32>::identity();
let conditional2 = double2.when(|x: &i32| *x > 0).or_else(identity2);
assert_eq!(conditional2.apply_once(-5), -5);
§Preserving predicate with clone
use prism3_function::{TransformerOnce, BoxTransformerOnce, RcPredicate};

let double = BoxTransformerOnce::new(|x: i32| x * 2);
let is_positive = RcPredicate::new(|x: &i32| *x > 0);

// Clone to preserve original predicate
let conditional = double.when(is_positive.clone())
    .or_else(BoxTransformerOnce::identity());

assert_eq!(conditional.apply_once(5), 10);

// Original predicate still usable
assert!(is_positive.test(&3));
Source§

impl<T, R> BoxTransformerOnce<T, R>
where T: 'static, R: Clone + 'static,

Source

pub fn constant(value: R) -> BoxTransformerOnce<T, R>

Creates a constant transformer

§Examples
use prism3_function::{BoxTransformerOnce, TransformerOnce};

let constant = BoxTransformerOnce::constant("hello");
assert_eq!(constant.apply_once(123), "hello");

Trait Implementations§

Source§

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

Source§

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

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

fn into_box_once(self) -> BoxTransformerOnce<T, R>
where T: 'static, R: 'static,

Converts to BoxTransformerOnce Read more
Source§

fn into_fn_once(self) -> impl FnOnce(T) -> R
where T: 'static, R: 'static,

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> !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, 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>, T: 'static,