Skip to main content

FnTransformerOps

Trait FnTransformerOps 

Source
pub trait FnTransformerOps<T, R>: Fn(T) -> R + Sized {
    // Provided methods
    fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>
       where Self: 'static,
             S: 'static,
             F: Transformer<R, S> + 'static,
             T: 'static,
             R: 'static { ... }
    fn compose<S, F>(self, before: F) -> BoxTransformer<S, R>
       where Self: 'static,
             S: 'static,
             F: Transformer<S, T> + 'static,
             T: 'static,
             R: 'static { ... }
    fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>
       where Self: 'static,
             P: Predicate<T> + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

Extension trait for closures implementing Fn(T) -> R

Provides composition methods (and_then, compose, when) for closures and function pointers without requiring explicit wrapping in BoxTransformer, RcTransformer, or ArcTransformer.

This trait is automatically implemented for all closures and function pointers that implement Fn(T) -> R.

§Design Rationale

While closures automatically implement Transformer<T, R> through blanket implementation, they don’t have access to instance methods like and_then, compose, and when. This extension trait provides those methods, returning BoxTransformer for maximum flexibility.

§Examples

§Chain composition with and_then

use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let to_string = |x: i32| x.to_string();

let composed = double.and_then(to_string);
assert_eq!(composed.apply(21), "42");

§Reverse composition with compose

use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let add_one = |x: i32| x + 1;

let composed = double.compose(add_one);
assert_eq!(composed.apply(5), 12); // (5 + 1) * 2

§Conditional transformation with when

use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);

assert_eq!(conditional.apply(5), 10);
assert_eq!(conditional.apply(-5), 5);

§Author

Haixing Hu

Provided Methods§

Source

fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>
where Self: 'static, S: 'static, F: Transformer<R, S> + 'static, T: 'static, R: 'static,

Chain composition - applies self first, then after

Creates a new transformer that applies this transformer first, then applies the after transformer to the result. Consumes self and returns a BoxTransformer.

§Type Parameters
  • S - The output type of the after transformer
  • F - The type of the after transformer (must implement Transformer<R, S>)
§Parameters
  • after - The transformer to apply after self. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original transformer, clone it first (if it implements Clone). Can be:
    • A closure: |x: R| -> S
    • A function pointer: fn(R) -> S
    • A BoxTransformer<R, S>
    • An RcTransformer<R, S>
    • An ArcTransformer<R, S>
    • Any type implementing Transformer<R, S>
§Returns

A new BoxTransformer<T, S> representing the composition

§Examples
§Direct value passing (ownership transfer)
use qubit_function::{Transformer, FnTransformerOps, BoxTransformer};

let double = |x: i32| x * 2;
let to_string = BoxTransformer::new(|x: i32| x.to_string());

// to_string is moved here
let composed = double.and_then(to_string);
assert_eq!(composed.apply(21), "42");
// to_string.apply(5); // Would not compile - moved
§Preserving original with separate closures
use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let to_string = |x: i32| x.to_string();
let to_string_for_validation = |x: i32| x.to_string();

let composed = double.and_then(to_string);
assert_eq!(composed.apply(21), "42");

