funcmap/
impls_core.rs

1//! Implementations of [`FuncMap`](crate::FuncMap) and
2//! [`TryFuncMap`](crate::TryFuncMap) for types in [`core`]
3
4#![allow(clippy::mismatching_type_param_order)]
5
6/// Implementations for [arrays](prim@array)
7mod array {
8    use crate::{array, FuncMap, TryFuncMap};
9
10    impl<A, B, const N: usize> FuncMap<A, B> for [A; N] {
11        type Output = [B; N];
12
13        fn func_map<F>(self, f: F) -> Self::Output
14        where
15            F: FnMut(A) -> B,
16        {
17            self.map(f)
18        }
19    }
20
21    impl<A, B, const N: usize> TryFuncMap<A, B> for [A; N] {
22        type Output = [B; N];
23
24        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
25        where
26            F: FnMut(A) -> Result<B, E>,
27        {
28            array::try_map(self, f)
29        }
30    }
31}
32
33/// Implementations for [`core::ops::Bound`]
34mod bound {
35    use crate::{FuncMap, TryFuncMap};
36
37    use core::ops::Bound;
38
39    impl<A, B> FuncMap<A, B> for Bound<A> {
40        type Output = Bound<B>;
41
42        fn func_map<F>(self, mut f: F) -> Self::Output
43        where
44            F: FnMut(A) -> B,
45        {
46            match self {
47                Self::Included(bound) => Bound::Included(f(bound)),
48                Self::Excluded(bound) => Bound::Excluded(f(bound)),
49                Self::Unbounded => Bound::Unbounded,
50            }
51        }
52    }
53
54    impl<A, B> TryFuncMap<A, B> for Bound<A> {
55        type Output = Bound<B>;
56
57        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
58        where
59            F: FnMut(A) -> Result<B, E>,
60        {
61            Ok(match self {
62                Self::Included(bound) => Bound::Included(f(bound)?),
63                Self::Excluded(bound) => Bound::Excluded(f(bound)?),
64                Self::Unbounded => Bound::Unbounded,
65            })
66        }
67    }
68}
69
70/// Implementations for [`core::cell::Cell`]
71mod cell {
72    use crate::{FuncMap, TryFuncMap};
73
74    use core::cell::Cell;
75
76    impl<A, B> FuncMap<A, B> for Cell<A> {
77        type Output = Cell<B>;
78
79        fn func_map<F>(self, mut f: F) -> Self::Output
80        where
81            F: FnMut(A) -> B,
82        {
83            f(self.into_inner()).into()
84        }
85    }
86
87    impl<A, B> TryFuncMap<A, B> for Cell<A> {
88        type Output = Cell<B>;
89
90        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
91        where
92            F: FnMut(A) -> Result<B, E>,
93        {
94            Ok(f(self.into_inner())?.into())
95        }
96    }
97}
98
99/// Implementations for [`core::ops::ControlFlow`]
100mod control_flow {
101    use crate::{FuncMap, TryFuncMap, TypeParam};
102
103    use core::ops::ControlFlow;
104
105    impl<T, U, C> FuncMap<T, U, TypeParam<0>> for ControlFlow<T, C> {
106        type Output = ControlFlow<U, C>;
107
108        fn func_map<F>(self, mut f: F) -> Self::Output
109        where
110            F: FnMut(T) -> U,
111        {
112            match self {
113                Self::Break(value) => ControlFlow::Break(f(value)),
114                Self::Continue(value) => ControlFlow::Continue(value),
115            }
116        }
117    }
118
119    impl<T, U, C> TryFuncMap<T, U, TypeParam<0>> for ControlFlow<T, C> {
120        type Output = ControlFlow<U, C>;
121
122        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
123        where
124            F: FnMut(T) -> Result<U, E>,
125        {
126            Ok(match self {
127                Self::Break(value) => ControlFlow::Break(f(value)?),
128                Self::Continue(value) => ControlFlow::Continue(value),
129            })
130        }
131    }
132
133    impl<B, T, U> FuncMap<T, U, TypeParam<1>> for ControlFlow<B, T> {
134        type Output = ControlFlow<B, U>;
135
136        fn func_map<F>(self, mut f: F) -> Self::Output
137        where
138            F: FnMut(T) -> U,
139        {
140            match self {
141                Self::Break(value) => ControlFlow::Break(value),
142                Self::Continue(value) => ControlFlow::Continue(f(value)),
143            }
144        }
145    }
146
147    impl<B, T, U> TryFuncMap<T, U, TypeParam<1>> for ControlFlow<B, T> {
148        type Output = ControlFlow<B, U>;
149
150        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
151        where
152            F: FnMut(T) -> Result<U, E>,
153        {
154            Ok(match self {
155                Self::Break(value) => ControlFlow::Break(value),
156                Self::Continue(value) => ControlFlow::Continue(f(value)?),
157            })
158        }
159    }
160}
161
162/// Implementations for [`core::option::Option`]
163mod option {
164    use crate::{FuncMap, TryFuncMap};
165
166    use core::option;
167
168    impl<A, B> FuncMap<A, B> for Option<A> {
169        type Output = Option<B>;
170
171        fn func_map<F>(self, f: F) -> Self::Output
172        where
173            F: FnMut(A) -> B,
174        {
175            self.map(f)
176        }
177    }
178
179    impl<A, B> TryFuncMap<A, B> for Option<A> {
180        type Output = Option<B>;
181
182        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
183        where
184            F: FnMut(A) -> Result<B, E>,
185        {
186            self.map(f).transpose()
187        }
188    }
189
190    impl<A, B> FuncMap<A, B> for option::IntoIter<A> {
191        type Output = option::IntoIter<B>;
192
193        fn func_map<F>(mut self, f: F) -> Self::Output
194        where
195            F: FnMut(A) -> B,
196        {
197            self.next().map(f).into_iter()
198        }
199    }
200
201    impl<A, B> TryFuncMap<A, B> for option::IntoIter<A> {
202        type Output = option::IntoIter<B>;
203
204        fn try_func_map<E, F>(mut self, f: F) -> Result<Self::Output, E>
205        where
206            F: FnMut(A) -> Result<B, E>,
207        {
208            Ok(self.next().map(f).transpose()?.into_iter())
209        }
210    }
211}
212
213/// Implementations for [`core::marker::PhantomData`]
214mod phantom_data {
215    use crate::{FuncMap, TryFuncMap};
216
217    use core::marker::PhantomData;
218
219    impl<A, B> FuncMap<A, B> for PhantomData<A> {
220        type Output = PhantomData<B>;
221
222        fn func_map<F>(self, _: F) -> Self::Output
223        where
224            F: FnMut(A) -> B,
225        {
226            PhantomData
227        }
228    }
229
230    impl<A, B> TryFuncMap<A, B> for PhantomData<A> {
231        type Output = PhantomData<B>;
232
233        fn try_func_map<E, F>(self, _: F) -> Result<Self::Output, E>
234        where
235            F: FnMut(A) -> Result<B, E>,
236        {
237            Ok(PhantomData)
238        }
239    }
240}
241
242/// Implementations for [`core::task::Poll`]
243mod poll {
244    use crate::{FuncMap, TryFuncMap};
245
246    use core::task::Poll;
247
248    impl<A, B> FuncMap<A, B> for Poll<A> {
249        type Output = Poll<B>;
250
251        fn func_map<F>(self, mut f: F) -> Self::Output
252        where
253            F: FnMut(A) -> B,
254        {
255            match self {
256                Self::Ready(value) => Poll::Ready(f(value)),
257                Self::Pending => Poll::Pending,
258            }
259        }
260    }
261
262    impl<A, B> TryFuncMap<A, B> for Poll<A> {
263        type Output = Poll<B>;
264
265        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
266        where
267            F: FnMut(A) -> Result<B, E>,
268        {
269            Ok(match self {
270                Self::Ready(value) => Poll::Ready(f(value)?),
271                Self::Pending => Poll::Pending,
272            })
273        }
274    }
275}
276
277/// Implementations for [`core::ops::Range`] etc.
278mod range {
279    use crate::{FuncMap, TryFuncMap};
280
281    use core::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
282
283    impl<A, B> FuncMap<A, B> for Range<A> {
284        type Output = Range<B>;
285
286        fn func_map<F>(self, mut f: F) -> Self::Output
287        where
288            F: FnMut(A) -> B,
289        {
290            f(self.start)..f(self.end)
291        }
292    }
293
294    impl<A, B> TryFuncMap<A, B> for Range<A> {
295        type Output = Range<B>;
296
297        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
298        where
299            F: FnMut(A) -> Result<B, E>,
300        {
301            Ok(f(self.start)?..f(self.end)?)
302        }
303    }
304
305    impl<A, B> FuncMap<A, B> for RangeFrom<A> {
306        type Output = RangeFrom<B>;
307
308        fn func_map<F>(self, mut f: F) -> Self::Output
309        where
310            F: FnMut(A) -> B,
311        {
312            f(self.start)..
313        }
314    }
315
316    impl<A, B> TryFuncMap<A, B> for RangeFrom<A> {
317        type Output = RangeFrom<B>;
318
319        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
320        where
321            F: FnMut(A) -> Result<B, E>,
322        {
323            Ok(f(self.start)?..)
324        }
325    }
326
327    impl<A, B> FuncMap<A, B> for RangeInclusive<A> {
328        type Output = RangeInclusive<B>;
329
330        fn func_map<F>(self, mut f: F) -> Self::Output
331        where
332            F: FnMut(A) -> B,
333        {
334            let (start, end) = self.into_inner();
335            f(start)..=f(end)
336        }
337    }
338
339    impl<A, B> TryFuncMap<A, B> for RangeInclusive<A> {
340        type Output = RangeInclusive<B>;
341
342        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
343        where
344            F: FnMut(A) -> Result<B, E>,
345        {
346            let (start, end) = self.into_inner();
347            Ok(f(start)?..=f(end)?)
348        }
349    }
350
351    impl<A, B> FuncMap<A, B> for RangeTo<A> {
352        type Output = RangeTo<B>;
353
354        fn func_map<F>(self, mut f: F) -> Self::Output
355        where
356            F: FnMut(A) -> B,
357        {
358            ..f(self.end)
359        }
360    }
361
362    impl<A, B> TryFuncMap<A, B> for RangeTo<A> {
363        type Output = RangeTo<B>;
364
365        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
366        where
367            F: FnMut(A) -> Result<B, E>,
368        {
369            Ok(..f(self.end)?)
370        }
371    }
372
373    impl<A, B> FuncMap<A, B> for RangeToInclusive<A> {
374        type Output = RangeToInclusive<B>;
375
376        fn func_map<F>(self, mut f: F) -> Self::Output
377        where
378            F: FnMut(A) -> B,
379        {
380            ..=f(self.end)
381        }
382    }
383
384    impl<A, B> TryFuncMap<A, B> for RangeToInclusive<A> {
385        type Output = RangeToInclusive<B>;
386
387        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
388        where
389            F: FnMut(A) -> Result<B, E>,
390        {
391            Ok(..=f(self.end)?)
392        }
393    }
394}
395
396/// Implementations for [`core::cell::RefCell`]
397mod ref_cell {
398    use crate::{FuncMap, TryFuncMap};
399
400    use core::cell::RefCell;
401
402    impl<A, B> FuncMap<A, B> for RefCell<A> {
403        type Output = RefCell<B>;
404
405        fn func_map<F>(self, mut f: F) -> Self::Output
406        where
407            F: FnMut(A) -> B,
408        {
409            f(self.into_inner()).into()
410        }
411    }
412
413    impl<A, B> TryFuncMap<A, B> for RefCell<A> {
414        type Output = RefCell<B>;
415
416        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
417        where
418            F: FnMut(A) -> Result<B, E>,
419        {
420            Ok(f(self.into_inner())?.into())
421        }
422    }
423}
424
425/// Implementations for [`core::result::Result`]
426mod result {
427    use crate::{FuncMap, TryFuncMap, TypeParam};
428
429    use core::result;
430
431    impl<A, B, U> FuncMap<A, B, TypeParam<0>> for Result<A, U> {
432        type Output = Result<B, U>;
433
434        fn func_map<F>(self, mut f: F) -> Self::Output
435        where
436            F: FnMut(A) -> B,
437        {
438            match self {
439                Ok(value) => Ok(f(value)),
440                Err(err) => Err(err),
441            }
442        }
443    }
444
445    impl<A, B, U> TryFuncMap<A, B, TypeParam<0>> for Result<A, U> {
446        type Output = Result<B, U>;
447
448        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
449        where
450            F: FnMut(A) -> Result<B, E>,
451        {
452            Ok(match self {
453                Ok(value) => Ok(f(value)?),
454                Err(err) => Err(err),
455            })
456        }
457    }
458
459    impl<T, A, B> FuncMap<A, B, TypeParam<1>> for Result<T, A> {
460        type Output = Result<T, B>;
461
462        fn func_map<F>(self, mut f: F) -> Self::Output
463        where
464            F: FnMut(A) -> B,
465        {
466            match self {
467                Ok(value) => Ok(value),
468                Err(err) => Err(f(err)),
469            }
470        }
471    }
472
473    impl<T, A, B> TryFuncMap<A, B, TypeParam<1>> for Result<T, A> {
474        type Output = Result<T, B>;
475
476        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
477        where
478            F: FnMut(A) -> Result<B, E>,
479        {
480            Ok(match self {
481                Ok(value) => Ok(value),
482                Err(err) => Err(f(err)?),
483            })
484        }
485    }
486
487    impl<A, B> FuncMap<A, B> for result::IntoIter<A> {
488        type Output = result::IntoIter<B>;
489
490        fn func_map<F>(mut self, f: F) -> Self::Output
491        where
492            F: FnMut(A) -> B,
493        {
494            self.next().map(f).ok_or(()).into_iter()
495        }
496    }
497
498    impl<A, B> TryFuncMap<A, B> for result::IntoIter<A> {
499        type Output = result::IntoIter<B>;
500
501        fn try_func_map<E, F>(mut self, f: F) -> Result<Self::Output, E>
502        where
503            F: FnMut(A) -> Result<B, E>,
504        {
505            Ok(self.next().map(f).transpose()?.ok_or(()).into_iter())
506        }
507    }
508}
509
510/// Implementations for [`core::cell::UnsafeCell`]
511mod unsafe_cell {
512    use crate::{FuncMap, TryFuncMap};
513
514    use core::cell::UnsafeCell;
515
516    impl<A, B> FuncMap<A, B> for UnsafeCell<A> {
517        type Output = UnsafeCell<B>;
518
519        fn func_map<F>(self, mut f: F) -> Self::Output
520        where
521            F: FnMut(A) -> B,
522        {
523            f(self.into_inner()).into()
524        }
525    }
526
527    impl<A, B> TryFuncMap<A, B> for UnsafeCell<A> {
528        type Output = UnsafeCell<B>;
529
530        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
531        where
532            F: FnMut(A) -> Result<B, E>,
533        {
534            Ok(f(self.into_inner())?.into())
535        }
536    }
537}