Skip to content

Commit 1aa6079

Browse files
authored
Merge pull request #61 from fraktalio/feature/map-to-own-f
`map` - better ergonomics - f1 and f2 are owned now
2 parents a39c7ce + be6d5c5 commit 1aa6079

8 files changed

+99
-81
lines changed

src/decider.rs

+50-39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use crate::{
24
DecideFunction, Decider3, Decider4, Decider5, Decider6, EvolveFunction, InitialStateFunction,
35
Sum, Sum3, Sum4, Sum5, Sum6,
@@ -163,22 +165,31 @@ pub struct Decider<'a, C: 'a, S: 'a, E: 'a, Error: 'a = ()> {
163165
impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
164166
/// Maps the Decider over the S/State type parameter.
165167
/// Creates a new instance of [Decider]`<C, S2, E, Error>`.
166-
pub fn map_state<S2, F1, F2>(self, f1: &'a F1, f2: &'a F2) -> Decider<'a, C, S2, E, Error>
168+
pub fn map_state<S2, F1, F2>(self, f1: F1, f2: F2) -> Decider<'a, C, S2, E, Error>
167169
where
168-
F1: Fn(&S2) -> S + Send + Sync,
169-
F2: Fn(&S) -> S2 + Send + Sync,
170+
F1: Fn(&S2) -> S + Send + Sync + 'a,
171+
F2: Fn(&S) -> S2 + Send + Sync + 'a,
170172
{
171-
let new_decide = Box::new(move |c: &C, s2: &S2| {
172-
let s = f1(s2);
173-
(self.decide)(c, &s)
174-
});
173+
let f1 = Arc::new(f1);
174+
let f2 = Arc::new(f2);
175175

176-
let new_evolve = Box::new(move |s2: &S2, e: &E| {
177-
let s = f1(s2);
178-
f2(&(self.evolve)(&s, e))
179-
});
176+
let new_decide = {
177+
let f1 = Arc::clone(&f1);
178+
Box::new(move |c: &C, s2: &S2| {
179+
let s = f1(s2);
180+
(self.decide)(c, &s)
181+
})
182+
};
183+
184+
let new_evolve = {
185+
let f2 = Arc::clone(&f2);
186+
Box::new(move |s2: &S2, e: &E| {
187+
let s = f1(s2);
188+
f2(&(self.evolve)(&s, e))
189+
})
190+
};
180191

181-
let new_initial_state = Box::new(move || f2(&(self.initial_state)()));
192+
let new_initial_state = { Box::new(move || f2(&(self.initial_state)())) };
182193

183194
Decider {
184195
decide: new_decide,
@@ -189,10 +200,10 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
189200

190201
/// Maps the Decider over the E/Event type parameter.
191202
/// Creates a new instance of [Decider]`<C, S, E2, Error>`.
192-
pub fn map_event<E2, F1, F2>(self, f1: &'a F1, f2: &'a F2) -> Decider<'a, C, S, E2, Error>
203+
pub fn map_event<E2, F1, F2>(self, f1: F1, f2: F2) -> Decider<'a, C, S, E2, Error>
193204
where
194-
F1: Fn(&E2) -> E + Send + Sync,
195-
F2: Fn(&E) -> E2 + Send + Sync,
205+
F1: Fn(&E2) -> E + Send + Sync + 'a,
206+
F2: Fn(&E) -> E2 + Send + Sync + 'a,
196207
{
197208
let new_decide = Box::new(move |c: &C, s: &S| {
198209
(self.decide)(c, s).map(|result| result.into_iter().map(|e: E| f2(&e)).collect())
@@ -214,9 +225,9 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
214225

215226
/// Maps the Decider over the C/Command type parameter.
216227
/// Creates a new instance of [Decider]`<C2, S, E, Error>`.
217-
pub fn map_command<C2, F>(self, f: &'a F) -> Decider<'a, C2, S, E, Error>
228+
pub fn map_command<C2, F>(self, f: F) -> Decider<'a, C2, S, E, Error>
218229
where
219-
F: Fn(&C2) -> C + Send + Sync,
230+
F: Fn(&C2) -> C + Send + Sync + 'a,
220231
{
221232
let new_decide = Box::new(move |c2: &C2, s: &S| {
222233
let c = f(c2);
@@ -236,9 +247,9 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
236247

237248
/// Maps the Decider over the Error type parameter.
238249
/// Creates a new instance of [Decider]`<C, S, E, Error2>`.
239-
pub fn map_error<Error2, F>(self, f: &'a F) -> Decider<'a, C, S, E, Error2>
250+
pub fn map_error<Error2, F>(self, f: F) -> Decider<'a, C, S, E, Error2>
240251
where
241-
F: Fn(&Error) -> Error2 + Send + Sync,
252+
F: Fn(&Error) -> Error2 + Send + Sync + 'a,
242253
{
243254
let new_decide = Box::new(move |c: &C, s: &S| (self.decide)(c, s).map_err(|e| f(&e)));
244255

@@ -337,22 +348,22 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
337348
combined
338349
.combine(decider3)
339350
.map_state(
340-
&|s: &(S, S2, S3)| ((s.0.clone(), s.1.clone()), s.2.clone()),
341-
&|s: &((S, S2), S3)| (s.0 .0.clone(), s.0 .1.clone(), s.1.clone()),
351+
|s: &(S, S2, S3)| ((s.0.clone(), s.1.clone()), s.2.clone()),
352+
|s: &((S, S2), S3)| (s.0 .0.clone(), s.0 .1.clone(), s.1.clone()),
342353
)
343354
.map_event(
344-
&|e: &Sum3<E, E2, E3>| match e {
355+
|e: &Sum3<E, E2, E3>| match e {
345356
Sum3::First(ref e) => Sum::First(Sum::First(e.clone())),
346357
Sum3::Second(ref e) => Sum::First(Sum::Second(e.clone())),
347358
Sum3::Third(ref e) => Sum::Second(e.clone()),
348359
},
349-
&|e| match e {
360+
|e: &Sum<Sum<E, E2>, E3>| match e {
350361
Sum::First(Sum::First(e)) => Sum3::First(e.clone()),
351362
Sum::First(Sum::Second(e)) => Sum3::Second(e.clone()),
352363
Sum::Second(e) => Sum3::Third(e.clone()),
353364
},
354365
)
355-
.map_command(&|c: &Sum3<C, C2, C3>| match c {
366+
.map_command(|c: &Sum3<C, C2, C3>| match c {
356367
Sum3::First(c) => Sum::First(Sum::First(c.clone())),
357368
Sum3::Second(c) => Sum::First(Sum::Second(c.clone())),
358369
Sum3::Third(c) => Sum::Second(c.clone()),
@@ -386,8 +397,8 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
386397
.combine(decider3)
387398
.combine(decider4)
388399
.map_state(
389-
&|s: &(S, S2, S3, S4)| (((s.0.clone(), s.1.clone()), s.2.clone()), s.3.clone()),
390-
&|s: &(((S, S2), S3), S4)| {
400+
|s: &(S, S2, S3, S4)| (((s.0.clone(), s.1.clone()), s.2.clone()), s.3.clone()),
401+
|s: &(((S, S2), S3), S4)| {
391402
(
392403
s.0 .0 .0.clone(),
393404
s.0 .0 .1.clone(),
@@ -397,20 +408,20 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
397408
},
398409
)
399410
.map_event(
400-
&|e: &Sum4<E, E2, E3, E4>| match e {
411+
|e: &Sum4<E, E2, E3, E4>| match e {
401412
Sum4::First(e) => Sum::First(Sum::First(Sum::First(e.clone()))),
402413
Sum4::Second(e) => Sum::First(Sum::First(Sum::Second(e.clone()))),
403414
Sum4::Third(e) => Sum::First(Sum::Second(e.clone())),
404415
Sum4::Fourth(e) => Sum::Second(e.clone()),
405416
},
406-
&|e| match e {
417+
|e: &Sum<Sum<Sum<E, E2>, E3>, E4>| match e {
407418
Sum::First(Sum::First(Sum::First(e))) => Sum4::First(e.clone()),
408419
Sum::First(Sum::First(Sum::Second(e))) => Sum4::Second(e.clone()),
409420
Sum::First(Sum::Second(e)) => Sum4::Third(e.clone()),
410421
Sum::Second(e) => Sum4::Fourth(e.clone()),
411422
},
412423
)
413-
.map_command(&|c: &Sum4<C, C2, C3, C4>| match c {
424+
.map_command(|c: &Sum4<C, C2, C3, C4>| match c {
414425
Sum4::First(c) => Sum::First(Sum::First(Sum::First(c.clone()))),
415426
Sum4::Second(c) => Sum::First(Sum::First(Sum::Second(c.clone()))),
416427
Sum4::Third(c) => Sum::First(Sum::Second(c.clone())),
@@ -451,13 +462,13 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
451462
.combine(decider4)
452463
.combine(decider5)
453464
.map_state(
454-
&|s: &(S, S2, S3, S4, S5)| {
465+
|s: &(S, S2, S3, S4, S5)| {
455466
(
456467
(((s.0.clone(), s.1.clone()), s.2.clone()), s.3.clone()),
457468
s.4.clone(),
458469
)
459470
},
460-
&|s: &((((S, S2), S3), S4), S5)| {
471+
|s: &((((S, S2), S3), S4), S5)| {
461472
(
462473
s.0 .0 .0 .0.clone(),
463474
s.0 .0 .0 .1.clone(),
@@ -468,22 +479,22 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
468479
},
469480
)
470481
.map_event(
471-
&|e: &Sum5<E, E2, E3, E4, E5>| match e {
482+
|e: &Sum5<E, E2, E3, E4, E5>| match e {
472483
Sum5::First(e) => Sum::First(Sum::First(Sum::First(Sum::First(e.clone())))),
473484
Sum5::Second(e) => Sum::First(Sum::First(Sum::First(Sum::Second(e.clone())))),
474485
Sum5::Third(e) => Sum::First(Sum::First(Sum::Second(e.clone()))),
475486
Sum5::Fourth(e) => Sum::First(Sum::Second(e.clone())),
476487
Sum5::Fifth(e) => Sum::Second(e.clone()),
477488
},
478-
&|e| match e {
489+
|e: &Sum<Sum<Sum<Sum<E, E2>, E3>, E4>, E5>| match e {
479490
Sum::First(Sum::First(Sum::First(Sum::First(e)))) => Sum5::First(e.clone()),
480491
Sum::First(Sum::First(Sum::First(Sum::Second(e)))) => Sum5::Second(e.clone()),
481492
Sum::First(Sum::First(Sum::Second(e))) => Sum5::Third(e.clone()),
482493
Sum::First(Sum::Second(e)) => Sum5::Fourth(e.clone()),
483494
Sum::Second(e) => Sum5::Fifth(e.clone()),
484495
},
485496
)
486-
.map_command(&|c: &Sum5<C, C2, C3, C4, C5>| match c {
497+
.map_command(|c: &Sum5<C, C2, C3, C4, C5>| match c {
487498
Sum5::First(c) => Sum::First(Sum::First(Sum::First(Sum::First(c.clone())))),
488499
Sum5::Second(c) => Sum::First(Sum::First(Sum::First(Sum::Second(c.clone())))),
489500
Sum5::Third(c) => Sum::First(Sum::First(Sum::Second(c.clone()))),
@@ -530,7 +541,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
530541
.combine(decider5)
531542
.combine(decider6)
532543
.map_state(
533-
&|s: &(S, S2, S3, S4, S5, S6)| {
544+
|s: &(S, S2, S3, S4, S5, S6)| {
534545
(
535546
(
536547
(((s.0.clone(), s.1.clone()), s.2.clone()), s.3.clone()),
@@ -539,7 +550,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
539550
s.5.clone(),
540551
)
541552
},
542-
&|s: &(((((S, S2), S3), S4), S5), S6)| {
553+
|s: &(((((S, S2), S3), S4), S5), S6)| {
543554
(
544555
s.0 .0 .0 .0 .0.clone(),
545556
s.0 .0 .0 .0 .1.clone(),
@@ -551,7 +562,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
551562
},
552563
)
553564
.map_event(
554-
&|e: &Sum6<E, E2, E3, E4, E5, E6>| match e {
565+
|e: &Sum6<E, E2, E3, E4, E5, E6>| match e {
555566
Sum6::First(e) => {
556567
Sum::First(Sum::First(Sum::First(Sum::First(Sum::First(e.clone())))))
557568
}
@@ -563,7 +574,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
563574
Sum6::Fifth(e) => Sum::First(Sum::Second(e.clone())),
564575
Sum6::Sixth(e) => Sum::Second(e.clone()),
565576
},
566-
&|e| match e {
577+
|e: &Sum<Sum<Sum<Sum<Sum<E, E2>, E3>, E4>, E5>, E6>| match e {
567578
Sum::First(Sum::First(Sum::First(Sum::First(Sum::First(e))))) => {
568579
Sum6::First(e.clone())
569580
}
@@ -576,7 +587,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
576587
Sum::Second(e) => Sum6::Sixth(e.clone()),
577588
},
578589
)
579-
.map_command(&|c: &Sum6<C, C2, C3, C4, C5, C6>| match c {
590+
.map_command(|c: &Sum6<C, C2, C3, C4, C5, C6>| match c {
580591
Sum6::First(c) => {
581592
Sum::First(Sum::First(Sum::First(Sum::First(Sum::First(c.clone())))))
582593
}

src/saga.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ pub struct Saga<'a, AR: 'a, A: 'a> {
8989
impl<'a, AR, A> Saga<'a, AR, A> {
9090
/// Maps the Saga over the A/Action type parameter.
9191
/// Creates a new instance of [Saga]`<AR, A2>`.
92-
pub fn map_action<A2, F>(self, f: &'a F) -> Saga<'a, AR, A2>
92+
pub fn map_action<A2, F>(self, f: F) -> Saga<'a, AR, A2>
9393
where
94-
F: Fn(&A) -> A2 + Send + Sync,
94+
F: Fn(&A) -> A2 + Send + Sync + 'a,
9595
{
9696
let new_react = Box::new(move |ar: &AR| {
9797
let a = (self.react)(ar);
@@ -103,9 +103,9 @@ impl<'a, AR, A> Saga<'a, AR, A> {
103103

104104
/// Maps the Saga over the AR/ActionResult type parameter.
105105
/// Creates a new instance of [Saga]`<AR2, A>`.
106-
pub fn map_action_result<AR2, F>(self, f: &'a F) -> Saga<'a, AR2, A>
106+
pub fn map_action_result<AR2, F>(self, f: F) -> Saga<'a, AR2, A>
107107
where
108-
F: Fn(&AR2) -> AR + Send + Sync,
108+
F: Fn(&AR2) -> AR + Send + Sync + 'a,
109109
{
110110
let new_react = Box::new(move |ar2: &AR2| {
111111
let ar = f(ar2);
@@ -170,7 +170,7 @@ impl<'a, AR, A> Saga<'a, AR, A> {
170170
{
171171
self.merge(saga2)
172172
.merge(saga3)
173-
.map_action(&|a: &Sum<A3, Sum<A2, A>>| match a {
173+
.map_action(|a: &Sum<A3, Sum<A2, A>>| match a {
174174
Sum::First(a) => Sum3::Third(a.clone()),
175175
Sum::Second(Sum::First(a)) => Sum3::Second(a.clone()),
176176
Sum::Second(Sum::Second(a)) => Sum3::First(a.clone()),
@@ -190,15 +190,14 @@ impl<'a, AR, A> Saga<'a, AR, A> {
190190
A3: Clone,
191191
A4: Clone,
192192
{
193-
self.merge(saga2)
194-
.merge(saga3)
195-
.merge(saga4)
196-
.map_action(&|a: &Sum<A4, Sum<A3, Sum<A2, A>>>| match a {
193+
self.merge(saga2).merge(saga3).merge(saga4).map_action(
194+
|a: &Sum<A4, Sum<A3, Sum<A2, A>>>| match a {
197195
Sum::First(a) => Sum4::Fourth(a.clone()),
198196
Sum::Second(Sum::First(a)) => Sum4::Third(a.clone()),
199197
Sum::Second(Sum::Second(Sum::First(a))) => Sum4::Second(a.clone()),
200198
Sum::Second(Sum::Second(Sum::Second(a))) => Sum4::First(a.clone()),
201-
})
199+
},
200+
)
202201
}
203202

204203
#[allow(clippy::type_complexity)]
@@ -221,7 +220,7 @@ impl<'a, AR, A> Saga<'a, AR, A> {
221220
.merge(saga3)
222221
.merge(saga4)
223222
.merge(saga5)
224-
.map_action(&|a: &Sum<A5, Sum<A4, Sum<A3, Sum<A2, A>>>>| match a {
223+
.map_action(|a: &Sum<A5, Sum<A4, Sum<A3, Sum<A2, A>>>>| match a {
225224
Sum::First(a) => Sum5::Fifth(a.clone()),
226225
Sum::Second(Sum::First(a)) => Sum5::Fourth(a.clone()),
227226
Sum::Second(Sum::Second(Sum::First(a))) => Sum5::Third(a.clone()),
@@ -254,7 +253,7 @@ impl<'a, AR, A> Saga<'a, AR, A> {
254253
.merge(saga5)
255254
.merge(saga6)
256255
.map_action(
257-
&|a: &Sum<A6, Sum<A5, Sum<A4, Sum<A3, Sum<A2, A>>>>>| match a {
256+
|a: &Sum<A6, Sum<A5, Sum<A4, Sum<A3, Sum<A2, A>>>>>| match a {
258257
Sum::First(a) => Sum6::Sixth(a.clone()),
259258
Sum::Second(Sum::First(a)) => Sum6::Fifth(a.clone()),
260259
Sum::Second(Sum::Second(Sum::First(a))) => Sum6::Fourth(a.clone()),

0 commit comments

Comments
 (0)