// Original still usable
assert_eq!(to_string_for_validation(5), "5");
Examples found in repository?
examples/transformers/fn_transformer_ops_demo.rs (line 26)
19fn main() {
20    println!("=== FnTransformerOps Example ===\n");
21
22    // 1. Basic and_then composition
23    println!("1. Basic and_then composition:");
24    let double = |x: i32| x * 2;
25    let to_string = |x: i32| x.to_string();
26    let composed = double.and_then(to_string);
27    println!(
28        "   double.and_then(to_string).apply(21) = {}",
29        composed.apply(21)
30    );
31    println!();
32
33    // 2. Chained and_then composition
34    println!("2. Chained and_then composition:");
35    let add_one = |x: i32| x + 1;
36    let double = |x: i32| x * 2;
37    let to_string = |x: i32| x.to_string();
38    let chained = add_one.and_then(double).and_then(to_string);
39    println!(
40        "   add_one.and_then(double).and_then(to_string).apply(5) = {}",
41        chained.apply(5)
42    ); // (5 + 1) * 2 = 12
43    println!();
44
45    // 3. compose reverse composition
46    println!("3. compose reverse composition:");
47    let double = |x: i32| x * 2;
48    let add_one = |x: i32| x + 1;
49    let composed = double.compose(add_one);
50    println!(
51        "   double.compose(add_one).apply(5) = {}",
52        composed.apply(5)
53    ); // (5 + 1) * 2 = 12
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    println!("     transform(-5) = {}", conditional.apply(-5)); // 5
63    println!();
64
65    // 5. Complex composition
66    println!("5. Complex composition:");
67    let add_one = |x: i32| x + 1;
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 = add_one
73        .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
74        .and_then(to_string);
75
76    println!("   add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
77    println!("     transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
78    println!("     transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
79    println!("     transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
80    println!();
81
82    // 6. Type conversion
83    println!("6. Type conversion:");
84    let to_string = |x: i32| x.to_string();
85    let get_length = |s: String| s.len();
86    let length_transformer = to_string.and_then(get_length);
87    println!(
88        "   to_string.and_then(get_length).apply(12345) = {}",
89        length_transformer.apply(12345)
90    ); // 5
91    println!();
92
93    // 7. Closures that capture environment
94    println!("7. Closures that capture environment:");
95    let multiplier = 3;
96    let multiply = move |x: i32| x * multiplier;
97    let add_ten = |x: i32| x + 10;
98    let with_capture = multiply.and_then(add_ten);
99    println!(
100        "   multiply(3).and_then(add_ten).apply(5) = {}",
101        with_capture.apply(5)
102    ); // 5 * 3 + 10 = 25
103    println!();
104
105    // 8. Function pointers
106    println!("8. Function pointers:");
107    fn double_fn(x: i32) -> i32 {
108        x * 2
109    }
110    fn add_one_fn(x: i32) -> i32 {
111        x + 1
112    }
113    let fn_composed = double_fn.and_then(add_one_fn);
114    println!(
115        "   double_fn.and_then(add_one_fn).apply(5) = {}",
116        fn_composed.apply(5)
117    ); // 5 * 2 + 1 = 11
118    println!();
119
120    // 9. Multi-conditional transformation
121    println!("9. Multi-conditional transformation:");
122    let abs = |x: i32| x.abs();
123    let double = |x: i32| x * 2;
124    let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
125    println!("   abs.when(x < 0).or_else(double):");
126    println!("     transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
127    println!("     transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
128    println!("     transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
129    println!();
130
131    println!("=== Example completed ===");
132}
Source

fn compose<S, F>(self, before: F) -> BoxTransformer<S, R>
where Self: 'static, S: 'static, F: Transformer<S, T> + 'static, T: 'static, R: 'static,

Reverse composition - applies before first, then self

Creates a new transformer that applies the before transformer first, then applies this transformer to the result. Consumes self and returns a BoxTransformer.

§Type Parameters
  • S - The input type of the before transformer
  • F - The type of the before transformer (must implement Transformer<S, T>)
§Parameters
  • before - The transformer to apply before self. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original transformer, clone it first (if it implements Clone). Can be:
    • A closure: |x: S| -> T
    • A function pointer: fn(S) -> T
    • A BoxTransformer<S, T>
    • An RcTransformer<S, T>
    • An ArcTransformer<S, T>
    • Any type implementing Transformer<S, T>
§Returns

A new BoxTransformer<S, R> representing the composition

§Examples
§Direct value passing (ownership transfer)
use qubit_function::{Transformer, FnTransformerOps, BoxTransformer};

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

// add_one is moved here
let composed = double.compose(add_one);
assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
// add_one.apply(3); // Would not compile - moved
§Preserving original with separate closures
use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let add_one = |x: i32| x + 1;
let add_one_for_validation = |x: i32| x + 1;

let composed = double.compose(add_one);
assert_eq!(composed.apply(5), 12); // (5 + 1) * 2

// Original still usable
assert_eq!(add_one_for_validation(3), 4);
Examples found in repository?
examples/transformers/fn_transformer_ops_demo.rs (line 49)
19fn main() {
20    println!("=== FnTransformerOps Example ===\n");
21
22    // 1. Basic and_then composition
23    println!("1. Basic and_then composition:");
24    let double = |x: i32| x * 2;
25    let to_string = |x: i32| x.to_string();
26    let composed = double.and_then(to_string);
27    println!(
28        "   double.and_then(to_string).apply(21) = {}",
29        composed.apply(21)
30    );
31    println!();
32
33    // 2. Chained and_then composition
34    println!("2. Chained and_then composition:");
35    let add_one = |x: i32| x + 1;
36    let double = |x: i32| x * 2;
37    let to_string = |x: i32| x.to_string();
38    let chained = add_one.and_then(double).and_then(to_string);
39    println!(
40        "   add_one.and_then(double).and_then(to_string).apply(5) = {}",
41        chained.apply(5)
42    ); // (5 + 1) * 2 = 12
43    println!();
44
45    // 3. compose reverse composition
46    println!("3. compose reverse composition:");
47    let double = |x: i32| x * 2;
48    let add_one = |x: i32| x + 1;
49    let composed = double.compose(add_one);
50    println!(
51        "   double.compose(add_one).apply(5) = {}",
52        composed.apply(5)
53    ); // (5 + 1) * 2 = 12
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    println!("     transform(-5) = {}", conditional.apply(-5)); // 5
63    println!();
64
65    // 5. Complex composition
66    println!("5. Complex composition:");
67    let add_one = |x: i32| x + 1;
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 = add_one
73        .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
74        .and_then(to_string);
75
76    println!("   add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
77    println!("     transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
78    println!("     transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
79    println!("     transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
80    println!();
81
82    // 6. Type conversion
83    println!("6. Type conversion:");
84    let to_string = |x: i32| x.to_string();
85    let get_length = |s: String| s.len();
86    let length_transformer = to_string.and_then(get_length);
87    println!(
88        "   to_string.and_then(get_length).apply(12345) = {}",
89        length_transformer.apply(12345)
90    ); // 5
91    println!();
92
93    // 7. Closures that capture environment
94    println!("7. Closures that capture environment:");
95    let multiplier = 3;
96    let multiply = move |x: i32| x * multiplier;
97    let add_ten = |x: i32| x + 10;
98    let with_capture = multiply.and_then(add_ten);
99    println!(
100        "   multiply(3).and_then(add_ten).apply(5) = {}",
101        with_capture.apply(5)
102    ); // 5 * 3 + 10 = 25
103    println!();
104
105    // 8. Function pointers
106    println!("8. Function pointers:");
107    fn double_fn(x: i32) -> i32 {
108        x * 2
109    }
110    fn add_one_fn(x: i32) -> i32 {
111        x + 1
112    }
113    let fn_composed = double_fn.and_then(add_one_fn);
114    println!(
115        "   double_fn.and_then(add_one_fn).apply(5) = {}",
116        fn_composed.apply(5)
117    ); // 5 * 2 + 1 = 11
118    println!();
119
120    // 9. Multi-conditional transformation
121    println!("9. Multi-conditional transformation:");
122    let abs = |x: i32| x.abs();
123    let double = |x: i32| x * 2;
124    let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
125    println!("   abs.when(x < 0).or_else(double):");
126    println!("     transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
127    println!("     transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
128    println!("     transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
129    println!();
130
131    println!("=== Example completed ===");
132}
Source

fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>
where Self: 'static, P: Predicate<T> + 'static, T: 'static, R: '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 for when the condition is not satisfied.

§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 BoxConditionalTransformer<T, R>

§Examples
§Basic usage with or_else
use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);

assert_eq!(conditional.apply(5), 10);
assert_eq!(conditional.apply(-5), 5);
§Preserving original with separate predicates
use qubit_function::{Transformer, FnTransformerOps};

let double = |x: i32| x * 2;
let is_positive = |x: &i32| *x > 0;
let is_positive_for_validation = |x: &i32| *x > 0;
let conditional = double.when(is_positive)
    .or_else(|x: i32| -x);

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

// Original predicate still usable
assert!(is_positive_for_validation(&3));
Examples found in repository?
examples/transformers/fn_transformer_ops_demo.rs (line 59)
19fn main() {
20    println!("=== FnTransformerOps Example ===\n");
21
22    // 1. Basic and_then composition
23    println!("1. Basic and_then composition:");
24    let double = |x: i32| x * 2;
25    let to_string = |x: i32| x.to_string();
26    let composed = double.and_then(to_string);
27    println!(
28        "   double.and_then(to_string).apply(21) = {}",
29        composed.apply(21)
30    );
31    println!();
32
33    // 2. Chained and_then composition
34    println!("2. Chained and_then composition:");
35    let add_one = |x: i32| x + 1;
36    let double = |x: i32| x * 2;
37    let to_string = |x: i32| x.to_string();
38    let chained = add_one.and_then(double).and_then(to_string);
39    println!(
40        "   add_one.and_then(double).and_then(to_string).apply(5) = {}",
41        chained.apply(5)
42    ); // (5 + 1) * 2 = 12
43    println!();
44
45    // 3. compose reverse composition
46    println!("3. compose reverse composition:");
47    let double = |x: i32| x * 2;
48    let add_one = |x: i32| x + 1;
49    let composed = double.compose(add_one);
50    println!(
51        "   double.compose(add_one).apply(5) = {}",
52        composed.apply(5)
53    ); // (5 + 1) * 2 = 12
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    println!("     transform(-5) = {}", conditional.apply(-5)); // 5
63    println!();
64
65    // 5. Complex composition
66    println!("5. Complex composition:");
67    let add_one = |x: i32| x + 1;
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 = add_one
73        .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
74        .and_then(to_string);
75
76    println!("   add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
77    println!("     transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
78    println!("     transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
79    println!("     transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
80    println!();
81
82    // 6. Type conversion
83    println!("6. Type conversion:");
84    let to_string = |x: i32| x.to_string();
85    let get_length = |s: String| s.len();
86    let length_transformer = to_string.and_then(get_length);
87    println!(
88        "   to_string.and_then(get_length).apply(12345) = {}",
89        length_transformer.apply(12345)
90    ); // 5
91    println!();
92
93    // 7. Closures that capture environment
94    println!("7. Closures that capture environment:");
95    let multiplier = 3;
96    let multiply = move |x: i32| x * multiplier;
97    let add_ten = |x: i32| x + 10;
98    let with_capture = multiply.and_then(add_ten);
99    println!(
100        "   multiply(3).and_then(add_ten).apply(5) = {}",
101        with_capture.apply(5)
102    ); // 5 * 3 + 10 = 25
103    println!();
104
105    // 8. Function pointers
106    println!("8. Function pointers:");
107    fn double_fn(x: i32) -> i32 {
108        x * 2
109    }
110    fn add_one_fn(x: i32) -> i32 {
111        x + 1
112    }
113    let fn_composed = double_fn.and_then(add_one_fn);
114    println!(
115        "   double_fn.and_then(add_one_fn).apply(5) = {}",
116        fn_composed.apply(5)
117    ); // 5 * 2 + 1 = 11
118    println!();
119
120    // 9. Multi-conditional transformation
121    println!("9. Multi-conditional transformation:");
122    let abs = |x: i32| x.abs();
123    let double = |x: i32| x * 2;
124    let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
125    println!("   abs.when(x < 0).or_else(double):");
126    println!("     transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
127    println!("     transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
128    println!("     transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
129    println!();
130
131    println!("=== Example completed ===");
132}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, R, F> FnTransformerOps<T, R> for F
where F: Fn(T) -> R,

Blanket implementation of FnTransformerOps for all closures

Automatically implements FnTransformerOps<T, R> for any type that implements Fn(T) -> R.

§Author

Haixing Hu