pub trait MapperOnce<T, R> {
// Required method
fn apply_once(self, input: T) -> R;
// Provided methods
fn into_box_once(self) -> BoxMapperOnce<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_fn_once(self) -> impl FnOnce(T) -> R
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn to_box_once(&self) -> BoxMapperOnce<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_fn_once(&self) -> impl FnOnce(T) -> R
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
}Expand description
MapperOnce trait - consuming mapper that takes ownership
Defines the behavior of a consuming mapper: converting a value of
type T to a value of type R by taking ownership of both self and the
input. This trait is analogous to FnOnce(T) -> R.
Unlike Mapper (which is like FnMut), MapperOnce consumes itself on
the first call, making it suitable for one-time transformations that may
own or consume resources.
§Type Parameters
T- The type of the input value (consumed)R- The type of the output value
§Author
胡海星
Required Methods§
Sourcefn apply_once(self, input: T) -> R
fn apply_once(self, input: T) -> R
Provided Methods§
Sourcefn into_box_once(self) -> BoxMapperOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_box_once(self) -> BoxMapperOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts to BoxMapperOnce
⚠️ Consumes self: The original mapper becomes unavailable
after calling this method.
§Returns
Returns BoxMapperOnce<T, R>
§Examples
use prism3_function::MapperOnce;
let double = |x: i32| x * 2;
let boxed = double.into_box_once();
assert_eq!(boxed.apply_once(21), 42);Examples found in repository?
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}Sourcefn into_fn_once(self) -> impl FnOnce(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn_once(self) -> impl FnOnce(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts mapper to a closure
⚠️ Consumes self: The original mapper becomes unavailable
after calling this method.
§Returns
Returns a closure that implements FnOnce(T) -> R
§Examples
use prism3_function::MapperOnce;
let double = |x: i32| x * 2;
let func = double.into_fn_once();
assert_eq!(func(21), 42);Sourcefn to_box_once(&self) -> BoxMapperOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_box_once(&self) -> BoxMapperOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to BoxMapperOnce without consuming self
📌 Borrows &self: The original mapper remains usable
after calling this method.
§Default Implementation
The default implementation creates a new BoxMapperOnce that
captures a clone. Types implementing Clone can override this method
to provide more efficient conversions.
§Returns
Returns BoxMapperOnce<T, R>
§Examples
use prism3_function::MapperOnce;
let double = |x: i32| x * 2;
let boxed = double.to_box_once();
assert_eq!(boxed.apply_once(21), 42);Examples found in repository?
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}Sourcefn to_fn_once(&self) -> impl FnOnce(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_fn_once(&self) -> impl FnOnce(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts mapper to a closure without consuming self
📌 Borrows &self: The original mapper remains usable
after calling this method.
§Default Implementation
The default implementation creates a closure that captures a
clone of self and calls its apply method. Types can
override this method to provide more efficient conversions.
§Returns
Returns a closure that implements FnOnce(T) -> R
§Examples
use prism3_function::MapperOnce;
let double = |x: i32| x * 2;
let func = double.to_fn_once();
assert_eq!(func(21), 42);Implementors§
impl<F, T, R> MapperOnce<T, R> for Fwhere
F: FnOnce(T) -> R,
T: 'static,
R: 'static,
Implement MapperOnce<T, R> for any type that implements FnOnce(T) -> R
This allows once-callable closures and function pointers to be used directly with our MapperOnce trait without wrapping.
§Examples
use prism3_function::MapperOnce;
fn parse(s: String) -> i32 {
s.parse().unwrap_or(0)
}
assert_eq!(parse.apply_once("42".to_string()), 42);
let owned_value = String::from("hello");
let consume = |s: String| {
format!("{} world", s)
};
assert_eq!(consume.apply_once(owned_value), "hello world");§Author
胡海星