1#![allow(deprecated_where_clause_location)]
5
6use super::*;
7
8macro_rules! fn_impl {
9 ($fn:tt) => {
10 impl<'a, A, B> Functor<'a, B> for Box<dyn 'a + $fn() -> A>
11 where
12 A: 'a,
13 B: 'a,
14 {
15 type Inner = A;
16 type Mapped = Box<dyn 'a + $fn() -> B>;
17 #[allow(unused_mut)]
18 fn fmap<F>(mut self, mut f: F) -> Self::Mapped
19 where
20 F: 'a + Send + FnMut(Self::Inner) -> B,
21 {
22 Box::new(move || f((self)()))
23 }
24 }
25 impl<'a, A, B> Functor<'a, B>
26 for Box<dyn 'a + Send + $fn() -> A>
27 where
28 A: 'a,
29 B: 'a,
30 {
31 type Inner = A;
32 type Mapped = Box<dyn 'a + Send + $fn() -> B>;
33 #[allow(unused_mut)]
34 fn fmap<F>(mut self, mut f: F) -> Self::Mapped
35 where
36 F: 'a + Send + FnMut(Self::Inner) -> B,
37 {
38 Box::new(move || f((self)()))
39 }
40 }
41
42 impl<'a, A> FunctorMut<'a, A> for Box<dyn 'a + $fn() -> A>
43 where
44 A: 'a,
45 {
46 fn fmap_mut<F>(&mut self, f: F)
47 where
48 F: 'a + Send + FnMut(&mut Self::Inner),
49 {
50 let this = std::mem::replace(
51 self,
52 Box::new(|| panic!("poisoned FunctorMut")),
53 );
54 *self = this.fmap_fn_mutref(f);
55 }
56 }
57 impl<'a, A> FunctorMut<'a, A>
58 for Box<dyn 'a + Send + $fn() -> A>
59 where
60 A: 'a,
61 {
62 fn fmap_mut<F>(&mut self, f: F)
63 where
64 F: 'a + Send + FnMut(&mut Self::Inner),
65 {
66 let this = std::mem::replace(
67 self,
68 Box::new(|| panic!("poisoned FunctorMut")),
69 );
70 *self = this.fmap_fn_mutref(f);
71 }
72 }
73
74 impl<'a, A, B, X> Functor<'a, B> for Box<dyn 'a + $fn(X) -> A>
75 where
76 A: 'a,
77 B: 'a,
78 X: 'a,
79 {
80 type Inner = A;
81 type Mapped = Box<dyn 'a + $fn(X) -> B>;
82 #[allow(unused_mut)]
83 fn fmap<F>(mut self, mut f: F) -> Self::Mapped
84 where
85 F: 'a + Send + FnMut(Self::Inner) -> B,
86 {
87 Box::new(move |x| f((self)(x)))
88 }
89 }
90 impl<'a, A, B, X> Functor<'a, B>
91 for Box<dyn 'a + Send + $fn(X) -> A>
92 where
93 A: 'a,
94 B: 'a,
95 X: 'a,
96 {
97 type Inner = A;
98 type Mapped = Box<dyn 'a + Send + $fn(X) -> B>;
99 #[allow(unused_mut)]
100 fn fmap<F>(mut self, mut f: F) -> Self::Mapped
101 where
102 F: 'a + Send + FnMut(Self::Inner) -> B,
103 {
104 Box::new(move |x| f((self)(x)))
105 }
106 }
107
108 impl<'a, A, X> FunctorMut<'a, A> for Box<dyn 'a + $fn(X) -> A>
109 where
110 A: 'a,
111 X: 'a,
112 {
113 fn fmap_mut<F>(&mut self, f: F)
114 where
115 F: 'a + Send + FnMut(&mut Self::Inner),
116 {
117 let this = std::mem::replace(
118 self,
119 Box::new(|_| panic!("poisoned FunctorMut")),
120 );
121 *self = this.fmap_fn_mutref(f);
122 }
123 }
124 impl<'a, A, X> FunctorMut<'a, A>
125 for Box<dyn 'a + Send + $fn(X) -> A>
126 where
127 A: 'a,
128 X: 'a,
129 {
130 fn fmap_mut<F>(&mut self, f: F)
131 where
132 F: 'a + Send + FnMut(&mut Self::Inner),
133 {
134 let this = std::mem::replace(
135 self,
136 Box::new(|_| panic!("poisoned FunctorMut")),
137 );
138 *self = this.fmap_fn_mutref(f);
139 }
140 }
141
142 impl<'a, A, B, R> Contravariant<'a, A>
143 for Box<dyn 'a + $fn(B) -> R>
144 where
145 A: 'a,
146 B: 'a,
147 R: 'a,
148 {
149 type Inner = B;
150 type Mapped = Box<dyn 'a + $fn(A) -> R>;
151 #[allow(unused_mut)]
152 fn contramap<F>(mut self, mut f: F) -> Self::Mapped
153 where
154 F: 'a + Send + FnMut(A) -> Self::Inner,
155 {
156 Box::new(move |consumee| (self)(f(consumee)))
157 }
158 }
159 impl<'a, A, B, R> Contravariant<'a, A>
160 for Box<dyn 'a + Send + $fn(B) -> R>
161 where
162 A: 'a,
163 B: 'a,
164 R: 'a,
165 {
166 type Inner = B;
167 type Mapped = Box<dyn 'a + Send + $fn(A) -> R>;
168 #[allow(unused_mut)]
169 fn contramap<F>(mut self, mut f: F) -> Self::Mapped
170 where
171 F: 'a + Send + FnMut(A) -> Self::Inner,
172 {
173 Box::new(move |consumee| (self)(f(consumee)))
174 }
175 }
176
177 impl<'a, A, R> ContravariantMut<'a, A>
178 for Box<dyn 'a + $fn(A) -> R>
179 where
180 A: 'a,
181 R: 'a,
182 {
183 fn contramap_mut<F>(&mut self, f: F)
184 where
185 F: 'a + Send + FnMut(&mut Self::Inner),
186 {
187 let this = std::mem::replace(
188 self,
189 Box::new(|_| panic!("poisoned ContravariantMut")),
190 );
191 *self = this.contramap_fn_mutref(f);
192 }
193 }
194 impl<'a, A, R> ContravariantMut<'a, A>
195 for Box<dyn 'a + Send + $fn(A) -> R>
196 where
197 A: 'a,
198 R: 'a,
199 {
200 fn contramap_mut<F>(&mut self, f: F)
201 where
202 F: 'a + Send + FnMut(&mut Self::Inner),
203 {
204 let this = std::mem::replace(
205 self,
206 Box::new(|_| panic!("poisoned ContravariantMut")),
207 );
208 *self = this.contramap_fn_mutref(f);
209 }
210 }
211 };
212}
213
214fn_impl!(FnOnce);
215fn_impl!(FnMut);
216
217impl<'a, A, B> Pure<'a, B> for Box<dyn 'a + FnOnce() -> A>
218where
219 A: 'a,
220 B: 'a,
221{
222 fn pure(b: B) -> Self::Mapped {
223 Box::new(move || b)
224 }
225}
226impl<'a, A, B> Pure<'a, B> for Box<dyn 'a + Send + FnOnce() -> A>
227where
228 A: 'a,
229 B: 'a + Send,
230{
231 fn pure(b: B) -> Self::Mapped {
232 Box::new(move || b)
233 }
234}
235
236impl<'a, A, B, X> Pure<'a, B> for Box<dyn 'a + FnOnce(X) -> A>
237where
238 A: 'a,
239 B: 'a,
240 X: 'a,
241{
242 fn pure(b: B) -> Self::Mapped {
243 Box::new(move |_| b)
244 }
245}
246impl<'a, A, B, X> Pure<'a, B> for Box<dyn 'a + Send + FnOnce(X) -> A>
247where
248 A: 'a,
249 B: 'a + Send,
250 X: 'a,
251{
252 fn pure(b: B) -> Self::Mapped {
253 Box::new(move |_| b)
254 }
255}
256
257impl<'a, A, B> Pure<'a, B> for Box<dyn 'a + FnMut() -> A>
258where
259 A: 'a,
260 B: 'a + Clone,
261{
262 fn pure(b: B) -> Self::Mapped {
263 Box::new(move || b.clone())
264 }
265}
266impl<'a, A, B> Pure<'a, B> for Box<dyn 'a + Send + FnMut() -> A>
267where
268 A: 'a,
269 B: 'a + Clone + Send,
270{
271 fn pure(b: B) -> Self::Mapped {
272 Box::new(move || b.clone())
273 }
274}
275
276impl<'a, A, B, X> Pure<'a, B> for Box<dyn 'a + FnMut(X) -> A>
277where
278 A: 'a,
279 B: 'a + Clone,
280 X: 'a,
281{
282 fn pure(b: B) -> Self::Mapped {
283 Box::new(move |_| b.clone())
284 }
285}
286impl<'a, A, B, X> Pure<'a, B> for Box<dyn 'a + Send + FnMut(X) -> A>
287where
288 A: 'a,
289 B: 'a + Clone + Send,
290 X: 'a,
291{
292 fn pure(b: B) -> Self::Mapped {
293 Box::new(move |_| b.clone())
294 }
295}
296
297impl<'a, A, B> Applicative<'a, B> for Box<dyn 'a + FnOnce() -> A>
298where
299 A: 'a,
300 B: 'a,
301{
302 fn apply(
303 self,
304 f: Box<dyn 'a + FnOnce() -> BoxMapper<'a, Self, B>>,
305 ) -> Box<dyn 'a + FnOnce() -> B> {
306 Box::new(move || (f())((self)()))
307 }
308}
309impl<'a, A, B> Applicative<'a, B> for Box<dyn 'a + Send + FnOnce() -> A>
310where
311 A: 'a,
312 B: 'a + Send,
313{
314 fn apply(
315 self,
316 f: Box<dyn 'a + Send + FnOnce() -> BoxMapper<'a, Self, B>>,
317 ) -> Box<dyn 'a + Send + FnOnce() -> B> {
318 Box::new(move || (f())((self)()))
319 }
320}
321
322impl<'a, A, B, X> Applicative<'a, B> for Box<dyn 'a + FnOnce(X) -> A>
323where
324 A: 'a,
325 B: 'a,
326 X: 'a + Clone,
327{
328 fn apply(
329 self,
330 f: Box<dyn 'a + FnOnce(X) -> BoxMapper<'a, Self, B>>,
331 ) -> Box<dyn 'a + FnOnce(X) -> B> {
332 Box::new(move |x| (f(x.clone()))((self)(x)))
333 }
334}
335impl<'a, A, B, X> Applicative<'a, B>
336 for Box<dyn 'a + Send + FnOnce(X) -> A>
337where
338 A: 'a,
339 B: 'a + Send,
340 X: 'a + Clone,
341{
342 fn apply(
343 self,
344 f: Box<dyn 'a + Send + FnOnce(X) -> BoxMapper<'a, Self, B>>,
345 ) -> Box<dyn 'a + Send + FnOnce(X) -> B> {
346 Box::new(move |x| (f(x.clone()))((self)(x)))
347 }
348}