1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 Apply,
10 brands::FnBrand,
11 classes::{
12 optics::*,
13 *,
14 },
15 kinds::*,
16 },
17 fp_macros::*,
18 };
19
20 #[document_type_parameters(
24 "The lifetime of the values.",
25 "The pointer brand for the function.",
26 "The source type of the structure.",
27 "The target type of the structure.",
28 "The source type of the focus.",
29 "The target type of the focus."
30 )]
31 pub struct Setter<'a, PointerBrand, S, T, A, B>
32 where
33 PointerBrand: UnsizedCoercible,
34 S: 'a,
35 T: 'a,
36 A: 'a,
37 B: 'a, {
38 pub over_fn: Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, U: 'b, V: 'b>: 'b; )>::Of<'a, (S, Box<dyn Fn(A) -> B + 'a>), T>),
40 }
41
42 #[document_type_parameters(
43 "The lifetime of the values.",
44 "The pointer brand for the function.",
45 "The source type of the structure.",
46 "The target type of the structure.",
47 "The source type of the focus.",
48 "The target type of the focus."
49 )]
50 #[document_parameters("The setter instance.")]
51 impl<'a, PointerBrand, S, T, A, B> Clone for Setter<'a, PointerBrand, S, T, A, B>
52 where
53 PointerBrand: UnsizedCoercible,
54 S: 'a,
55 T: 'a,
56 A: 'a,
57 B: 'a,
58 {
59 #[document_signature]
60 #[document_returns("A new `Setter` instance that is a copy of the original.")]
61 #[document_examples]
62 fn clone(&self) -> Self {
75 Setter {
76 over_fn: self.over_fn.clone(),
77 }
78 }
79 }
80
81 #[document_type_parameters(
82 "The lifetime of the values.",
83 "The pointer brand for the function.",
84 "The source type of the structure.",
85 "The target type of the structure.",
86 "The source type of the focus.",
87 "The target type of the focus."
88 )]
89 #[document_parameters("The setter instance.")]
90 impl<'a, PointerBrand, S, T, A, B> Setter<'a, PointerBrand, S, T, A, B>
91 where
92 PointerBrand: UnsizedCoercible,
93 S: 'a,
94 T: 'a,
95 A: 'a,
96 B: 'a,
97 {
98 #[document_signature]
100 #[document_parameters("The over function.")]
102 #[document_returns("A new instance of the type.")]
104 #[document_examples]
106 pub fn new(over: impl 'a + Fn((S, Box<dyn Fn(A) -> B + 'a>)) -> T) -> Self {
118 Setter {
119 over_fn: <FnBrand<PointerBrand> as LiftFn>::new(over),
120 }
121 }
122
123 #[document_signature]
125 #[document_parameters("The structure to update.", "The function to apply to the focus.")]
127 #[document_returns("The updated structure.")]
129 #[document_examples]
131 pub fn over(
143 &self,
144 s: S,
145 f: impl Fn(A) -> B + 'a,
146 ) -> T {
147 (self.over_fn)((s, Box::new(f)))
148 }
149 }
150
151 #[document_type_parameters(
152 "The lifetime of the values.",
153 "The pointer brand for the setter.",
154 "The source type of the structure.",
155 "The target type of the structure.",
156 "The source type of the focus.",
157 "The target type of the focus.",
158 "The pointer brand for the function profunctor."
159 )]
160 #[document_parameters("The setter instance.")]
161 impl<'a, Q, PointerBrand, S, T, A, B> Optic<'a, FnBrand<Q>, S, T, A, B>
162 for Setter<'a, PointerBrand, S, T, A, B>
163 where
164 PointerBrand: UnsizedCoercible,
165 Q: UnsizedCoercible,
166 S: 'a,
167 T: 'a,
168 A: 'a,
169 B: 'a,
170 {
171 #[document_signature]
172 #[document_parameters("The profunctor value to transform.")]
173 #[document_returns("The transformed profunctor value.")]
174 #[document_examples]
175 fn evaluate(
195 &self,
196 pab: Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, A, B>),
197 ) -> Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, S, T>) {
198 let over = self.over_fn.clone();
199 <FnBrand<Q> as Arrow>::arrow(move |s: S| {
200 let pab_clone = pab.clone();
201 over((s, Box::new(move |a| pab_clone(a))))
202 })
203 }
204 }
205
206 #[document_type_parameters(
207 "The lifetime of the values.",
208 "The pointer brand for the setter.",
209 "The source type of the structure.",
210 "The target type of the structure.",
211 "The source type of the focus.",
212 "The target type of the focus.",
213 "The pointer brand for the function profunctor."
214 )]
215 #[document_parameters("The setter instance.")]
216 impl<'a, Q, PointerBrand, S, T, A, B> SetterOptic<'a, Q, S, T, A, B>
217 for Setter<'a, PointerBrand, S, T, A, B>
218 where
219 PointerBrand: UnsizedCoercible,
220 Q: UnsizedCoercible,
221 S: 'a,
222 T: 'a,
223 A: 'a,
224 B: 'a,
225 {
226 #[document_signature]
227 #[document_parameters("The profunctor value to transform.")]
228 #[document_returns("The transformed profunctor value.")]
229 #[document_examples]
230 fn evaluate(
254 &self,
255 pab: Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, A, B>),
256 ) -> Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, S, T>) {
257 Optic::<FnBrand<Q>, S, T, A, B>::evaluate(self, pab)
258 }
259 }
260
261 #[document_type_parameters(
265 "The lifetime of the values.",
266 "The pointer brand for the function.",
267 "The type of the structure.",
268 "The type of the focus."
269 )]
270 pub struct SetterPrime<'a, PointerBrand, S, A>
271 where
272 PointerBrand: UnsizedCoercible,
273 S: 'a,
274 A: 'a, {
275 pub over_fn: Apply!(<FnBrand<PointerBrand> as Kind!( type Of<'b, U: 'b, V: 'b>: 'b; )>::Of<'a, (S, Box<dyn Fn(A) -> A + 'a>), S>),
277 }
278
279 #[document_type_parameters(
280 "The lifetime of the values.",
281 "The pointer brand for the function.",
282 "The type of the structure.",
283 "The type of the focus."
284 )]
285 #[document_parameters("The setter instance.")]
286 impl<'a, PointerBrand, S, A> Clone for SetterPrime<'a, PointerBrand, S, A>
287 where
288 PointerBrand: UnsizedCoercible,
289 S: 'a,
290 A: 'a,
291 {
292 #[document_signature]
293 #[document_returns("A new `SetterPrime` instance that is a copy of the original.")]
294 #[document_examples]
295 fn clone(&self) -> Self {
308 SetterPrime {
309 over_fn: self.over_fn.clone(),
310 }
311 }
312 }
313
314 #[document_type_parameters(
315 "The lifetime of the values.",
316 "The pointer brand for the function.",
317 "The type of the structure.",
318 "The type of the focus."
319 )]
320 #[document_parameters("The setter instance.")]
321 impl<'a, PointerBrand, S, A> SetterPrime<'a, PointerBrand, S, A>
322 where
323 PointerBrand: UnsizedCoercible,
324 S: 'a,
325 A: 'a,
326 {
327 #[document_signature]
329 #[document_parameters("The over function.")]
331 #[document_returns("A new instance of the type.")]
333 #[document_examples]
335 pub fn new(over: impl 'a + Fn((S, Box<dyn Fn(A) -> A + 'a>)) -> S) -> Self {
347 SetterPrime {
348 over_fn: <FnBrand<PointerBrand> as LiftFn>::new(over),
349 }
350 }
351
352 #[document_signature]
354 #[document_parameters("The structure to update.", "The function to apply to the focus.")]
356 #[document_returns("The updated structure.")]
358 #[document_examples]
360 pub fn over(
372 &self,
373 s: S,
374 f: impl Fn(A) -> A + 'a,
375 ) -> S {
376 (self.over_fn)((s, Box::new(f)))
377 }
378 }
379
380 #[document_type_parameters(
381 "The lifetime of the values.",
382 "The pointer brand for the setter.",
383 "The type of the structure.",
384 "The type of the focus.",
385 "The pointer brand for the function profunctor."
386 )]
387 #[document_parameters("The setter instance.")]
388 impl<'a, Q, PointerBrand, S, A> Optic<'a, FnBrand<Q>, S, S, A, A>
389 for SetterPrime<'a, PointerBrand, S, A>
390 where
391 PointerBrand: UnsizedCoercible,
392 Q: UnsizedCoercible,
393 S: 'a,
394 A: 'a,
395 {
396 #[document_signature]
397 #[document_parameters("The profunctor value to transform.")]
398 #[document_returns("The transformed profunctor value.")]
399 #[document_examples]
400 fn evaluate(
420 &self,
421 pab: Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, A, A>),
422 ) -> Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, S, S>) {
423 let over = self.over_fn.clone();
424 <FnBrand<Q> as Arrow>::arrow(move |s: S| {
425 let pab_clone = pab.clone();
426 over((s, Box::new(move |a| pab_clone(a))))
427 })
428 }
429 }
430
431 #[document_type_parameters(
432 "The lifetime of the values.",
433 "The pointer brand for the setter.",
434 "The type of the structure.",
435 "The type of the focus.",
436 "The pointer brand for the function profunctor."
437 )]
438 #[document_parameters("The setter instance.")]
439 impl<'a, Q, PointerBrand, S, A> SetterOptic<'a, Q, S, S, A, A>
440 for SetterPrime<'a, PointerBrand, S, A>
441 where
442 PointerBrand: UnsizedCoercible,
443 Q: UnsizedCoercible,
444 S: 'a,
445 A: 'a,
446 {
447 #[document_signature]
448 #[document_parameters("The profunctor value to transform.")]
449 #[document_returns("The transformed profunctor value.")]
450 #[document_examples]
451 fn evaluate(
475 &self,
476 pab: Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, A, A>),
477 ) -> Apply!(<FnBrand<Q> as Kind!( type Of<'b, X: 'b, Y: 'b>: 'b; )>::Of<'a, S, S>) {
478 Optic::<FnBrand<Q>, S, S, A, A>::evaluate(self, pab)
479 }
480 }
481}
482pub use inner::*;