1#[fp_macros::document_module]
20mod inner {
21 use {
22 crate::{
23 Apply,
24 brands::{
25 ControlFlowBrand,
26 ControlFlowBreakAppliedBrand,
27 ControlFlowContinueAppliedBrand,
28 },
29 classes::{
30 Applicative,
31 ApplyFirst,
32 ApplySecond,
33 Bifoldable,
34 Bifunctor,
35 Bitraversable,
36 CloneableFn,
37 Foldable,
38 Functor,
39 Lift,
40 MonadRec,
41 Monoid,
42 Pointed,
43 Semiapplicative,
44 Semimonad,
45 Traversable,
46 },
47 impl_kind,
48 kinds::*,
49 },
50 core::ops::ControlFlow,
51 fp_macros::*,
52 };
53
54 impl ControlFlowBrand {
66 #[document_signature]
68 #[document_type_parameters("The break type.", "The continue type.")]
70 #[document_parameters("The control flow value.")]
72 #[document_returns("`true` if the value is `Continue`, `false` otherwise.")]
74 #[inline]
76 #[document_examples]
77 pub fn is_continue<B, C>(cf: &ControlFlow<B, C>) -> bool {
88 matches!(cf, ControlFlow::Continue(_))
89 }
90
91 #[document_signature]
93 #[document_type_parameters("The break type.", "The continue type.")]
95 #[document_parameters("The control flow value.")]
97 #[document_returns("`true` if the value is `Break`, `false` otherwise.")]
99 #[inline]
101 #[document_examples]
102 pub fn is_break<B, C>(cf: &ControlFlow<B, C>) -> bool {
113 matches!(cf, ControlFlow::Break(_))
114 }
115
116 #[document_signature]
118 #[document_type_parameters(
120 "The break type.",
121 "The original continue type.",
122 "The new continue type."
123 )]
124 #[document_parameters(
126 "The control flow value.",
127 "The function to apply to the continue value."
128 )]
129 #[document_returns("A new `ControlFlow` with the continue value transformed.")]
131 #[document_examples]
133 pub fn map_continue<B, C, C2>(
145 cf: ControlFlow<B, C>,
146 f: impl FnOnce(C) -> C2,
147 ) -> ControlFlow<B, C2> {
148 match cf {
149 ControlFlow::Continue(c) => ControlFlow::Continue(f(c)),
150 ControlFlow::Break(b) => ControlFlow::Break(b),
151 }
152 }
153
154 #[document_signature]
156 #[document_type_parameters(
158 "The original break type.",
159 "The continue type.",
160 "The new break type."
161 )]
162 #[document_parameters(
164 "The control flow value.",
165 "The function to apply to the break value."
166 )]
167 #[document_returns("A new `ControlFlow` with the break value transformed.")]
169 #[document_examples]
171 pub fn map_break<B, C, B2>(
183 cf: ControlFlow<B, C>,
184 f: impl FnOnce(B) -> B2,
185 ) -> ControlFlow<B2, C> {
186 match cf {
187 ControlFlow::Continue(c) => ControlFlow::Continue(c),
188 ControlFlow::Break(b) => ControlFlow::Break(f(b)),
189 }
190 }
191
192 #[document_signature]
194 #[document_type_parameters(
196 "The original break type.",
197 "The original continue type.",
198 "The new break type.",
199 "The new continue type."
200 )]
201 #[document_parameters(
203 "The control flow value.",
204 "The function to apply to the continue value.",
205 "The function to apply to the break value."
206 )]
207 #[document_returns("A new `ControlFlow` with both values transformed.")]
209 #[document_examples]
210 pub fn bimap<B, C, B2, C2>(
222 cf: ControlFlow<B, C>,
223 f: impl FnOnce(C) -> C2,
224 g: impl FnOnce(B) -> B2,
225 ) -> ControlFlow<B2, C2> {
226 match cf {
227 ControlFlow::Continue(c) => ControlFlow::Continue(f(c)),
228 ControlFlow::Break(b) => ControlFlow::Break(g(b)),
229 }
230 }
231
232 #[document_signature]
236 #[document_type_parameters(
238 "The break type.",
239 "The continue type.",
240 "The accumulator type."
241 )]
242 #[document_parameters(
244 "The control flow value.",
245 "The step function for the Continue variant.",
246 "The step function for the Break variant.",
247 "The initial accumulator."
248 )]
249 #[document_returns("The result of folding.")]
251 #[document_examples]
253 pub fn bi_fold_right<B, C, Acc>(
264 cf: ControlFlow<B, C>,
265 f: impl FnOnce(C, Acc) -> Acc,
266 g: impl FnOnce(B, Acc) -> Acc,
267 z: Acc,
268 ) -> Acc {
269 match cf {
270 ControlFlow::Continue(c) => f(c, z),
271 ControlFlow::Break(b) => g(b, z),
272 }
273 }
274
275 #[document_signature]
279 #[document_type_parameters(
281 "The break type.",
282 "The continue type.",
283 "The accumulator type."
284 )]
285 #[document_parameters(
287 "The control flow value.",
288 "The step function for the Continue variant.",
289 "The step function for the Break variant.",
290 "The initial accumulator."
291 )]
292 #[document_returns("The result of folding.")]
294 #[document_examples]
296 pub fn bi_fold_left<B, C, Acc>(
307 cf: ControlFlow<B, C>,
308 f: impl FnOnce(Acc, C) -> Acc,
309 g: impl FnOnce(Acc, B) -> Acc,
310 z: Acc,
311 ) -> Acc {
312 match cf {
313 ControlFlow::Continue(c) => f(z, c),
314 ControlFlow::Break(b) => g(z, b),
315 }
316 }
317
318 #[document_signature]
322 #[document_type_parameters("The break type.", "The continue type.", "The monoid type.")]
324 #[document_parameters(
326 "The control flow value.",
327 "The function mapping the Continue value to the monoid.",
328 "The function mapping the Break value to the monoid."
329 )]
330 #[document_returns("The monoid value.")]
332 #[document_examples]
334 pub fn bi_fold_map<B, C, M>(
348 cf: ControlFlow<B, C>,
349 f: impl FnOnce(C) -> M,
350 g: impl FnOnce(B) -> M,
351 ) -> M {
352 match cf {
353 ControlFlow::Continue(c) => f(c),
354 ControlFlow::Break(b) => g(b),
355 }
356 }
357
358 #[document_signature]
363 #[document_type_parameters(
365 "The break type.",
366 "The continue type.",
367 "The accumulator type."
368 )]
369 #[document_parameters(
371 "The control flow value.",
372 "The function to apply to the Break value and the accumulator.",
373 "The initial accumulator."
374 )]
375 #[document_returns("The result of folding.")]
377 #[document_examples]
379 pub fn fold_right<B, C, Acc>(
390 cf: ControlFlow<B, C>,
391 f: impl FnOnce(B, Acc) -> Acc,
392 initial: Acc,
393 ) -> Acc {
394 match cf {
395 ControlFlow::Continue(_) => initial,
396 ControlFlow::Break(b) => f(b, initial),
397 }
398 }
399
400 #[document_signature]
405 #[document_type_parameters(
407 "The break type.",
408 "The continue type.",
409 "The accumulator type."
410 )]
411 #[document_parameters(
413 "The control flow value.",
414 "The function to apply to the accumulator and the Break value.",
415 "The initial accumulator."
416 )]
417 #[document_returns("The result of folding.")]
419 #[document_examples]
421 pub fn fold_left<B, C, Acc>(
432 cf: ControlFlow<B, C>,
433 f: impl FnOnce(Acc, B) -> Acc,
434 initial: Acc,
435 ) -> Acc {
436 match cf {
437 ControlFlow::Continue(_) => initial,
438 ControlFlow::Break(b) => f(initial, b),
439 }
440 }
441
442 #[document_signature]
447 #[document_type_parameters("The break type.", "The continue type.", "The monoid type.")]
449 #[document_parameters("The control flow value.", "The mapping function.")]
451 #[document_returns("The monoid value.")]
453 #[document_examples]
455 pub fn fold_map<B, C, M: Monoid>(
466 cf: ControlFlow<B, C>,
467 f: impl FnOnce(B) -> M,
468 ) -> M {
469 match cf {
470 ControlFlow::Continue(_) => M::empty(),
471 ControlFlow::Break(b) => f(b),
472 }
473 }
474
475 #[document_signature]
480 #[document_type_parameters(
482 "The original break type.",
483 "The continue type.",
484 "The type of the resulting break value."
485 )]
486 #[document_parameters(
488 "The control flow value.",
489 "The function to apply to the Break value."
490 )]
491 #[document_returns("The result of the computation.")]
493 #[document_examples]
495 pub fn bind_break<B, C, B2>(
507 cf: ControlFlow<B, C>,
508 f: impl FnOnce(B) -> ControlFlow<B2, C>,
509 ) -> ControlFlow<B2, C> {
510 match cf {
511 ControlFlow::Continue(c) => ControlFlow::Continue(c),
512 ControlFlow::Break(b) => f(b),
513 }
514 }
515
516 #[document_signature]
521 #[document_type_parameters(
523 "The break type.",
524 "The original continue type.",
525 "The type of the resulting continue value."
526 )]
527 #[document_parameters(
529 "The control flow value.",
530 "The function to apply to the Continue value."
531 )]
532 #[document_returns("The result of the computation.")]
534 #[document_examples]
536 pub fn bind_continue<B, C, C2>(
548 cf: ControlFlow<B, C>,
549 f: impl FnOnce(C) -> ControlFlow<B, C2>,
550 ) -> ControlFlow<B, C2> {
551 match cf {
552 ControlFlow::Continue(c) => f(c),
553 ControlFlow::Break(b) => ControlFlow::Break(b),
554 }
555 }
556
557 #[document_signature]
559 #[document_type_parameters("The break type.", "The continue type.")]
561 #[document_parameters("The control flow value.")]
563 #[document_returns("`Some(b)` if `Break(b)`, `None` if `Continue(_)`.")]
565 #[inline]
567 #[document_examples]
568 pub fn break_val<B, C>(cf: ControlFlow<B, C>) -> Option<B> {
582 match cf {
583 ControlFlow::Break(b) => Some(b),
584 ControlFlow::Continue(_) => None,
585 }
586 }
587
588 #[document_signature]
590 #[document_type_parameters("The break type.", "The continue type.")]
592 #[document_parameters("The control flow value.")]
594 #[document_returns("`Some(c)` if `Continue(c)`, `None` if `Break(_)`.")]
596 #[inline]
598 #[document_examples]
599 pub fn continue_val<B, C>(cf: ControlFlow<B, C>) -> Option<C> {
613 match cf {
614 ControlFlow::Continue(c) => Some(c),
615 ControlFlow::Break(_) => None,
616 }
617 }
618
619 #[document_signature]
621 #[document_type_parameters("The break type.", "The continue type.")]
623 #[document_parameters("The control flow value.")]
625 #[document_returns("A new `ControlFlow` with the variants swapped.")]
627 #[inline]
629 #[document_examples]
630 pub fn swap<B, C>(cf: ControlFlow<B, C>) -> ControlFlow<C, B> {
644 match cf {
645 ControlFlow::Continue(c) => ControlFlow::Break(c),
646 ControlFlow::Break(b) => ControlFlow::Continue(b),
647 }
648 }
649
650 #[document_signature]
654 #[document_type_parameters(
656 "The lifetime of the values.",
657 "The break type.",
658 "The continue type.",
659 "The output type for the Continue value.",
660 "The output type for the Break value.",
661 "The applicative context."
662 )]
663 #[document_parameters(
665 "The control flow value.",
666 "The function for the Continue value.",
667 "The function for the Break value."
668 )]
669 #[document_returns("The transformed control flow wrapped in the applicative context.")]
671 #[document_examples]
673 pub fn bi_traverse<'a, B: 'a, C: 'a, C2: 'a + Clone, B2: 'a + Clone, F: Applicative>(
692 cf: ControlFlow<B, C>,
693 f: impl Fn(C) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C2>) + 'a,
694 g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B2>) + 'a,
695 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B2, C2>>)
696 where
697 ControlFlow<B2, C2>: Clone, {
698 match cf {
699 ControlFlow::Continue(c) => F::map(|c2| ControlFlow::Continue(c2), f(c)),
700 ControlFlow::Break(b) => F::map(|b2| ControlFlow::Break(b2), g(b)),
701 }
702 }
703 }
704
705 impl_kind! {
708 for ControlFlowBrand {
709 type Of<C, B> = ControlFlow<B, C>;
710 }
711 }
712
713 impl_kind! {
714 for ControlFlowBrand {
715 type Of<'a, C: 'a, B: 'a>: 'a = ControlFlow<B, C>;
716 }
717 }
718
719 impl Bifunctor for ControlFlowBrand {
720 #[document_signature]
724 #[document_type_parameters(
726 "The lifetime of the values.",
727 "The type of the continue value.",
728 "The type of the mapped continue value.",
729 "The type of the break value.",
730 "The type of the mapped break value."
731 )]
732 #[document_parameters(
734 "The function to apply to the continue value.",
735 "The function to apply to the break value.",
736 "The control flow to map over."
737 )]
738 #[document_returns("A new control flow containing the mapped values.")]
740 #[document_examples]
741 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
759 f: impl Fn(A) -> B + 'a,
760 g: impl Fn(C) -> D + 'a,
761 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
762 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
763 ControlFlowBrand::bimap(p, f, g)
764 }
765 }
766
767 impl Bifoldable for ControlFlowBrand {
768 #[document_signature]
772 #[document_type_parameters(
774 "The lifetime of the values.",
775 "The brand of the cloneable function to use.",
776 "The type of the Continue value.",
777 "The type of the Break value.",
778 "The accumulator type."
779 )]
780 #[document_parameters(
782 "The step function for the Continue variant.",
783 "The step function for the Break variant.",
784 "The initial accumulator.",
785 "The control flow to fold."
786 )]
787 #[document_returns("The folded result.")]
789 #[document_examples]
790 fn bi_fold_right<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
812 f: impl Fn(A, C) -> C + 'a,
813 g: impl Fn(B, C) -> C + 'a,
814 z: C,
815 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
816 ) -> C {
817 ControlFlowBrand::bi_fold_right(p, f, g, z)
818 }
819
820 #[document_signature]
824 #[document_type_parameters(
826 "The lifetime of the values.",
827 "The brand of the cloneable function to use.",
828 "The type of the Continue value.",
829 "The type of the Break value.",
830 "The accumulator type."
831 )]
832 #[document_parameters(
834 "The step function for the Continue variant.",
835 "The step function for the Break variant.",
836 "The initial accumulator.",
837 "The control flow to fold."
838 )]
839 #[document_returns("The folded result.")]
841 #[document_examples]
842 fn bi_fold_left<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
864 f: impl Fn(C, A) -> C + 'a,
865 g: impl Fn(C, B) -> C + 'a,
866 z: C,
867 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
868 ) -> C {
869 ControlFlowBrand::bi_fold_left(p, f, g, z)
870 }
871
872 #[document_signature]
876 #[document_type_parameters(
878 "The lifetime of the values.",
879 "The brand of the cloneable function to use.",
880 "The type of the Continue value.",
881 "The type of the Break value.",
882 "The monoid type."
883 )]
884 #[document_parameters(
886 "The function mapping the Continue value to the monoid.",
887 "The function mapping the Break value to the monoid.",
888 "The control flow to fold."
889 )]
890 #[document_returns("The monoid value.")]
892 #[document_examples]
893 fn bi_fold_map<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
914 f: impl Fn(A) -> M + 'a,
915 g: impl Fn(B) -> M + 'a,
916 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
917 ) -> M
918 where
919 M: Monoid + 'a, {
920 ControlFlowBrand::bi_fold_map(p, f, g)
921 }
922 }
923
924 impl Bitraversable for ControlFlowBrand {
925 #[document_signature]
930 #[document_type_parameters(
932 "The lifetime of the values.",
933 "The type of the Continue value.",
934 "The type of the Break value.",
935 "The output type for Continue.",
936 "The output type for Break.",
937 "The applicative context."
938 )]
939 #[document_parameters(
941 "The function applied to the Continue value.",
942 "The function applied to the Break value.",
943 "The control flow to traverse."
944 )]
945 #[document_returns("The transformed control flow wrapped in the applicative context.")]
947 #[document_examples]
948 fn bi_traverse<
969 'a,
970 A: 'a + Clone,
971 B: 'a + Clone,
972 C: 'a + Clone,
973 D: 'a + Clone,
974 F: Applicative,
975 >(
976 f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
977 g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
978 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
979 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>)>)
980 {
981 ControlFlowBrand::bi_traverse::<_, _, C, D, F>(p, f, g)
982 }
983 }
984
985 impl_kind! {
988 impl<ContinueType: 'static> for ControlFlowContinueAppliedBrand<ContinueType> {
989 type Of<'a, B: 'a>: 'a = ControlFlow<B, ContinueType>;
990 }
991 }
992
993 #[document_type_parameters("The continue type.")]
994 impl<ContinueType: 'static> Functor for ControlFlowContinueAppliedBrand<ContinueType> {
995 #[document_signature]
999 #[document_type_parameters(
1001 "The lifetime of the values.",
1002 "The type of the break value.",
1003 "The type of the result of applying the function."
1004 )]
1005 #[document_parameters(
1007 "The function to apply to the break value.",
1008 "The control flow to map over."
1009 )]
1010 #[document_returns(
1012 "A new control flow containing the result of applying the function to the break value."
1013 )]
1014 #[document_examples]
1016 fn map<'a, A: 'a, B: 'a>(
1035 func: impl Fn(A) -> B + 'a,
1036 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1037 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1038 ControlFlowBrand::map_break(fa, func)
1039 }
1040 }
1041
1042 #[document_type_parameters("The continue type.")]
1043 impl<ContinueType: Clone + 'static> Lift for ControlFlowContinueAppliedBrand<ContinueType> {
1044 #[document_signature]
1048 #[document_type_parameters(
1050 "The lifetime of the values.",
1051 "The type of the first value.",
1052 "The type of the second value.",
1053 "The type of the result."
1054 )]
1055 #[document_parameters(
1057 "The binary function to apply.",
1058 "The first control flow.",
1059 "The second control flow."
1060 )]
1061 #[document_returns(
1063 "`Break(f(a, b))` if both are `Break`, otherwise the first continue encountered."
1064 )]
1065 #[document_examples]
1066 fn lift2<'a, A, B, C>(
1094 func: impl Fn(A, B) -> C + 'a,
1095 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1096 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1097 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1098 where
1099 A: Clone + 'a,
1100 B: Clone + 'a,
1101 C: 'a, {
1102 match (fa, fb) {
1103 (ControlFlow::Break(a), ControlFlow::Break(b)) => ControlFlow::Break(func(a, b)),
1104 (ControlFlow::Continue(e), _) => ControlFlow::Continue(e),
1105 (_, ControlFlow::Continue(e)) => ControlFlow::Continue(e),
1106 }
1107 }
1108 }
1109
1110 #[document_type_parameters("The continue type.")]
1111 impl<ContinueType: 'static> Pointed for ControlFlowContinueAppliedBrand<ContinueType> {
1112 #[document_signature]
1116 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1118 #[document_parameters("The value to wrap.")]
1120 #[document_returns("`Break(a)`.")]
1122 #[document_examples]
1124 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1137 ControlFlow::Break(a)
1138 }
1139 }
1140
1141 #[document_type_parameters("The continue type.")]
1142 impl<ContinueType: Clone + 'static> ApplyFirst for ControlFlowContinueAppliedBrand<ContinueType> {}
1143
1144 #[document_type_parameters("The continue type.")]
1145 impl<ContinueType: Clone + 'static> ApplySecond for ControlFlowContinueAppliedBrand<ContinueType> {}
1146
1147 #[document_type_parameters("The continue type.")]
1148 impl<ContinueType: Clone + 'static> Semiapplicative
1149 for ControlFlowContinueAppliedBrand<ContinueType>
1150 {
1151 #[document_signature]
1155 #[document_type_parameters(
1157 "The lifetime of the values.",
1158 "The brand of the cloneable function wrapper.",
1159 "The type of the input value.",
1160 "The type of the output value."
1161 )]
1162 #[document_parameters(
1164 "The control flow containing the function.",
1165 "The control flow containing the value."
1166 )]
1167 #[document_returns(
1169 "`Break(f(a))` if both are `Break`, otherwise the first continue encountered."
1170 )]
1171 #[document_examples]
1172 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
1191 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
1192 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1193 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1194 match (ff, fa) {
1195 (ControlFlow::Break(f), ControlFlow::Break(a)) => ControlFlow::Break(f(a)),
1196 (ControlFlow::Continue(e), _) => ControlFlow::Continue(e),
1197 (_, ControlFlow::Continue(e)) => ControlFlow::Continue(e),
1198 }
1199 }
1200 }
1201
1202 #[document_type_parameters("The continue type.")]
1203 impl<ContinueType: Clone + 'static> Semimonad for ControlFlowContinueAppliedBrand<ContinueType> {
1204 #[document_signature]
1208 #[document_type_parameters(
1210 "The lifetime of the values.",
1211 "The type of the result of the first computation.",
1212 "The type of the result of the second computation."
1213 )]
1214 #[document_parameters(
1216 "The first control flow.",
1217 "The function to apply to the value inside the control flow."
1218 )]
1219 #[document_returns(
1221 "The result of applying `f` to the value if `ma` is `Break`, otherwise the original continue."
1222 )]
1223 #[document_examples]
1224 fn bind<'a, A: 'a, B: 'a>(
1242 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1243 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1244 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1245 ControlFlowBrand::bind_break(ma, func)
1246 }
1247 }
1248
1249 #[document_type_parameters("The continue type.")]
1250 impl<ContinueType: 'static> Foldable for ControlFlowContinueAppliedBrand<ContinueType> {
1251 #[document_signature]
1255 #[document_type_parameters(
1257 "The lifetime of the values.",
1258 "The brand of the cloneable function to use.",
1259 "The type of the elements in the structure.",
1260 "The type of the accumulator."
1261 )]
1262 #[document_parameters(
1264 "The folding function.",
1265 "The initial value.",
1266 "The control flow to fold."
1267 )]
1268 #[document_returns("`func(a, initial)` if `fa` is `Break(a)`, otherwise `initial`.")]
1270 #[document_examples]
1272 fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
1300 func: impl Fn(A, B) -> B + 'a,
1301 initial: B,
1302 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1303 ) -> B
1304 where
1305 FnBrand: CloneableFn + 'a, {
1306 ControlFlowBrand::fold_right(fa, func, initial)
1307 }
1308
1309 #[document_signature]
1313 #[document_type_parameters(
1315 "The lifetime of the values.",
1316 "The brand of the cloneable function to use.",
1317 "The type of the elements in the structure.",
1318 "The type of the accumulator."
1319 )]
1320 #[document_parameters(
1322 "The folding function.",
1323 "The initial value.",
1324 "The control flow to fold."
1325 )]
1326 #[document_returns("`func(initial, a)` if `fa` is `Break(a)`, otherwise `initial`.")]
1328 #[document_examples]
1330 fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
1358 func: impl Fn(B, A) -> B + 'a,
1359 initial: B,
1360 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1361 ) -> B
1362 where
1363 FnBrand: CloneableFn + 'a, {
1364 ControlFlowBrand::fold_left(fa, func, initial)
1365 }
1366
1367 #[document_signature]
1371 #[document_type_parameters(
1373 "The lifetime of the values.",
1374 "The brand of the cloneable function to use.",
1375 "The type of the elements in the structure.",
1376 "The type of the monoid."
1377 )]
1378 #[document_parameters("The mapping function.", "The control flow to fold.")]
1380 #[document_returns("`func(a)` if `fa` is `Break(a)`, otherwise `M::empty()`.")]
1382 #[document_examples]
1384 fn fold_map<'a, FnBrand, A: 'a, M>(
1410 func: impl Fn(A) -> M + 'a,
1411 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1412 ) -> M
1413 where
1414 M: Monoid + 'a,
1415 FnBrand: CloneableFn + 'a, {
1416 ControlFlowBrand::fold_map(fa, func)
1417 }
1418 }
1419
1420 #[document_type_parameters("The continue type.")]
1421 impl<ContinueType: Clone + 'static> Traversable for ControlFlowContinueAppliedBrand<ContinueType> {
1422 #[document_signature]
1426 #[document_type_parameters(
1428 "The lifetime of the values.",
1429 "The type of the elements in the traversable structure.",
1430 "The type of the elements in the resulting traversable structure.",
1431 "The applicative context."
1432 )]
1433 #[document_parameters("The function to apply.", "The control flow to traverse.")]
1435 #[document_returns("The control flow wrapped in the applicative context.")]
1437 #[document_examples]
1439 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1465 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1466 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1467 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1468 where
1469 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1470 match ta {
1471 ControlFlow::Break(a) => F::map(|b| ControlFlow::Break(b), func(a)),
1472 ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1473 }
1474 }
1475
1476 #[document_signature]
1480 #[document_type_parameters(
1482 "The lifetime of the values.",
1483 "The type of the elements in the traversable structure.",
1484 "The applicative context."
1485 )]
1486 #[document_parameters("The control flow containing the applicative value.")]
1488 #[document_returns("The control flow wrapped in the applicative context.")]
1490 #[document_examples]
1492 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1517 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1518 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1519 where
1520 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1521 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1522 match ta {
1523 ControlFlow::Break(fa) => F::map(|a| ControlFlow::Break(a), fa),
1524 ControlFlow::Continue(e) => F::pure(ControlFlow::Continue(e)),
1525 }
1526 }
1527 }
1528
1529 impl_kind! {
1532 impl<BreakType: 'static> for ControlFlowBreakAppliedBrand<BreakType> {
1533 type Of<'a, C: 'a>: 'a = ControlFlow<BreakType, C>;
1534 }
1535 }
1536
1537 #[document_type_parameters("The break type.")]
1538 impl<BreakType: 'static> Functor for ControlFlowBreakAppliedBrand<BreakType> {
1539 #[document_signature]
1543 #[document_type_parameters(
1545 "The lifetime of the values.",
1546 "The type of the continue value.",
1547 "The type of the result of applying the function."
1548 )]
1549 #[document_parameters(
1551 "The function to apply to the continue value.",
1552 "The control flow to map over."
1553 )]
1554 #[document_returns(
1556 "A new control flow containing the result of applying the function to the continue value."
1557 )]
1558 #[document_examples]
1560 fn map<'a, A: 'a, B: 'a>(
1579 func: impl Fn(A) -> B + 'a,
1580 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1581 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1582 ControlFlowBrand::map_continue(fa, func)
1583 }
1584 }
1585
1586 #[document_type_parameters("The break type.")]
1587 impl<BreakType: Clone + 'static> Lift for ControlFlowBreakAppliedBrand<BreakType> {
1588 #[document_signature]
1592 #[document_type_parameters(
1594 "The lifetime of the values.",
1595 "The type of the first continue value.",
1596 "The type of the second continue value.",
1597 "The type of the result continue value."
1598 )]
1599 #[document_parameters(
1601 "The binary function to apply to the continues.",
1602 "The first control flow.",
1603 "The second control flow."
1604 )]
1605 #[document_returns(
1607 "`Continue(f(a, b))` if both are `Continue`, otherwise the first break encountered."
1608 )]
1609 #[document_examples]
1610 fn lift2<'a, A, B, C>(
1638 func: impl Fn(A, B) -> C + 'a,
1639 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1640 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1641 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1642 where
1643 A: Clone + 'a,
1644 B: Clone + 'a,
1645 C: 'a, {
1646 match (fa, fb) {
1647 (ControlFlow::Continue(a), ControlFlow::Continue(b)) =>
1648 ControlFlow::Continue(func(a, b)),
1649 (ControlFlow::Break(t), _) => ControlFlow::Break(t),
1650 (_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1651 }
1652 }
1653 }
1654
1655 #[document_type_parameters("The break type.")]
1656 impl<BreakType: 'static> Pointed for ControlFlowBreakAppliedBrand<BreakType> {
1657 #[document_signature]
1661 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1663 #[document_parameters("The value to wrap.")]
1665 #[document_returns("`Continue(a)`.")]
1667 #[document_examples]
1669 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1682 ControlFlow::Continue(a)
1683 }
1684 }
1685
1686 #[document_type_parameters("The break type.")]
1687 impl<BreakType: Clone + 'static> ApplyFirst for ControlFlowBreakAppliedBrand<BreakType> {}
1688
1689 #[document_type_parameters("The break type.")]
1690 impl<BreakType: Clone + 'static> ApplySecond for ControlFlowBreakAppliedBrand<BreakType> {}
1691
1692 #[document_type_parameters("The break type.")]
1693 impl<BreakType: Clone + 'static> Semiapplicative for ControlFlowBreakAppliedBrand<BreakType> {
1694 #[document_signature]
1698 #[document_type_parameters(
1700 "The lifetime of the values.",
1701 "The brand of the cloneable function wrapper.",
1702 "The type of the input value.",
1703 "The type of the output value."
1704 )]
1705 #[document_parameters(
1707 "The control flow containing the function (in Continue).",
1708 "The control flow containing the value (in Continue)."
1709 )]
1710 #[document_returns(
1712 "`Continue(f(a))` if both are `Continue`, otherwise the first break encountered."
1713 )]
1714 #[document_examples]
1715 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
1734 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
1735 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1736 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1737 match (ff, fa) {
1738 (ControlFlow::Continue(f), ControlFlow::Continue(a)) => ControlFlow::Continue(f(a)),
1739 (ControlFlow::Break(t), _) => ControlFlow::Break(t),
1740 (_, ControlFlow::Break(t)) => ControlFlow::Break(t),
1741 }
1742 }
1743 }
1744
1745 #[document_type_parameters("The break type.")]
1746 impl<BreakType: Clone + 'static> Semimonad for ControlFlowBreakAppliedBrand<BreakType> {
1747 #[document_signature]
1751 #[document_type_parameters(
1753 "The lifetime of the values.",
1754 "The type of the result of the first computation.",
1755 "The type of the result of the second computation."
1756 )]
1757 #[document_parameters(
1759 "The first control flow.",
1760 "The function to apply to the continue value."
1761 )]
1762 #[document_returns(
1764 "The result of applying `f` to the continue if `ma` is `Continue`, otherwise the original break."
1765 )]
1766 #[document_examples]
1768 fn bind<'a, A: 'a, B: 'a>(
1786 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1787 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1788 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1789 ControlFlowBrand::bind_continue(ma, func)
1790 }
1791 }
1792
1793 #[document_type_parameters("The break type.")]
1794 impl<BreakType: 'static> Foldable for ControlFlowBreakAppliedBrand<BreakType> {
1795 #[document_signature]
1799 #[document_type_parameters(
1801 "The lifetime of the values.",
1802 "The brand of the cloneable function to use.",
1803 "The type of the elements in the structure.",
1804 "The type of the accumulator."
1805 )]
1806 #[document_parameters(
1808 "The folding function.",
1809 "The initial value.",
1810 "The control flow to fold."
1811 )]
1812 #[document_returns("`func(a, initial)` if `fa` is `Continue(a)`, otherwise `initial`.")]
1814 #[document_examples]
1816 fn fold_right<'a, FnBrand, A: 'a, B: 'a>(
1844 func: impl Fn(A, B) -> B + 'a,
1845 initial: B,
1846 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1847 ) -> B
1848 where
1849 FnBrand: CloneableFn + 'a, {
1850 match fa {
1851 ControlFlow::Continue(e) => func(e, initial),
1852 ControlFlow::Break(_) => initial,
1853 }
1854 }
1855
1856 #[document_signature]
1860 #[document_type_parameters(
1862 "The lifetime of the values.",
1863 "The brand of the cloneable function to use.",
1864 "The type of the elements in the structure.",
1865 "The type of the accumulator."
1866 )]
1867 #[document_parameters(
1869 "The folding function.",
1870 "The initial value.",
1871 "The control flow to fold."
1872 )]
1873 #[document_returns("`func(initial, a)` if `fa` is `Continue(a)`, otherwise `initial`.")]
1875 #[document_examples]
1877 fn fold_left<'a, FnBrand, A: 'a, B: 'a>(
1905 func: impl Fn(B, A) -> B + 'a,
1906 initial: B,
1907 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1908 ) -> B
1909 where
1910 FnBrand: CloneableFn + 'a, {
1911 match fa {
1912 ControlFlow::Continue(e) => func(initial, e),
1913 ControlFlow::Break(_) => initial,
1914 }
1915 }
1916
1917 #[document_signature]
1921 #[document_type_parameters(
1923 "The lifetime of the values.",
1924 "The brand of the cloneable function to use.",
1925 "The type of the elements in the structure.",
1926 "The type of the monoid."
1927 )]
1928 #[document_parameters("The mapping function.", "The control flow to fold.")]
1930 #[document_returns("`func(a)` if `fa` is `Continue(a)`, otherwise `M::empty()`.")]
1932 #[document_examples]
1934 fn fold_map<'a, FnBrand, A: 'a, M>(
1960 func: impl Fn(A) -> M + 'a,
1961 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1962 ) -> M
1963 where
1964 M: Monoid + 'a,
1965 FnBrand: CloneableFn + 'a, {
1966 match fa {
1967 ControlFlow::Continue(e) => func(e),
1968 ControlFlow::Break(_) => M::empty(),
1969 }
1970 }
1971 }
1972
1973 #[document_type_parameters("The break type.")]
1974 impl<BreakType: Clone + 'static> Traversable for ControlFlowBreakAppliedBrand<BreakType> {
1975 #[document_signature]
1979 #[document_type_parameters(
1981 "The lifetime of the values.",
1982 "The type of the elements in the traversable structure.",
1983 "The type of the elements in the resulting traversable structure.",
1984 "The applicative context."
1985 )]
1986 #[document_parameters("The function to apply.", "The control flow to traverse.")]
1988 #[document_returns("The control flow wrapped in the applicative context.")]
1990 #[document_examples]
1992 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2018 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2019 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2020 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
2021 where
2022 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2023 match ta {
2024 ControlFlow::Continue(e) => F::map(|b| ControlFlow::Continue(b), func(e)),
2025 ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2026 }
2027 }
2028
2029 #[document_signature]
2033 #[document_type_parameters(
2035 "The lifetime of the values.",
2036 "The type of the elements in the traversable structure.",
2037 "The applicative context."
2038 )]
2039 #[document_parameters("The control flow containing the applicative value.")]
2041 #[document_returns("The control flow wrapped in the applicative context.")]
2043 #[document_examples]
2045 fn sequence<'a, A: 'a + Clone, F: Applicative>(
2070 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
2071 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
2072 where
2073 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
2074 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
2075 match ta {
2076 ControlFlow::Continue(fe) => F::map(|e| ControlFlow::Continue(e), fe),
2077 ControlFlow::Break(t) => F::pure(ControlFlow::Break(t)),
2078 }
2079 }
2080 }
2081
2082 #[document_type_parameters("The continue type.")]
2084 impl<ContinueType: Clone + 'static> MonadRec for ControlFlowContinueAppliedBrand<ContinueType> {
2085 #[document_signature]
2092 #[document_type_parameters(
2094 "The lifetime of the computation.",
2095 "The type of the initial value and loop state.",
2096 "The type of the result."
2097 )]
2098 #[document_parameters("The step function.", "The initial value.")]
2100 #[document_returns(
2102 "The result of the computation, or a continue if the step function returned `Continue`."
2103 )]
2104 #[document_examples]
2106 fn tail_rec_m<'a, A: 'a, B: 'a>(
2129 func: impl Fn(
2130 A,
2131 )
2132 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2133 + 'a,
2134 initial: A,
2135 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2136 let mut current = initial;
2137 loop {
2138 match func(current) {
2139 ControlFlow::Continue(l) => return ControlFlow::Continue(l),
2140 ControlFlow::Break(ControlFlow::Continue(next)) => current = next,
2141 ControlFlow::Break(ControlFlow::Break(b)) => return ControlFlow::Break(b),
2142 }
2143 }
2144 }
2145 }
2146
2147 #[document_type_parameters("The break type.")]
2149 impl<BreakType: Clone + 'static> MonadRec for ControlFlowBreakAppliedBrand<BreakType> {
2150 #[document_signature]
2157 #[document_type_parameters(
2159 "The lifetime of the computation.",
2160 "The type of the initial value and loop state.",
2161 "The type of the result."
2162 )]
2163 #[document_parameters("The step function.", "The initial value.")]
2165 #[document_returns(
2167 "The result of the computation, or a break if the step function returned `Break`."
2168 )]
2169 #[document_examples]
2171 fn tail_rec_m<'a, A: 'a, B: 'a>(
2194 func: impl Fn(
2195 A,
2196 )
2197 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2198 + 'a,
2199 initial: A,
2200 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2201 let mut current = initial;
2202 loop {
2203 match func(current) {
2204 ControlFlow::Break(d) => return ControlFlow::Break(d),
2205 ControlFlow::Continue(ControlFlow::Continue(next)) => current = next,
2206 ControlFlow::Continue(ControlFlow::Break(b)) =>
2207 return ControlFlow::Continue(b),
2208 }
2209 }
2210 }
2211 }
2212}
2213
2214#[cfg(test)]
2215mod tests {
2216 use {
2217 crate::{
2218 brands::*,
2219 classes::{
2220 bifunctor::*,
2221 foldable::*,
2222 functor::*,
2223 lift::*,
2224 pointed::*,
2225 semiapplicative::*,
2226 semimonad::*,
2227 traversable::*,
2228 },
2229 functions::*,
2230 },
2231 core::ops::ControlFlow,
2232 quickcheck::{
2233 Arbitrary,
2234 Gen,
2235 },
2236 quickcheck_macros::quickcheck,
2237 };
2238
2239 impl<B: Arbitrary, C: Arbitrary> Arbitrary for ControlFlowWrapper<B, C> {
2240 fn arbitrary(g: &mut Gen) -> Self {
2241 if bool::arbitrary(g) {
2242 ControlFlowWrapper(ControlFlow::Continue(C::arbitrary(g)))
2243 } else {
2244 ControlFlowWrapper(ControlFlow::Break(B::arbitrary(g)))
2245 }
2246 }
2247 }
2248
2249 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
2251 struct ControlFlowWrapper<B, C>(ControlFlow<B, C>);
2252
2253 impl<B, C> ControlFlowWrapper<B, C> {
2254 fn into_inner(self) -> ControlFlow<B, C> {
2255 self.0
2256 }
2257 }
2258
2259 #[test]
2263 fn test_is_continue() {
2264 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2265 assert!(ControlFlowBrand::is_continue(&cf));
2266 assert!(!ControlFlowBrand::is_break(&cf));
2267 }
2268
2269 #[test]
2273 fn test_is_break() {
2274 let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2275 assert!(ControlFlowBrand::is_break(&cf));
2276 assert!(!ControlFlowBrand::is_continue(&cf));
2277 }
2278
2279 #[test]
2283 fn test_map_continue() {
2284 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2285 let mapped = ControlFlowBrand::map_continue(cf, |x| x + 1);
2286 assert_eq!(mapped, ControlFlow::Continue(2));
2287
2288 let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2289 let mapped_brk = ControlFlowBrand::map_continue(brk, |x| x + 1);
2290 assert_eq!(mapped_brk, ControlFlow::Break(1));
2291 }
2292
2293 #[test]
2297 fn test_map_break() {
2298 let cf: ControlFlow<i32, i32> = ControlFlow::Break(1);
2299 let mapped = ControlFlowBrand::map_break(cf, |x| x + 1);
2300 assert_eq!(mapped, ControlFlow::Break(2));
2301
2302 let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2303 let mapped_cont = ControlFlowBrand::map_break(cont, |x| x + 1);
2304 assert_eq!(mapped_cont, ControlFlow::Continue(1));
2305 }
2306
2307 #[test]
2311 fn test_bimap() {
2312 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2313 let mapped = ControlFlowBrand::bimap(cf, |x| x + 1, |x| x * 2);
2314 assert_eq!(mapped, ControlFlow::Continue(2));
2315
2316 let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2317 let mapped_brk = ControlFlowBrand::bimap(brk, |x| x + 1, |x| x * 2);
2318 assert_eq!(mapped_brk, ControlFlow::Break(2));
2319 }
2320
2321 #[test]
2323 fn test_functor_with_continue() {
2324 let cf: ControlFlow<i32, i32> = ControlFlow::Break(5);
2325 assert_eq!(
2326 map::<ControlFlowContinueAppliedBrand<_>, _, _>(|x: i32| x * 2, cf),
2327 ControlFlow::Break(10)
2328 );
2329
2330 let cont: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2331 assert_eq!(
2332 map::<ControlFlowContinueAppliedBrand<_>, _, _>(|x: i32| x * 2, cont),
2333 ControlFlow::Continue(5)
2334 );
2335 }
2336
2337 #[test]
2339 fn test_functor_with_break() {
2340 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2341 assert_eq!(
2342 map::<ControlFlowBreakAppliedBrand<_>, _, _>(|x: i32| x * 2, cf),
2343 ControlFlow::Continue(10)
2344 );
2345
2346 let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2347 assert_eq!(
2348 map::<ControlFlowBreakAppliedBrand<_>, _, _>(|x: i32| x * 2, brk),
2349 ControlFlow::Break(5)
2350 );
2351 }
2352
2353 #[test]
2355 fn test_bifunctor() {
2356 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(5);
2357 assert_eq!(
2358 bimap::<ControlFlowBrand, _, _, _, _>(|c| c + 1, |b| b * 2, cf),
2359 ControlFlow::Continue(6)
2360 );
2361
2362 let brk: ControlFlow<i32, i32> = ControlFlow::Break(5);
2363 assert_eq!(
2364 bimap::<ControlFlowBrand, _, _, _, _>(|c| c + 1, |b| b * 2, brk),
2365 ControlFlow::Break(10)
2366 );
2367 }
2368
2369 #[quickcheck]
2372 fn functor_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2373 let x = x.into_inner();
2374 map::<ControlFlowContinueAppliedBrand<i32>, _, _>(identity, x) == x
2375 }
2376
2377 #[quickcheck]
2378 fn functor_composition_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2379 let x = x.into_inner();
2380 let f = |x: i32| x.wrapping_add(1);
2381 let g = |x: i32| x.wrapping_mul(2);
2382 map::<ControlFlowContinueAppliedBrand<i32>, _, _>(compose(f, g), x)
2383 == map::<ControlFlowContinueAppliedBrand<i32>, _, _>(
2384 f,
2385 map::<ControlFlowContinueAppliedBrand<i32>, _, _>(g, x),
2386 )
2387 }
2388
2389 #[quickcheck]
2392 fn functor_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2393 let x = x.into_inner();
2394 map::<ControlFlowBreakAppliedBrand<i32>, _, _>(identity, x) == x
2395 }
2396
2397 #[quickcheck]
2398 fn functor_composition_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2399 let x = x.into_inner();
2400 let f = |x: i32| x.wrapping_add(1);
2401 let g = |x: i32| x.wrapping_mul(2);
2402 map::<ControlFlowBreakAppliedBrand<i32>, _, _>(compose(f, g), x)
2403 == map::<ControlFlowBreakAppliedBrand<i32>, _, _>(
2404 f,
2405 map::<ControlFlowBreakAppliedBrand<i32>, _, _>(g, x),
2406 )
2407 }
2408
2409 #[quickcheck]
2412 fn bifunctor_identity(x: ControlFlowWrapper<i32, i32>) -> bool {
2413 let x = x.into_inner();
2414 bimap::<ControlFlowBrand, _, _, _, _>(identity, identity, x) == x
2415 }
2416
2417 #[quickcheck]
2418 fn bifunctor_composition(x: ControlFlowWrapper<i32, i32>) -> bool {
2419 let x = x.into_inner();
2420 let f = |x: i32| x.wrapping_add(1);
2421 let g = |x: i32| x.wrapping_mul(2);
2422 let h = |x: i32| x.wrapping_sub(1);
2423 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2424
2425 bimap::<ControlFlowBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
2426 == bimap::<ControlFlowBrand, _, _, _, _>(
2427 f,
2428 h,
2429 bimap::<ControlFlowBrand, _, _, _, _>(g, i, x),
2430 )
2431 }
2432
2433 #[test]
2440 fn test_lift2_with_continue() {
2441 let s1: ControlFlow<i32, i32> = ControlFlow::Break(1);
2442 let s2: ControlFlow<i32, i32> = ControlFlow::Break(2);
2443 let s3: ControlFlow<i32, i32> = ControlFlow::Continue(3);
2444
2445 assert_eq!(
2446 lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s2),
2447 ControlFlow::Break(3)
2448 );
2449 assert_eq!(
2450 lift2::<ControlFlowContinueAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s3),
2451 ControlFlow::Continue(3)
2452 );
2453 }
2454
2455 #[test]
2460 fn test_lift2_with_break() {
2461 let s1: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2462 let s2: ControlFlow<i32, i32> = ControlFlow::Continue(2);
2463 let s3: ControlFlow<i32, i32> = ControlFlow::Break(3);
2464
2465 assert_eq!(
2466 lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s2),
2467 ControlFlow::Continue(3)
2468 );
2469 assert_eq!(
2470 lift2::<ControlFlowBreakAppliedBrand<i32>, _, _, _>(|x, y| x + y, s1, s3),
2471 ControlFlow::Break(3)
2472 );
2473 }
2474
2475 #[test]
2481 fn test_pointed_with_continue() {
2482 assert_eq!(
2483 pure::<ControlFlowContinueAppliedBrand<()>, _>(5),
2484 ControlFlow::<_, ()>::Break(5)
2485 );
2486 }
2487
2488 #[test]
2492 fn test_pointed_with_break() {
2493 assert_eq!(
2494 pure::<ControlFlowBreakAppliedBrand<()>, _>(5),
2495 ControlFlow::<(), _>::Continue(5)
2496 );
2497 }
2498
2499 #[test]
2506 fn test_apply_with_continue() {
2507 let f = pure::<ControlFlowContinueAppliedBrand<()>, _>(
2508 cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2),
2509 );
2510 let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2511 assert_eq!(
2512 apply::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(f, x),
2513 ControlFlow::Break(10)
2514 );
2515
2516 let cont: ControlFlow<_, i32> = ControlFlow::Continue(1);
2517 let f_cont =
2518 pure::<ControlFlowContinueAppliedBrand<i32>, _>(cloneable_fn_new::<RcFnBrand, _, _>(
2519 |x: i32| x * 2,
2520 ));
2521 assert_eq!(
2522 apply::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _>(f_cont, cont),
2523 ControlFlow::Continue(1)
2524 );
2525 }
2526
2527 #[test]
2532 fn test_apply_with_break() {
2533 let f = pure::<ControlFlowBreakAppliedBrand<()>, _>(cloneable_fn_new::<RcFnBrand, _, _>(
2534 |x: i32| x * 2,
2535 ));
2536 let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2537 assert_eq!(
2538 apply::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(f, x),
2539 ControlFlow::Continue(10)
2540 );
2541
2542 let brk: ControlFlow<i32, _> = ControlFlow::Break(1);
2543 let f_brk = pure::<ControlFlowBreakAppliedBrand<i32>, _>(
2544 cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2),
2545 );
2546 assert_eq!(
2547 apply::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _>(f_brk, brk),
2548 ControlFlow::Break(1)
2549 );
2550 }
2551
2552 #[test]
2558 fn test_bind_with_continue() {
2559 let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2560 assert_eq!(
2561 bind::<ControlFlowContinueAppliedBrand<()>, _, _>(x, |i| pure::<
2562 ControlFlowContinueAppliedBrand<()>,
2563 _,
2564 >(i * 2)),
2565 ControlFlow::Break(10)
2566 );
2567
2568 let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2569 assert_eq!(
2570 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(cont, |i| pure::<
2571 ControlFlowContinueAppliedBrand<i32>,
2572 _,
2573 >(i * 2)),
2574 ControlFlow::Continue(1)
2575 );
2576 }
2577
2578 #[test]
2582 fn test_bind_with_break() {
2583 let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2584 assert_eq!(
2585 bind::<ControlFlowBreakAppliedBrand<()>, _, _>(x, |i| pure::<
2586 ControlFlowBreakAppliedBrand<()>,
2587 _,
2588 >(i * 2)),
2589 ControlFlow::Continue(10)
2590 );
2591
2592 let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2593 assert_eq!(
2594 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(brk, |i| pure::<
2595 ControlFlowBreakAppliedBrand<i32>,
2596 _,
2597 >(i * 2)),
2598 ControlFlow::Break(1)
2599 );
2600 }
2601
2602 #[test]
2608 fn test_foldable_with_continue() {
2609 let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2610 assert_eq!(
2611 fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(|a, b| a + b, 10, x),
2612 15
2613 );
2614 assert_eq!(
2615 fold_left::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(|b, a| b + a, 10, x),
2616 15
2617 );
2618 assert_eq!(
2619 fold_map::<RcFnBrand, ControlFlowContinueAppliedBrand<()>, _, _>(
2620 |a: i32| a.to_string(),
2621 x
2622 ),
2623 "5"
2624 );
2625
2626 let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2627 assert_eq!(
2628 fold_right::<RcFnBrand, ControlFlowContinueAppliedBrand<i32>, _, _>(
2629 |a, b| a + b,
2630 10,
2631 cont
2632 ),
2633 10
2634 );
2635 }
2636
2637 #[test]
2641 fn test_foldable_with_break() {
2642 let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2643 assert_eq!(
2644 fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(|a, b| a + b, 10, x),
2645 15
2646 );
2647 assert_eq!(
2648 fold_left::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(|b, a| b + a, 10, x),
2649 15
2650 );
2651 assert_eq!(
2652 fold_map::<RcFnBrand, ControlFlowBreakAppliedBrand<()>, _, _>(
2653 |a: i32| a.to_string(),
2654 x
2655 ),
2656 "5"
2657 );
2658
2659 let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2660 assert_eq!(
2661 fold_right::<RcFnBrand, ControlFlowBreakAppliedBrand<i32>, _, _>(|a, b| a + b, 10, brk),
2662 10
2663 );
2664 }
2665
2666 #[test]
2672 fn test_traversable_with_continue() {
2673 let x = pure::<ControlFlowContinueAppliedBrand<()>, _>(5);
2674 assert_eq!(
2675 traverse::<ControlFlowContinueAppliedBrand<()>, _, _, OptionBrand>(|a| Some(a * 2), x),
2676 Some(ControlFlow::Break(10))
2677 );
2678
2679 let cont: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2680 assert_eq!(
2681 traverse::<ControlFlowContinueAppliedBrand<i32>, _, _, OptionBrand>(
2682 |a| Some(a * 2),
2683 cont
2684 ),
2685 Some(ControlFlow::Continue(1))
2686 );
2687 }
2688
2689 #[test]
2693 fn test_traversable_with_break() {
2694 let x = pure::<ControlFlowBreakAppliedBrand<()>, _>(5);
2695 assert_eq!(
2696 traverse::<ControlFlowBreakAppliedBrand<()>, _, _, OptionBrand>(|a| Some(a * 2), x),
2697 Some(ControlFlow::Continue(10))
2698 );
2699
2700 let brk: ControlFlow<i32, i32> = ControlFlow::Break(1);
2701 assert_eq!(
2702 traverse::<ControlFlowBreakAppliedBrand<i32>, _, _, OptionBrand>(|a| Some(a * 2), brk),
2703 Some(ControlFlow::Break(1))
2704 );
2705 }
2706
2707 #[quickcheck]
2711 fn monad_left_identity_with_continue(a: i32) -> bool {
2712 let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2713 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(
2714 pure::<ControlFlowContinueAppliedBrand<i32>, _>(a),
2715 f,
2716 ) == f(a)
2717 }
2718
2719 #[quickcheck]
2721 fn monad_right_identity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2722 let x = x.into_inner();
2723 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(
2724 x,
2725 pure::<ControlFlowContinueAppliedBrand<i32>, _>,
2726 ) == x
2727 }
2728
2729 #[quickcheck]
2731 fn monad_associativity_with_continue(x: ControlFlowWrapper<i32, i32>) -> bool {
2732 let x = x.into_inner();
2733 let f = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_mul(2));
2734 let g = |x: i32| pure::<ControlFlowContinueAppliedBrand<i32>, _>(x.wrapping_add(1));
2735 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(
2736 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(x, f),
2737 g,
2738 ) == bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(x, |a| {
2739 bind::<ControlFlowContinueAppliedBrand<i32>, _, _>(f(a), g)
2740 })
2741 }
2742
2743 #[quickcheck]
2747 fn monad_left_identity_with_break(a: i32) -> bool {
2748 let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
2749 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(
2750 pure::<ControlFlowBreakAppliedBrand<i32>, _>(a),
2751 f,
2752 ) == f(a)
2753 }
2754
2755 #[quickcheck]
2757 fn monad_right_identity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2758 let x = x.into_inner();
2759 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(
2760 x,
2761 pure::<ControlFlowBreakAppliedBrand<i32>, _>,
2762 ) == x
2763 }
2764
2765 #[quickcheck]
2767 fn monad_associativity_with_break(x: ControlFlowWrapper<i32, i32>) -> bool {
2768 let x = x.into_inner();
2769 let f = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_mul(2));
2770 let g = |x: i32| pure::<ControlFlowBreakAppliedBrand<i32>, _>(x.wrapping_add(1));
2771 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(
2772 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(x, f),
2773 g,
2774 ) == bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(x, |a| {
2775 bind::<ControlFlowBreakAppliedBrand<i32>, _, _>(f(a), g)
2776 })
2777 }
2778
2779 #[test]
2783 fn test_applicative_continue_applied() {
2784 fn assert_applicative<B: crate::classes::Applicative>() {}
2785 assert_applicative::<ControlFlowContinueAppliedBrand<i32>>();
2786 }
2787
2788 #[test]
2790 fn test_applicative_break_applied() {
2791 fn assert_applicative<B: crate::classes::Applicative>() {}
2792 assert_applicative::<ControlFlowBreakAppliedBrand<i32>>();
2793 }
2794
2795 #[test]
2797 fn test_monad_continue_applied() {
2798 fn assert_monad<B: crate::classes::Monad>() {}
2799 assert_monad::<ControlFlowContinueAppliedBrand<i32>>();
2800 }
2801
2802 #[test]
2804 fn test_monad_break_applied() {
2805 fn assert_monad<B: crate::classes::Monad>() {}
2806 assert_monad::<ControlFlowBreakAppliedBrand<i32>>();
2807 }
2808
2809 #[quickcheck]
2814 fn monad_rec_continue_applied_identity(x: i32) -> bool {
2815 tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
2816 |a| ControlFlow::Break(ControlFlow::Break(a)),
2817 x,
2818 ) == ControlFlow::Break(x)
2819 }
2820
2821 #[test]
2824 fn monad_rec_continue_applied_sum_range() {
2825 let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
2826 |(n, acc)| {
2827 if n == 0 {
2828 ControlFlow::Break(ControlFlow::Break(acc))
2829 } else {
2830 ControlFlow::Break(ControlFlow::Continue((n - 1, acc + n)))
2831 }
2832 },
2833 (100i64, 0i64),
2834 );
2835 assert_eq!(result, ControlFlow::Break(5050));
2836 }
2837
2838 #[test]
2840 fn monad_rec_continue_applied_short_circuit() {
2841 let result = tail_rec_m::<ControlFlowContinueAppliedBrand<&str>, _, _>(
2842 |n| {
2843 if n == 5 {
2844 ControlFlow::Continue("stopped")
2845 } else {
2846 ControlFlow::Break(ControlFlow::Continue(n + 1))
2847 }
2848 },
2849 0,
2850 );
2851 assert_eq!(result, ControlFlow::<i32, &str>::Continue("stopped"));
2852 }
2853
2854 #[test]
2857 fn monad_rec_continue_applied_stack_safety() {
2858 let iterations: i64 = 200_000;
2859 let result = tail_rec_m::<ControlFlowContinueAppliedBrand<()>, _, _>(
2860 |acc| {
2861 if acc < iterations {
2862 ControlFlow::Break(ControlFlow::Continue(acc + 1))
2863 } else {
2864 ControlFlow::Break(ControlFlow::Break(acc))
2865 }
2866 },
2867 0i64,
2868 );
2869 assert_eq!(result, ControlFlow::Break(iterations));
2870 }
2871
2872 #[quickcheck]
2877 fn monad_rec_break_applied_identity(x: i32) -> bool {
2878 tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
2879 |a| ControlFlow::Continue(ControlFlow::Break(a)),
2880 x,
2881 ) == ControlFlow::Continue(x)
2882 }
2883
2884 #[test]
2887 fn monad_rec_break_applied_sum_range() {
2888 let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
2889 |(n, acc)| {
2890 if n == 0 {
2891 ControlFlow::Continue(ControlFlow::Break(acc))
2892 } else {
2893 ControlFlow::Continue(ControlFlow::Continue((n - 1, acc + n)))
2894 }
2895 },
2896 (100i64, 0i64),
2897 );
2898 assert_eq!(result, ControlFlow::Continue(5050));
2899 }
2900
2901 #[test]
2903 fn monad_rec_break_applied_short_circuit() {
2904 let result = tail_rec_m::<ControlFlowBreakAppliedBrand<&str>, _, _>(
2905 |n| {
2906 if n == 5 {
2907 ControlFlow::Break("stopped")
2908 } else {
2909 ControlFlow::Continue(ControlFlow::Continue(n + 1))
2910 }
2911 },
2912 0,
2913 );
2914 assert_eq!(result, ControlFlow::<&str, i32>::Break("stopped"));
2915 }
2916
2917 #[test]
2920 fn monad_rec_break_applied_stack_safety() {
2921 let iterations: i64 = 200_000;
2922 let result = tail_rec_m::<ControlFlowBreakAppliedBrand<()>, _, _>(
2923 |acc| {
2924 if acc < iterations {
2925 ControlFlow::Continue(ControlFlow::Continue(acc + 1))
2926 } else {
2927 ControlFlow::Continue(ControlFlow::Break(acc))
2928 }
2929 },
2930 0i64,
2931 );
2932 assert_eq!(result, ControlFlow::Continue(iterations));
2933 }
2934
2935 #[test]
2939 fn test_monad_rec_continue_applied() {
2940 fn assert_monad_rec<B: crate::classes::MonadRec>() {}
2941 assert_monad_rec::<ControlFlowContinueAppliedBrand<i32>>();
2942 }
2943
2944 #[test]
2946 fn test_monad_rec_break_applied() {
2947 fn assert_monad_rec<B: crate::classes::MonadRec>() {}
2948 assert_monad_rec::<ControlFlowBreakAppliedBrand<i32>>();
2949 }
2950
2951 #[test]
2955 fn test_break_val() {
2956 let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
2957 assert_eq!(ControlFlowBrand::break_val(cf), Some(42));
2958
2959 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(1);
2960 assert_eq!(ControlFlowBrand::break_val(cf), None);
2961 }
2962
2963 #[test]
2967 fn test_continue_val() {
2968 let cf: ControlFlow<i32, i32> = ControlFlow::Continue(7);
2969 assert_eq!(ControlFlowBrand::continue_val(cf), Some(7));
2970
2971 let cf: ControlFlow<i32, i32> = ControlFlow::Break(42);
2972 assert_eq!(ControlFlowBrand::continue_val(cf), None);
2973 }
2974
2975 #[test]
2979 fn test_swap() {
2980 let cf: ControlFlow<&str, i32> = ControlFlow::Continue(1);
2981 assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Break(1));
2982
2983 let cf: ControlFlow<&str, i32> = ControlFlow::Break("hello");
2984 assert_eq!(ControlFlowBrand::swap(cf), ControlFlow::Continue("hello"));
2985 }
2986
2987 #[quickcheck]
2989 fn break_and_continue_val_complementary(x: ControlFlowWrapper<i32, i32>) -> bool {
2990 let cf = x.into_inner();
2991 ControlFlowBrand::break_val(cf).is_some() != ControlFlowBrand::continue_val(cf).is_some()
2992 }
2993
2994 #[quickcheck]
2996 fn swap_involution(x: ControlFlowWrapper<i32, i32>) -> bool {
2997 let cf = x.into_inner();
2998 ControlFlowBrand::swap(ControlFlowBrand::swap(cf)) == cf
2999 }
3000}