pub struct BoxConditionalMapper<T, R> { /* private fields */ }Expand description
BoxConditionalMapper struct
A conditional mapper that only executes when a predicate is satisfied.
Uses BoxMapper and BoxPredicate for single ownership semantics.
This type is typically created by calling BoxMapper::when() and is
designed to work with the or_else() method to create if-then-else
logic.
§Features
- Single Ownership: Not cloneable, consumes
selfon use - Conditional Execution: Only maps when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic - Implements Mapper: Can be used anywhere a
Mapperis expected
§Examples
use prism3_function::{Mapper, BoxMapper};
let mut high_count = 0;
let mut low_count = 0;
let mut mapper = BoxMapper::new(move |x: i32| {
high_count += 1;
x * 2
})
.when(|x: &i32| *x >= 10)
.or_else(move |x| {
low_count += 1;
x + 1
});
assert_eq!(mapper.apply(15), 30); // when branch executed
assert_eq!(mapper.apply(5), 6); // or_else branch executed§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxConditionalMapper<T, R>where
T: 'static,
R: 'static,
impl<T, R> BoxConditionalMapper<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(self, else_mapper: F) -> BoxMapper<T, R>where
F: Mapper<T, R> + 'static,
pub fn or_else<F>(self, else_mapper: F) -> BoxMapper<T, R>where
F: Mapper<T, R> + 'static,
Adds an else branch
Executes the original mapper when the condition is satisfied, otherwise executes else_mapper.
§Parameters
else_mapper- The mapper for the else branch, can be:- Closure:
|x: T| -> R BoxMapper<T, R>,RcMapper<T, R>,ArcMapper<T, R>- Any type implementing
Mapper<T, R>
- Closure:
§Returns
Returns the composed BoxMapper<T, R>
§Examples
use prism3_function::{Mapper, BoxMapper};
let mut mapper = BoxMapper::new(|x: i32| x * 2)
.when(|x: &i32| *x > 0)
.or_else(|x: i32| -x);
assert_eq!(mapper.apply(5), 10); // Condition satisfied
assert_eq!(mapper.apply(-5), 5); // Condition not satisfiedExamples found in repository?
examples/mapper_demo.rs (lines 61-64)
17fn main() {
18 println!("=== Mapper Demo ===\n");
19
20 // 1. Basic BoxMapper with state
21 println!("1. BoxMapper with stateful counter:");
22 let mut counter = 0;
23 let mut mapper = BoxMapper::new(move |x: i32| {
24 counter += 1;
25 format!("Item #{}: {}", counter, x)
26 });
27
28 println!(" {}", mapper.apply(100)); // Item #1: 100
29 println!(" {}", mapper.apply(200)); // Item #2: 200
30 println!(" {}", mapper.apply(300)); // Item #3: 300
31
32 // 2. Composing mappers with and_then
33 println!("\n2. Composing mappers with and_then:");
34 let mut counter1 = 0;
35 let mapper1 = BoxMapper::new(move |x: i32| {
36 counter1 += 1;
37 x + counter1
38 });
39
40 let mut counter2 = 0;
41 let mapper2 = BoxMapper::new(move |x: i32| {
42 counter2 += 1;
43 x * counter2
44 });
45
46 let mut composed = mapper1.and_then(mapper2);
47 println!(" First call: {}", composed.apply(10)); // (10 + 1) * 1 = 11
48 println!(" Second call: {}", composed.apply(10)); // (10 + 2) * 2 = 24
49 println!(" Third call: {}", composed.apply(10)); // (10 + 3) * 3 = 39
50
51 // 3. Conditional mapping with when/or_else
52 println!("\n3. Conditional mapping:");
53 let mut high_count = 0;
54 let mut low_count = 0;
55
56 let mut conditional = BoxMapper::new(move |x: i32| {
57 high_count += 1;
58 format!("High[{}]: {} * 2 = {}", high_count, x, x * 2)
59 })
60 .when(|x: &i32| *x >= 10)
61 .or_else(move |x| {
62 low_count += 1;
63 format!("Low[{}]: {} + 1 = {}", low_count, x, x + 1)
64 });
65
66 println!(" {}", conditional.apply(15)); // High[1]: 15 * 2 = 30
67 println!(" {}", conditional.apply(5)); // Low[1]: 5 + 1 = 6
68 println!(" {}", conditional.apply(20)); // High[2]: 20 * 2 = 40
69
70 // 4. RcMapper for cloneable mappers
71 println!("\n4. RcMapper (cloneable, single-threaded):");
72 let mut counter = 0;
73 let mapper = RcMapper::new(move |x: i32| {
74 counter += 1;
75 x + counter
76 });
77
78 let mut mapper1 = mapper.clone();
79 let mut mapper2 = mapper.clone();
80
81 println!(" mapper1: {}", mapper1.apply(10)); // 11
82 println!(" mapper2: {}", mapper2.apply(10)); // 12
83 println!(" mapper1: {}", mapper1.apply(10)); // 13
84
85 // 5. ArcMapper for thread-safe mappers
86 println!("\n5. ArcMapper (thread-safe):");
87 let mut counter = 0;
88 let mapper = ArcMapper::new(move |x: i32| {
89 counter += 1;
90 format!("Result[{}]: {}", counter, x * 2)
91 });
92
93 let mut mapper_clone = mapper.clone();
94 println!(" Original: {}", mapper_clone.apply(5)); // Result[1]: 10
95 println!(" Clone: {}", mapper_clone.apply(7)); // Result[2]: 14
96
97 // 6. Using FnMapperOps extension trait
98 println!("\n6. Using FnMapperOps extension trait:");
99 let mut count = 0;
100 let mut mapper = (move |x: i32| {
101 count += 1;
102 x + count
103 })
104 .and_then(|x| x * 2);
105
106 println!(" {}", mapper.apply(10)); // (10 + 1) * 2 = 22
107 println!(" {}", mapper.apply(10)); // (10 + 2) * 2 = 24
108
109 // 7. Building a complex pipeline
110 println!("\n7. Complex processing pipeline:");
111 let mut step1_count = 0;
112 let step1 = BoxMapper::new(move |x: i32| {
113 step1_count += 1;
114 format!("Step1[{}]: {}", step1_count, x)
115 });
116
117 let mut step2_count = 0;
118 let step2 = BoxMapper::new(move |s: String| {
119 step2_count += 1;
120 format!("{} -> Step2[{}]", s, step2_count)
121 });
122
123 let mut step3_count = 0;
124 let step3 = BoxMapper::new(move |s: String| {
125 step3_count += 1;
126 format!("{} -> Step3[{}]", s, step3_count)
127 });
128
129 let mut pipeline = step1.and_then(step2).and_then(step3);
130
131 println!(" {}", pipeline.apply(100));
132 println!(" {}", pipeline.apply(200));
133
134 // 7. MapperOnce implementation - consuming mappers
135 println!("\n7. MapperOnce implementation - consuming Mappers:");
136
137 // BoxMapper can be consumed as MapperOnce
138 let mut counter = 0;
139 let box_mapper = BoxMapper::new(move |x: i32| {
140 counter += 1;
141 x * counter
142 });
143 println!(" BoxMapper consumed once: {}", box_mapper.apply_once(10)); // 10 * 1 = 10
144
145 // RcMapper can be consumed as MapperOnce
146 let mut counter = 0;
147 let rc_mapper = RcMapper::new(move |x: i32| {
148 counter += 1;
149 x + counter
150 });
151 let rc_clone = rc_mapper.clone(); // Clone before consuming
152 println!(" RcMapper consumed once: {}", rc_mapper.apply_once(10)); // 10 + 1 = 11
153 println!(
154 " RcMapper clone still works: {}",
155 rc_clone.clone().apply(10)
156 ); // 10 + 2 = 12
157
158 // ArcMapper can be consumed as MapperOnce
159 let mut counter = 0;
160 let arc_mapper = ArcMapper::new(move |x: i32| {
161 counter += 1;
162 x * counter
163 });
164 let arc_clone = arc_mapper.clone(); // Clone before consuming
165 println!(" ArcMapper consumed once: {}", arc_mapper.apply_once(10)); // 10 * 1 = 10
166 println!(
167 " ArcMapper clone still works: {}",
168 arc_clone.clone().apply(10)
169 ); // 10 * 2 = 20
170
171 // 8. Converting to BoxMapperOnce
172 println!("\n8. Converting Mappers to BoxMapperOnce:");
173
174 let mut counter = 0;
175 let mapper = BoxMapper::new(move |x: i32| {
176 counter += 1;
177 x * counter
178 });
179 let once_mapper = mapper.into_box_once();
180 println!(" BoxMapper->BoxMapperOnce: {}", once_mapper.apply_once(5)); // 5 * 1 = 5
181
182 // RcMapper can use to_box_once() to preserve original
183 let mut counter = 0;
184 let rc_mapper = RcMapper::new(move |x: i32| {
185 counter += 1;
186 x * counter
187 });
188 let once_mapper = rc_mapper.to_box_once();
189 println!(" RcMapper->BoxMapperOnce: {}", once_mapper.apply_once(5)); // 5 * 1 = 5
190 println!(
191 " Original RcMapper still works: {}",
192 rc_mapper.clone().apply(5)
193 ); // 5 * 2 = 10
194
195 println!("\n=== Demo Complete ===");
196}Auto Trait Implementations§
impl<T, R> Freeze for BoxConditionalMapper<T, R>
impl<T, R> !RefUnwindSafe for BoxConditionalMapper<T, R>
impl<T, R> !Send for BoxConditionalMapper<T, R>
impl<T, R> !Sync for BoxConditionalMapper<T, R>
impl<T, R> Unpin for BoxConditionalMapper<T, R>
impl<T, R> !UnwindSafe for BoxConditionalMapper<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more