1
+ use std:: sync:: Arc ;
2
+
1
3
use crate :: {
2
4
DecideFunction , Decider3 , Decider4 , Decider5 , Decider6 , EvolveFunction , InitialStateFunction ,
3
5
Sum , Sum3 , Sum4 , Sum5 , Sum6 ,
@@ -163,22 +165,31 @@ pub struct Decider<'a, C: 'a, S: 'a, E: 'a, Error: 'a = ()> {
163
165
impl < ' a , C , S , E , Error > Decider < ' a , C , S , E , Error > {
164
166
/// Maps the Decider over the S/State type parameter.
165
167
/// 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 >
167
169
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 ,
170
172
{
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) ;
175
175
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
+ } ;
180
191
181
- let new_initial_state = Box :: new ( move || f2 ( & ( self . initial_state ) ( ) ) ) ;
192
+ let new_initial_state = { Box :: new ( move || f2 ( & ( self . initial_state ) ( ) ) ) } ;
182
193
183
194
Decider {
184
195
decide : new_decide,
@@ -189,10 +200,10 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
189
200
190
201
/// Maps the Decider over the E/Event type parameter.
191
202
/// 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 >
193
204
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 ,
196
207
{
197
208
let new_decide = Box :: new ( move |c : & C , s : & S | {
198
209
( 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> {
214
225
215
226
/// Maps the Decider over the C/Command type parameter.
216
227
/// 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 >
218
229
where
219
- F : Fn ( & C2 ) -> C + Send + Sync ,
230
+ F : Fn ( & C2 ) -> C + Send + Sync + ' a ,
220
231
{
221
232
let new_decide = Box :: new ( move |c2 : & C2 , s : & S | {
222
233
let c = f ( c2) ;
@@ -236,9 +247,9 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
236
247
237
248
/// Maps the Decider over the Error type parameter.
238
249
/// 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 >
240
251
where
241
- F : Fn ( & Error ) -> Error2 + Send + Sync ,
252
+ F : Fn ( & Error ) -> Error2 + Send + Sync + ' a ,
242
253
{
243
254
let new_decide = Box :: new ( move |c : & C , s : & S | ( self . decide ) ( c, s) . map_err ( |e| f ( & e) ) ) ;
244
255
@@ -337,22 +348,22 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
337
348
combined
338
349
. combine ( decider3)
339
350
. 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 ( ) ) ,
342
353
)
343
354
. map_event (
344
- & |e : & Sum3 < E , E2 , E3 > | match e {
355
+ |e : & Sum3 < E , E2 , E3 > | match e {
345
356
Sum3 :: First ( ref e) => Sum :: First ( Sum :: First ( e. clone ( ) ) ) ,
346
357
Sum3 :: Second ( ref e) => Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ,
347
358
Sum3 :: Third ( ref e) => Sum :: Second ( e. clone ( ) ) ,
348
359
} ,
349
- & |e | match e {
360
+ | e : & Sum < Sum < E , E2 > , E3 > | match e {
350
361
Sum :: First ( Sum :: First ( e) ) => Sum3 :: First ( e. clone ( ) ) ,
351
362
Sum :: First ( Sum :: Second ( e) ) => Sum3 :: Second ( e. clone ( ) ) ,
352
363
Sum :: Second ( e) => Sum3 :: Third ( e. clone ( ) ) ,
353
364
} ,
354
365
)
355
- . map_command ( & |c : & Sum3 < C , C2 , C3 > | match c {
366
+ . map_command ( |c : & Sum3 < C , C2 , C3 > | match c {
356
367
Sum3 :: First ( c) => Sum :: First ( Sum :: First ( c. clone ( ) ) ) ,
357
368
Sum3 :: Second ( c) => Sum :: First ( Sum :: Second ( c. clone ( ) ) ) ,
358
369
Sum3 :: Third ( c) => Sum :: Second ( c. clone ( ) ) ,
@@ -386,8 +397,8 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
386
397
. combine ( decider3)
387
398
. combine ( decider4)
388
399
. 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 ) | {
391
402
(
392
403
s. 0 . 0 . 0 . clone ( ) ,
393
404
s. 0 . 0 . 1 . clone ( ) ,
@@ -397,20 +408,20 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
397
408
} ,
398
409
)
399
410
. map_event (
400
- & |e : & Sum4 < E , E2 , E3 , E4 > | match e {
411
+ |e : & Sum4 < E , E2 , E3 , E4 > | match e {
401
412
Sum4 :: First ( e) => Sum :: First ( Sum :: First ( Sum :: First ( e. clone ( ) ) ) ) ,
402
413
Sum4 :: Second ( e) => Sum :: First ( Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ) ,
403
414
Sum4 :: Third ( e) => Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ,
404
415
Sum4 :: Fourth ( e) => Sum :: Second ( e. clone ( ) ) ,
405
416
} ,
406
- & |e | match e {
417
+ | e : & Sum < Sum < Sum < E , E2 > , E3 > , E4 > | match e {
407
418
Sum :: First ( Sum :: First ( Sum :: First ( e) ) ) => Sum4 :: First ( e. clone ( ) ) ,
408
419
Sum :: First ( Sum :: First ( Sum :: Second ( e) ) ) => Sum4 :: Second ( e. clone ( ) ) ,
409
420
Sum :: First ( Sum :: Second ( e) ) => Sum4 :: Third ( e. clone ( ) ) ,
410
421
Sum :: Second ( e) => Sum4 :: Fourth ( e. clone ( ) ) ,
411
422
} ,
412
423
)
413
- . map_command ( & |c : & Sum4 < C , C2 , C3 , C4 > | match c {
424
+ . map_command ( |c : & Sum4 < C , C2 , C3 , C4 > | match c {
414
425
Sum4 :: First ( c) => Sum :: First ( Sum :: First ( Sum :: First ( c. clone ( ) ) ) ) ,
415
426
Sum4 :: Second ( c) => Sum :: First ( Sum :: First ( Sum :: Second ( c. clone ( ) ) ) ) ,
416
427
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> {
451
462
. combine ( decider4)
452
463
. combine ( decider5)
453
464
. map_state (
454
- & |s : & ( S , S2 , S3 , S4 , S5 ) | {
465
+ |s : & ( S , S2 , S3 , S4 , S5 ) | {
455
466
(
456
467
( ( ( s. 0 . clone ( ) , s. 1 . clone ( ) ) , s. 2 . clone ( ) ) , s. 3 . clone ( ) ) ,
457
468
s. 4 . clone ( ) ,
458
469
)
459
470
} ,
460
- & |s : & ( ( ( ( S , S2 ) , S3 ) , S4 ) , S5 ) | {
471
+ |s : & ( ( ( ( S , S2 ) , S3 ) , S4 ) , S5 ) | {
461
472
(
462
473
s. 0 . 0 . 0 . 0 . clone ( ) ,
463
474
s. 0 . 0 . 0 . 1 . clone ( ) ,
@@ -468,22 +479,22 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
468
479
} ,
469
480
)
470
481
. map_event (
471
- & |e : & Sum5 < E , E2 , E3 , E4 , E5 > | match e {
482
+ |e : & Sum5 < E , E2 , E3 , E4 , E5 > | match e {
472
483
Sum5 :: First ( e) => Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( e. clone ( ) ) ) ) ) ,
473
484
Sum5 :: Second ( e) => Sum :: First ( Sum :: First ( Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ) ) ,
474
485
Sum5 :: Third ( e) => Sum :: First ( Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ) ,
475
486
Sum5 :: Fourth ( e) => Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ,
476
487
Sum5 :: Fifth ( e) => Sum :: Second ( e. clone ( ) ) ,
477
488
} ,
478
- & |e | match e {
489
+ | e : & Sum < Sum < Sum < Sum < E , E2 > , E3 > , E4 > , E5 > | match e {
479
490
Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( e) ) ) ) => Sum5 :: First ( e. clone ( ) ) ,
480
491
Sum :: First ( Sum :: First ( Sum :: First ( Sum :: Second ( e) ) ) ) => Sum5 :: Second ( e. clone ( ) ) ,
481
492
Sum :: First ( Sum :: First ( Sum :: Second ( e) ) ) => Sum5 :: Third ( e. clone ( ) ) ,
482
493
Sum :: First ( Sum :: Second ( e) ) => Sum5 :: Fourth ( e. clone ( ) ) ,
483
494
Sum :: Second ( e) => Sum5 :: Fifth ( e. clone ( ) ) ,
484
495
} ,
485
496
)
486
- . map_command ( & |c : & Sum5 < C , C2 , C3 , C4 , C5 > | match c {
497
+ . map_command ( |c : & Sum5 < C , C2 , C3 , C4 , C5 > | match c {
487
498
Sum5 :: First ( c) => Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( c. clone ( ) ) ) ) ) ,
488
499
Sum5 :: Second ( c) => Sum :: First ( Sum :: First ( Sum :: First ( Sum :: Second ( c. clone ( ) ) ) ) ) ,
489
500
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> {
530
541
. combine ( decider5)
531
542
. combine ( decider6)
532
543
. map_state (
533
- & |s : & ( S , S2 , S3 , S4 , S5 , S6 ) | {
544
+ |s : & ( S , S2 , S3 , S4 , S5 , S6 ) | {
534
545
(
535
546
(
536
547
( ( ( 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> {
539
550
s. 5 . clone ( ) ,
540
551
)
541
552
} ,
542
- & |s : & ( ( ( ( ( S , S2 ) , S3 ) , S4 ) , S5 ) , S6 ) | {
553
+ |s : & ( ( ( ( ( S , S2 ) , S3 ) , S4 ) , S5 ) , S6 ) | {
543
554
(
544
555
s. 0 . 0 . 0 . 0 . 0 . clone ( ) ,
545
556
s. 0 . 0 . 0 . 0 . 1 . clone ( ) ,
@@ -551,7 +562,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
551
562
} ,
552
563
)
553
564
. map_event (
554
- & |e : & Sum6 < E , E2 , E3 , E4 , E5 , E6 > | match e {
565
+ |e : & Sum6 < E , E2 , E3 , E4 , E5 , E6 > | match e {
555
566
Sum6 :: First ( e) => {
556
567
Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( e. clone ( ) ) ) ) ) )
557
568
}
@@ -563,7 +574,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
563
574
Sum6 :: Fifth ( e) => Sum :: First ( Sum :: Second ( e. clone ( ) ) ) ,
564
575
Sum6 :: Sixth ( e) => Sum :: Second ( e. clone ( ) ) ,
565
576
} ,
566
- & |e | match e {
577
+ | e : & Sum < Sum < Sum < Sum < Sum < E , E2 > , E3 > , E4 > , E5 > , E6 > | match e {
567
578
Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( e) ) ) ) ) => {
568
579
Sum6 :: First ( e. clone ( ) )
569
580
}
@@ -576,7 +587,7 @@ impl<'a, C, S, E, Error> Decider<'a, C, S, E, Error> {
576
587
Sum :: Second ( e) => Sum6 :: Sixth ( e. clone ( ) ) ,
577
588
} ,
578
589
)
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 {
580
591
Sum6 :: First ( c) => {
581
592
Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( Sum :: First ( c. clone ( ) ) ) ) ) )
582
593
}
0 commit comments