pub trait FnTransformerOnceOps<T, R>:
FnOnce(T) -> R
+ Sized
+ 'static {
// Provided methods
fn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>
where S: 'static,
G: TransformerOnce<R, S> + 'static,
T: 'static,
R: 'static { ... }
fn compose<S, G>(self, before: G) -> BoxTransformerOnce<S, R>
where S: 'static,
G: TransformerOnce<S, T> + 'static,
T: 'static,
R: 'static { ... }
fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>
where 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 prism3_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);§Reverse composition with compose
use prism3_function::{TransformerOnce, FnTransformerOnceOps};
let double = |x: i32| x * 2;
let to_string = |x: i32| x.to_string();
let composed = to_string.compose(double);
assert_eq!(composed.apply(21), "42");§Conditional transformation with when
use prism3_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);§Author
Hu Haixing
Provided Methods§
Sourcefn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>where
S: 'static,
G: TransformerOnce<R, S> + 'static,
T: 'static,
R: 'static,
fn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>where
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 transformerG- 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 aFnOncetransformer, 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>
- A closure:
§Returns
A new BoxTransformerOnce<T, S> representing the composition
§Examples
use prism3_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 - movedExamples found in repository?
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(\"21\") = {}",
26 composed.apply("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(\"5\") = {}",
38 chained.apply("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(21) = {}",
49 composed.apply(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(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(-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!(" transform(\"3\") = {}", complex.apply("3".to_string())); // 3 <= 5, so 3 * 3 = 9
78
79 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
80 let double2 = |x: i32| x * 2;
81 let triple2 = |x: i32| x * 3;
82 let to_string2 = |x: i32| x.to_string();
83 let complex2 = parse2
84 .and_then(double2.when(|x: &i32| *x > 5).or_else(triple2))
85 .and_then(to_string2);
86 println!(
87 " transform(\"10\") = {}",
88 complex2.apply("10".to_string())
89 ); // 10 > 5, so 10 * 2 = 20
90 println!();
91
92 // 6. Type conversion
93 println!("6. Type conversion:");
94 let to_string = |x: i32| x.to_string();
95 let get_length = |s: String| s.len();
96 let length_transformer = to_string.and_then(get_length);
97 println!(
98 " to_string.and_then(get_length).apply(12345) = {}",
99 length_transformer.apply(12345)
100 ); // 5
101 println!();
102
103 // 7. Closures that capture environment
104 println!("7. Closures that capture environment:");
105 let multiplier = 3;
106 let multiply = move |x: i32| x * multiplier;
107 let add_ten = |x: i32| x + 10;
108 let with_capture = multiply.and_then(add_ten);
109 println!(
110 " multiply(3).and_then(add_ten).apply(5) = {}",
111 with_capture.apply(5)
112 ); // 5 * 3 + 10 = 25
113 println!();
114
115 // 8. Function pointers
116 println!("8. Function pointers:");
117 fn parse_fn(s: String) -> i32 {
118 s.parse().unwrap_or(0)
119 }
120 fn double_fn(x: i32) -> i32 {
121 x * 2
122 }
123 let fn_composed = parse_fn.and_then(double_fn);
124 println!(
125 " parse_fn.and_then(double_fn).apply(\"21\") = {}",
126 fn_composed.apply("21".to_string())
127 ); // 42
128 println!();
129
130 // 9. String operations that consume ownership
131 println!("9. String operations that consume ownership:");
132 let owned = String::from("hello");
133 let append = move |s: String| format!("{} {}", s, owned);
134 let uppercase = |s: String| s.to_uppercase();
135 let composed = append.and_then(uppercase);
136 println!(
137 " append.and_then(uppercase).apply(\"world\") = {}",
138 composed.apply("world".to_string())
139 ); // "WORLD HELLO"
140 println!();
141
142 // 10. Parsing and validation
143 println!("10. Parsing and validation:");
144 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
145 let validate = |x: i32| if x > 0 { x } else { 1 };
146 let composed = parse.and_then(validate);
147 println!(
148 " parse.and_then(validate).apply(\"42\") = {}",
149 composed.apply("42".to_string())
150 ); // 42
151
152 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
153 let validate2 = |x: i32| if x > 0 { x } else { 1 };
154 let composed2 = parse2.and_then(validate2);
155 println!(
156 " parse.and_then(validate).apply(\"-5\") = {}",
157 composed2.apply("-5".to_string())
158 ); // 1
159 println!();
160
161 println!("=== Example completed ===");
162}Sourcefn compose<S, G>(self, before: G) -> BoxTransformerOnce<S, R>where
S: 'static,
G: TransformerOnce<S, T> + 'static,
T: 'static,
R: 'static,
fn compose<S, G>(self, before: G) -> BoxTransformerOnce<S, R>where
S: 'static,
G: TransformerOnce<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 BoxTransformerOnce.
§Type Parameters
S- The input type of the before transformerG- 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 this is aFnOncetransformer, 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>
- A closure:
§Returns
A new BoxTransformerOnce<S, R> representing the composition
§Examples
use prism3_function::{TransformerOnce, FnTransformerOnceOps,
BoxTransformerOnce};
let double = BoxTransformerOnce::new(|x: i32| x * 2);
let to_string = |x: i32| x.to_string();
// double is moved and consumed
let composed = to_string.compose(double);
assert_eq!(composed.apply(21), "42");
// double.apply(5); // Would not compile - movedExamples found in repository?
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(\"21\") = {}",
26 composed.apply("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(\"5\") = {}",
38 chained.apply("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(21) = {}",
49 composed.apply(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(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(-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!(" transform(\"3\") = {}", complex.apply("3".to_string())); // 3 <= 5, so 3 * 3 = 9
78
79 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
80 let double2 = |x: i32| x * 2;
81 let triple2 = |x: i32| x * 3;
82 let to_string2 = |x: i32| x.to_string();
83 let complex2 = parse2
84 .and_then(double2.when(|x: &i32| *x > 5).or_else(triple2))
85 .and_then(to_string2);
86 println!(
87 " transform(\"10\") = {}",
88 complex2.apply("10".to_string())
89 ); // 10 > 5, so 10 * 2 = 20
90 println!();
91
92 // 6. Type conversion
93 println!("6. Type conversion:");
94 let to_string = |x: i32| x.to_string();
95 let get_length = |s: String| s.len();
96 let length_transformer = to_string.and_then(get_length);
97 println!(
98 " to_string.and_then(get_length).apply(12345) = {}",
99 length_transformer.apply(12345)
100 ); // 5
101 println!();
102
103 // 7. Closures that capture environment
104 println!("7. Closures that capture environment:");
105 let multiplier = 3;
106 let multiply = move |x: i32| x * multiplier;
107 let add_ten = |x: i32| x + 10;
108 let with_capture = multiply.and_then(add_ten);
109 println!(
110 " multiply(3).and_then(add_ten).apply(5) = {}",
111 with_capture.apply(5)
112 ); // 5 * 3 + 10 = 25
113 println!();
114
115 // 8. Function pointers
116 println!("8. Function pointers:");
117 fn parse_fn(s: String) -> i32 {
118 s.parse().unwrap_or(0)
119 }
120 fn double_fn(x: i32) -> i32 {
121 x * 2
122 }
123 let fn_composed = parse_fn.and_then(double_fn);
124 println!(
125 " parse_fn.and_then(double_fn).apply(\"21\") = {}",
126 fn_composed.apply("21".to_string())
127 ); // 42
128 println!();
129
130 // 9. String operations that consume ownership
131 println!("9. String operations that consume ownership:");
132 let owned = String::from("hello");
133 let append = move |s: String| format!("{} {}", s, owned);
134 let uppercase = |s: String| s.to_uppercase();
135 let composed = append.and_then(uppercase);
136 println!(
137 " append.and_then(uppercase).apply(\"world\") = {}",
138 composed.apply("world".to_string())
139 ); // "WORLD HELLO"
140 println!();
141
142 // 10. Parsing and validation
143 println!("10. Parsing and validation:");
144 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
145 let validate = |x: i32| if x > 0 { x } else { 1 };
146 let composed = parse.and_then(validate);
147 println!(
148 " parse.and_then(validate).apply(\"42\") = {}",
149 composed.apply("42".to_string())
150 ); // 42
151
152 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
153 let validate2 = |x: i32| if x > 0 { x } else { 1 };
154 let composed2 = parse2.and_then(validate2);
155 println!(
156 " parse.and_then(validate).apply(\"-5\") = {}",
157 composed2.apply("-5".to_string())
158 ); // 1
159 println!();
160
161 println!("=== Example completed ===");
162}Sourcefn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>where
P: Predicate<T> + 'static,
T: 'static,
R: 'static,
fn when<P>(self, predicate: P) -> BoxConditionalTransformerOnce<T, R>where
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 implementsClone). 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>
- A closure:
§Returns
Returns BoxConditionalTransformerOnce<T, R>
§Examples
§Basic usage with or_else
use prism3_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 clone
use prism3_function::{TransformerOnce, FnTransformerOnceOps,
RcPredicate};
let double = |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(|x: i32| -x);
assert_eq!(conditional.apply(5), 10);
// Original predicate still usable
assert!(is_positive.test(&3));Examples found in repository?
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(\"21\") = {}",
26 composed.apply("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(\"5\") = {}",
38 chained.apply("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(21) = {}",
49 composed.apply(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(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(-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!(" transform(\"3\") = {}", complex.apply("3".to_string())); // 3 <= 5, so 3 * 3 = 9
78
79 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
80 let double2 = |x: i32| x * 2;
81 let triple2 = |x: i32| x * 3;
82 let to_string2 = |x: i32| x.to_string();
83 let complex2 = parse2
84 .and_then(double2.when(|x: &i32| *x > 5).or_else(triple2))
85 .and_then(to_string2);
86 println!(
87 " transform(\"10\") = {}",
88 complex2.apply("10".to_string())
89 ); // 10 > 5, so 10 * 2 = 20
90 println!();
91
92 // 6. Type conversion
93 println!("6. Type conversion:");
94 let to_string = |x: i32| x.to_string();
95 let get_length = |s: String| s.len();
96 let length_transformer = to_string.and_then(get_length);
97 println!(
98 " to_string.and_then(get_length).apply(12345) = {}",
99 length_transformer.apply(12345)
100 ); // 5
101 println!();
102
103 // 7. Closures that capture environment
104 println!("7. Closures that capture environment:");
105 let multiplier = 3;
106 let multiply = move |x: i32| x * multiplier;
107 let add_ten = |x: i32| x + 10;
108 let with_capture = multiply.and_then(add_ten);
109 println!(
110 " multiply(3).and_then(add_ten).apply(5) = {}",
111 with_capture.apply(5)
112 ); // 5 * 3 + 10 = 25
113 println!();
114
115 // 8. Function pointers
116 println!("8. Function pointers:");
117 fn parse_fn(s: String) -> i32 {
118 s.parse().unwrap_or(0)
119 }
120 fn double_fn(x: i32) -> i32 {
121 x * 2
122 }
123 let fn_composed = parse_fn.and_then(double_fn);
124 println!(
125 " parse_fn.and_then(double_fn).apply(\"21\") = {}",
126 fn_composed.apply("21".to_string())
127 ); // 42
128 println!();
129
130 // 9. String operations that consume ownership
131 println!("9. String operations that consume ownership:");
132 let owned = String::from("hello");
133 let append = move |s: String| format!("{} {}", s, owned);
134 let uppercase = |s: String| s.to_uppercase();
135 let composed = append.and_then(uppercase);
136 println!(
137 " append.and_then(uppercase).apply(\"world\") = {}",
138 composed.apply("world".to_string())
139 ); // "WORLD HELLO"
140 println!();
141
142 // 10. Parsing and validation
143 println!("10. Parsing and validation:");
144 let parse = |s: String| s.parse::<i32>().unwrap_or(0);
145 let validate = |x: i32| if x > 0 { x } else { 1 };
146 let composed = parse.and_then(validate);
147 println!(
148 " parse.and_then(validate).apply(\"42\") = {}",
149 composed.apply("42".to_string())
150 ); // 42
151
152 let parse2 = |s: String| s.parse::<i32>().unwrap_or(0);
153 let validate2 = |x: i32| if x > 0 { x } else { 1 };
154 let composed2 = parse2.and_then(validate2);
155 println!(
156 " parse.and_then(validate).apply(\"-5\") = {}",
157 composed2.apply("-5".to_string())
158 ); // 1
159 println!();
160
161 println!("=== Example completed ===");
162}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§
impl<T, R, F> FnTransformerOnceOps<T, R> for Fwhere
F: FnOnce(T) -> R + 'static,
Blanket implementation of FnTransformerOnceOps for all FnOnce closures
Automatically implements FnTransformerOnceOps<T, R> for any type that
implements FnOnce(T) -> R.
§Author
Hu Haixing