Skip to main content

FnTransformerOnceOps

Trait FnTransformerOnceOps 

Source
pub trait FnTransformerOnceOps<T, R>: FnOnce(T) -> R + Sized {
    // Provided methods
    fn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>
       where Self: 'static,
             S: 'static,
             G: TransformerOnce<R, S> + 'static,
             T: 'static,
             R: 'static { ... }
    fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>
       where Self: 'static,
             P: Predicate<T> + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

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

Provides composition methods (and_then, compose, when) for one-time use closures and function pointers without requiring explicit wrapping in BoxTransformerOnce.

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

§Design Rationale

While closures automatically implement TransformerOnce<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 BoxTransformerOnce for maximum flexibility.

§Examples

§Chain composition with and_then

use qubit_function::{TransformerOnce, FnTransformerOnceOps};

let parse = |s: String| s.parse::<i32>().unwrap_or(0);
let double = |x: i32| x * 2;

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

§Forward composition with and_then

use qubit_function::{TransformerOnce, FnTransformerOnceOps};

let double = |x: i32| x * 2;
let parse = |s: String| s.parse::<i32>().unwrap_or(0);

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

§Conditional transformation with when

use qubit_function::{TransformerOnce, FnTransformerOnceOps};

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);

Provided Methods§

Source

fn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>
where Self: 'static, S: 'static, G: TransformerOnce<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 BoxTransformerOnce.

§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 this is a FnOnce transformer, 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<T, S> representing the composition

§Examples
use qubit_function::{TransformerOnce, FnTransformerOnceOps,
    BoxTransformerOnce};

let parse = |s: String| s.parse::<i32>().unwrap_or(0);
let double = BoxTransformerOnce::new(|x: i32| x * 2);

// double is moved and consumed
let composed = parse.and_then(double);
assert_eq!(composed.apply("21".to_string()), 42);
// double.apply(5); // Would not compile - moved
Examples found in repository?
examples/transformers/fn_transformer_once_ops_demo.rs (line 27)
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

fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<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 BoxConditionalTransformerOnce<T, R>

§Examples
§Basic usage with or_else
use qubit_function::{TransformerOnce, FnTransformerOnceOps};

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);
§Preserving predicate with a second closure
use qubit_function::{Predicate, TransformerOnce, FnTransformerOnceOps};

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_once_ops_demo.rs (line 60)
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}

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> FnTransformerOnceOps<T, R> for F
where F: FnOnce(T) -> R,

Blanket implementation of FnTransformerOnceOps for all FnOnce closures

Automatically implements FnTransformerOnceOps<T, R> for any type that implements FnOnce(T) -> R.