1#[fp_macros::document_module]
7mod inner {
8 use {
9 crate::{
10 Apply,
11 brands::FnBrand,
12 classes::{
13 optics::*,
14 profunctor::Closed,
15 *,
16 },
17 kinds::*,
18 types::optics::zip_with_of,
19 },
20 fp_macros::*,
21 };
22
23 #[document_type_parameters(
27 "The lifetime of the values.",
28 "The reference-counted pointer type.",
29 "The source type of the structure.",
30 "The target type of the structure after an update.",
31 "The source type of the focus.",
32 "The target type of the focus after an update."
33 )]
34 pub struct Grate<'a, PointerBrand, S, T, A, B>
35 where
36 PointerBrand: ToDynCloneFn,
37 S: 'a,
38 T: 'a,
39 A: 'a,
40 B: 'a, {
41 pub grate: <FnBrand<PointerBrand> as CloneFn>::Of<
43 'a,
44 <FnBrand<PointerBrand> as CloneFn>::Of<
45 'a,
46 <FnBrand<PointerBrand> as CloneFn>::Of<
47 'a,
48 <PointerBrand as RefCountedPointer>::Of<'a, S>,
49 A,
50 >,
51 B,
52 >,
53 T,
54 >,
55 }
56
57 #[document_type_parameters(
58 "The lifetime of the values.",
59 "The reference-counted pointer type.",
60 "The source type of the structure.",
61 "The target type of the structure after an update.",
62 "The source type of the focus.",
63 "The target type of the focus after an update."
64 )]
65 #[document_parameters("The grate instance.")]
66 impl<'a, PointerBrand, S, T, A, B> Clone for Grate<'a, PointerBrand, S, T, A, B>
67 where
68 PointerBrand: ToDynCloneFn,
69 S: 'a,
70 T: 'a,
71 A: 'a,
72 B: 'a,
73 {
74 #[document_signature]
75 #[document_returns("A new `Grate` instance that is a copy of the original.")]
76 #[document_examples]
77 fn clone(&self) -> Self {
97 Grate {
98 grate: self.grate.clone(),
99 }
100 }
101 }
102
103 #[document_type_parameters(
104 "The lifetime of the values.",
105 "The reference-counted pointer type.",
106 "The source type of the structure.",
107 "The target type of the structure after an update.",
108 "The source type of the focus.",
109 "The target type of the focus after an update."
110 )]
111 #[document_parameters("The grate instance.")]
112 impl<'a, PointerBrand, S, T, A, B> Grate<'a, PointerBrand, S, T, A, B>
113 where
114 PointerBrand: ToDynCloneFn,
115 S: 'a,
116 T: 'a,
117 A: 'a,
118 B: 'a + Clone,
119 {
120 #[document_signature]
122 #[document_parameters("The grating function.")]
124 #[document_returns("A new instance of the type.")]
126 #[document_examples]
128 pub fn new(
147 grate: impl Fn(
148 <FnBrand<PointerBrand> as CloneFn>::Of<
149 'a,
150 <FnBrand<PointerBrand> as CloneFn>::Of<
151 'a,
152 <PointerBrand as RefCountedPointer>::Of<'a, S>,
153 A,
154 >,
155 B,
156 >,
157 ) -> T
158 + 'a
159 ) -> Self
160 where
161 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized, {
162 Grate {
163 grate: <FnBrand<PointerBrand> as LiftFn>::new(grate),
164 }
165 }
166
167 #[document_signature]
171 #[document_parameters(
173 "The combining function, taking a pair `(A, A)` and returning `B`.",
174 "The first structure.",
175 "The second structure."
176 )]
177 #[document_returns("The combined structure.")]
179 #[document_examples]
180 pub fn zip_with(
200 &self,
201 f: impl Fn((A, A)) -> B + 'a,
202 s1: S,
203 s2: S,
204 ) -> T
205 where
206 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized, {
207 zip_with_of::<FnBrand<PointerBrand>, S, T, A, B>(self, f, s1, s2)
208 }
209 }
210
211 #[document_type_parameters(
212 "The lifetime of the values.",
213 "The profunctor type.",
214 "The reference-counted pointer type.",
215 "The source type of the structure.",
216 "The target type of the structure after an update.",
217 "The source type of the focus.",
218 "The target type of the focus after an update."
219 )]
220 #[document_parameters("The grate instance.")]
221 impl<'a, Q, PointerBrand, S, T, A, B> Optic<'a, Q, S, T, A, B>
222 for Grate<'a, PointerBrand, S, T, A, B>
223 where
224 Q: Closed<FnBrand<PointerBrand>>,
225 PointerBrand: ToDynCloneFn,
226 S: 'a,
227 T: 'a,
228 A: 'a,
229 B: 'a + Clone,
230 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
231 {
232 #[document_signature]
234 #[document_parameters("The profunctor value to transform.")]
236 #[document_returns("The transformed profunctor value.")]
238 #[document_examples]
240 fn evaluate(
266 &self,
267 pab: Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
268 ) -> Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>) {
269 let grate = self.grate.clone();
270
271 Q::dimap(
272 move |s: S| {
273 let s_ptr = <PointerBrand as RefCountedPointer>::new(s);
274 <FnBrand<PointerBrand> as LiftFn>::new(
275 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
276 'a,
277 <PointerBrand as RefCountedPointer>::Of<'a, S>,
278 A,
279 >|
280 -> A { (f)(Clone::clone(&s_ptr)) },
281 )
282 },
283 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
284 'a,
285 <FnBrand<PointerBrand> as CloneFn>::Of<
286 'a,
287 <PointerBrand as RefCountedPointer>::Of<'a, S>,
288 A,
289 >,
290 B,
291 >| {
292 let f_brand = <FnBrand<PointerBrand> as LiftFn>::new(move |x| f(x));
293 grate(f_brand)
294 },
295 Q::closed(pab),
296 )
297 }
298 }
299
300 #[document_type_parameters(
301 "The lifetime of the values.",
302 "The reference-counted pointer type.",
303 "The source type of the structure.",
304 "The target type of the structure after an update.",
305 "The source type of the focus.",
306 "The target type of the focus after an update."
307 )]
308 #[document_parameters("The grate instance.")]
309 impl<'a, PointerBrand, S, T, A, B> GrateOptic<'a, FnBrand<PointerBrand>, S, T, A, B>
310 for Grate<'a, PointerBrand, S, T, A, B>
311 where
312 PointerBrand: ToDynCloneFn,
313 S: 'a,
314 A: 'a,
315 B: 'a + Clone,
316 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
317 {
318 #[document_signature]
319 #[document_type_parameters("The profunctor type.")]
320 #[document_parameters("The profunctor value to transform.")]
321 #[document_returns("The transformed profunctor value.")]
322 #[document_examples]
323 fn evaluate<Q: Closed<FnBrand<PointerBrand>>>(
349 &self,
350 pab: Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
351 ) -> Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>)
352 where
353 T: 'a,
354 B: 'a, {
355 let grate = self.grate.clone();
356
357 Q::dimap(
358 move |s: S| {
359 let s_ptr = <PointerBrand as RefCountedPointer>::new(s);
360 <FnBrand<PointerBrand> as LiftFn>::new(
361 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
362 'a,
363 <PointerBrand as RefCountedPointer>::Of<'a, S>,
364 A,
365 >|
366 -> A { (f)(Clone::clone(&s_ptr)) },
367 )
368 },
369 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
370 'a,
371 <FnBrand<PointerBrand> as CloneFn>::Of<
372 'a,
373 <PointerBrand as RefCountedPointer>::Of<'a, S>,
374 A,
375 >,
376 B,
377 >| {
378 let f_brand = <FnBrand<PointerBrand> as LiftFn>::new(move |x| f(x));
379 grate(f_brand)
380 },
381 Q::closed(pab),
382 )
383 }
384 }
385
386 #[document_type_parameters(
387 "The lifetime of the values.",
388 "The reference-counted pointer type.",
389 "The source type of the structure.",
390 "The target type of the structure after an update.",
391 "The source type of the focus.",
392 "The target type of the focus after an update."
393 )]
394 #[document_parameters("The grate instance.")]
395 impl<'a, PointerBrand, S, T, A, B> SetterOptic<'a, PointerBrand, S, T, A, B>
396 for Grate<'a, PointerBrand, S, T, A, B>
397 where
398 PointerBrand: ToDynCloneFn,
399 S: 'a,
400 A: 'a,
401 B: 'a + Clone,
402 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
403 {
404 #[document_signature]
405 #[document_parameters("The profunctor value to transform.")]
406 #[document_returns("The transformed profunctor value.")]
407 #[document_examples]
408 fn evaluate(
435 &self,
436 pab: Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, B>),
437 ) -> Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, T>)
438 {
439 GrateOptic::<FnBrand<PointerBrand>, S, T, A, B>::evaluate::<FnBrand<PointerBrand>>(
440 self, pab,
441 )
442 }
443 }
444
445 #[document_type_parameters(
449 "The lifetime of the values.",
450 "The reference-counted pointer type.",
451 "The type of the structure.",
452 "The type of the focus."
453 )]
454 pub struct GratePrime<'a, PointerBrand, S, A>
455 where
456 PointerBrand: ToDynCloneFn,
457 S: 'a,
458 A: 'a, {
459 pub(crate) grate_fn: <FnBrand<PointerBrand> as CloneFn>::Of<
460 'a,
461 <FnBrand<PointerBrand> as CloneFn>::Of<
462 'a,
463 <FnBrand<PointerBrand> as CloneFn>::Of<
464 'a,
465 <PointerBrand as RefCountedPointer>::Of<'a, S>,
466 A,
467 >,
468 A,
469 >,
470 S,
471 >,
472 }
473
474 #[document_type_parameters(
475 "The lifetime of the values.",
476 "The reference-counted pointer type.",
477 "The type of the structure.",
478 "The type of the focus."
479 )]
480 #[document_parameters("The grate instance.")]
481 impl<'a, PointerBrand, S, A> Clone for GratePrime<'a, PointerBrand, S, A>
482 where
483 PointerBrand: ToDynCloneFn,
484 S: 'a,
485 A: 'a,
486 {
487 #[document_signature]
488 #[document_returns("The cloned grate instance.")]
489 #[document_examples]
490 fn clone(&self) -> Self {
513 GratePrime {
514 grate_fn: self.grate_fn.clone(),
515 }
516 }
517 }
518
519 #[document_type_parameters(
520 "The lifetime of the values.",
521 "The reference-counted pointer type.",
522 "The type of the structure.",
523 "The type of the focus."
524 )]
525 impl<'a, PointerBrand, S, A> GratePrime<'a, PointerBrand, S, A>
526 where
527 PointerBrand: ToDynCloneFn,
528 S: 'a,
529 A: 'a,
530 {
531 #[document_signature]
533 #[document_parameters("The grating function.")]
535 #[document_returns("A new instance of the type.")]
537 #[document_examples]
539 pub fn new(
561 grate: impl Fn(
562 <FnBrand<PointerBrand> as CloneFn>::Of<
563 'a,
564 <FnBrand<PointerBrand> as CloneFn>::Of<
565 'a,
566 <PointerBrand as RefCountedPointer>::Of<'a, S>,
567 A,
568 >,
569 A,
570 >,
571 ) -> S
572 + 'a
573 ) -> Self
574 where
575 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized, {
576 GratePrime {
577 grate_fn: <FnBrand<PointerBrand> as LiftFn>::new(grate),
578 }
579 }
580 }
581
582 #[document_type_parameters(
583 "The lifetime of the values.",
584 "The reference-counted pointer type.",
585 "The type of the structure.",
586 "The type of the focus."
587 )]
588 #[document_parameters("The grate instance.")]
589 impl<'a, PointerBrand, S, A> GratePrime<'a, PointerBrand, S, A>
590 where
591 PointerBrand: ToDynCloneFn,
592 S: 'a,
593 A: 'a + Clone,
594 {
595 #[document_signature]
599 #[document_parameters(
601 "The combining function, taking a pair `(A, A)` and returning `A`.",
602 "The first structure.",
603 "The second structure."
604 )]
605 #[document_returns("The combined structure.")]
607 #[document_examples]
608 pub fn zip_with(
631 &self,
632 f: impl Fn((A, A)) -> A + 'a,
633 s1: S,
634 s2: S,
635 ) -> S
636 where
637 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized, {
638 zip_with_of::<FnBrand<PointerBrand>, S, S, A, A>(self, f, s1, s2)
639 }
640 }
641
642 #[document_type_parameters(
643 "The lifetime of the values.",
644 "The profunctor type.",
645 "The reference-counted pointer type.",
646 "The type of the structure.",
647 "The type of the focus."
648 )]
649 #[document_parameters("The grate instance.")]
650 impl<'a, Q, PointerBrand, S, A> Optic<'a, Q, S, S, A, A> for GratePrime<'a, PointerBrand, S, A>
651 where
652 Q: Closed<FnBrand<PointerBrand>>,
653 PointerBrand: ToDynCloneFn,
654 S: 'a,
655 A: 'a + Clone,
656 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
657 {
658 #[document_signature]
660 #[document_parameters("The profunctor value to transform.")]
661 #[document_returns("The transformed profunctor value.")]
662 #[document_examples]
664 fn evaluate(
691 &self,
692 pab: Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, A>),
693 ) -> Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>) {
694 let grate = self.grate_fn.clone();
695
696 Q::dimap(
697 move |s: S| {
698 let s_ptr = <PointerBrand as RefCountedPointer>::new(s);
699 <FnBrand<PointerBrand> as LiftFn>::new(
700 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
701 'a,
702 <PointerBrand as RefCountedPointer>::Of<'a, S>,
703 A,
704 >| { (f)(Clone::clone(&s_ptr)) },
705 )
706 },
707 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
708 'a,
709 <FnBrand<PointerBrand> as CloneFn>::Of<
710 'a,
711 <PointerBrand as RefCountedPointer>::Of<'a, S>,
712 A,
713 >,
714 A,
715 >| {
716 let f_brand = <FnBrand<PointerBrand> as LiftFn>::new(move |x| f(x));
717 grate(f_brand)
718 },
719 Q::closed(pab),
720 )
721 }
722 }
723
724 #[document_type_parameters(
725 "The lifetime of the values.",
726 "The reference-counted pointer type.",
727 "The type of the structure.",
728 "The type of the focus."
729 )]
730 #[document_parameters("The grate instance.")]
731 impl<'a, PointerBrand, S, A> GrateOptic<'a, FnBrand<PointerBrand>, S, S, A, A>
732 for GratePrime<'a, PointerBrand, S, A>
733 where
734 PointerBrand: ToDynCloneFn,
735 S: 'a,
736 A: 'a + Clone,
737 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
738 {
739 #[document_signature]
740 #[document_type_parameters("The profunctor type.")]
741 #[document_parameters("The profunctor value to transform.")]
742 #[document_returns("The transformed profunctor value.")]
743 #[document_examples]
744 fn evaluate<Q: Closed<FnBrand<PointerBrand>>>(
771 &self,
772 pab: Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, A>),
773 ) -> Apply!(<Q as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>) {
774 let grate = self.grate_fn.clone();
775
776 Q::dimap(
777 move |s: S| {
778 let s_ptr = <PointerBrand as RefCountedPointer>::new(s);
779 <FnBrand<PointerBrand> as LiftFn>::new(
780 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
781 'a,
782 <PointerBrand as RefCountedPointer>::Of<'a, S>,
783 A,
784 >| { (f)(Clone::clone(&s_ptr)) },
785 )
786 },
787 move |f: <FnBrand<PointerBrand> as CloneFn>::Of<
788 'a,
789 <FnBrand<PointerBrand> as CloneFn>::Of<
790 'a,
791 <PointerBrand as RefCountedPointer>::Of<'a, S>,
792 A,
793 >,
794 A,
795 >| {
796 let f_brand = <FnBrand<PointerBrand> as LiftFn>::new(move |x| f(x));
797 grate(f_brand)
798 },
799 Q::closed(pab),
800 )
801 }
802 }
803
804 #[document_type_parameters(
805 "The lifetime of the values.",
806 "The reference-counted pointer type.",
807 "The type of the structure.",
808 "The type of the focus."
809 )]
810 #[document_parameters("The grate instance.")]
811 impl<'a, PointerBrand, S, A> SetterOptic<'a, PointerBrand, S, S, A, A>
812 for GratePrime<'a, PointerBrand, S, A>
813 where
814 PointerBrand: ToDynCloneFn,
815 S: 'a,
816 A: 'a + Clone,
817 <PointerBrand as RefCountedPointer>::Of<'a, S>: Sized,
818 {
819 #[document_signature]
820 #[document_parameters("The profunctor value to transform.")]
821 #[document_returns("The transformed profunctor value.")]
822 #[document_examples]
823 fn evaluate(
850 &self,
851 pab: Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, A, A>),
852 ) -> Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, T: 'b, U: 'b>: 'b; )>::Of<'a, S, S>)
853 {
854 GrateOptic::<FnBrand<PointerBrand>, S, S, A, A>::evaluate::<FnBrand<PointerBrand>>(
855 self, pab,
856 )
857 }
858 }
859}
860pub use inner::*;