Skip to main content

funcmap/
impls_alloc.rs

1//! Implementations of [`FuncMap`](crate::FuncMap) and
2//! [`TryFuncMap`](crate::TryFuncMap) for types in [`alloc`]
3
4#![allow(clippy::mismatching_type_param_order)]
5
6/// Implementations for types in [`alloc::collections::binary_heap`]
7mod binary_heap {
8    extern crate alloc;
9
10    use crate::{FuncMap, TryFuncMap};
11
12    use alloc::collections::{binary_heap, BinaryHeap};
13
14    impl<A, B> FuncMap<A, B> for BinaryHeap<A>
15    where
16        B: Ord,
17    {
18        type Output = BinaryHeap<B>;
19
20        fn func_map<F>(self, f: F) -> Self::Output
21        where
22            F: FnMut(A) -> B,
23        {
24            self.into_iter().map(f).collect()
25        }
26    }
27
28    impl<A, B> TryFuncMap<A, B> for BinaryHeap<A>
29    where
30        B: Ord,
31    {
32        type Output = BinaryHeap<B>;
33
34        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
35        where
36            F: FnMut(A) -> Result<B, E>,
37        {
38            self.into_iter().map(f).collect()
39        }
40    }
41
42    impl<A, B> FuncMap<A, B> for binary_heap::IntoIter<A>
43    where
44        B: Ord,
45    {
46        type Output = binary_heap::IntoIter<B>;
47
48        fn func_map<F>(self, f: F) -> Self::Output
49        where
50            F: FnMut(A) -> B,
51        {
52            self.map(f).collect::<BinaryHeap<_>>().into_iter()
53        }
54    }
55
56    impl<A, B> TryFuncMap<A, B> for binary_heap::IntoIter<A>
57    where
58        B: Ord,
59    {
60        type Output = binary_heap::IntoIter<B>;
61
62        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
63        where
64            F: FnMut(A) -> Result<B, E>,
65        {
66            self.map(f)
67                .collect::<Result<BinaryHeap<_>, _>>()
68                .map(IntoIterator::into_iter)
69        }
70    }
71}
72
73/// Implementations for types in [`alloc::boxed`]
74mod boxed {
75    use crate::{FuncMap, TryFuncMap};
76
77    impl<A, B> FuncMap<A, B> for Box<A> {
78        type Output = Box<B>;
79
80        fn func_map<F>(self, mut f: F) -> Self::Output
81        where
82            F: FnMut(A) -> B,
83        {
84            f(*self).into()
85        }
86    }
87
88    impl<A, B> TryFuncMap<A, B> for Box<A> {
89        type Output = Box<B>;
90
91        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
92        where
93            F: FnMut(A) -> Result<B, E>,
94        {
95            Ok(f(*self)?.into())
96        }
97    }
98}
99
100/// Implementations for types in [`alloc::collections::btree_map`]
101mod btree_map {
102    extern crate alloc;
103
104    use crate::{FuncMap, TryFuncMap, TypeParam};
105
106    use alloc::collections::{btree_map, BTreeMap};
107
108    impl<A, B, V> FuncMap<A, B, TypeParam<0>> for BTreeMap<A, V>
109    where
110        B: Ord,
111    {
112        type Output = BTreeMap<B, V>;
113
114        fn func_map<F>(self, mut f: F) -> Self::Output
115        where
116            F: FnMut(A) -> B,
117        {
118            self.into_iter().map(|(k, v)| (f(k), v)).collect()
119        }
120    }
121
122    impl<A, B, V> TryFuncMap<A, B, TypeParam<0>> for BTreeMap<A, V>
123    where
124        B: Ord,
125    {
126        type Output = BTreeMap<B, V>;
127
128        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
129        where
130            F: FnMut(A) -> Result<B, E>,
131        {
132            self.into_iter().map(|(k, v)| Ok((f(k)?, v))).collect()
133        }
134    }
135
136    impl<K, A, B> FuncMap<A, B, TypeParam<1>> for BTreeMap<K, A>
137    where
138        K: Ord,
139    {
140        type Output = BTreeMap<K, B>;
141
142        fn func_map<F>(self, mut f: F) -> Self::Output
143        where
144            F: FnMut(A) -> B,
145        {
146            self.into_iter().map(|(k, v)| (k, f(v))).collect()
147        }
148    }
149
150    impl<K, A, B> TryFuncMap<A, B, TypeParam<1>> for BTreeMap<K, A>
151    where
152        K: Ord,
153    {
154        type Output = BTreeMap<K, B>;
155
156        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
157        where
158            F: FnMut(A) -> Result<B, E>,
159        {
160            self.into_iter().map(|(k, v)| Ok((k, f(v)?))).collect()
161        }
162    }
163
164    impl<A, B, V> FuncMap<A, B, TypeParam<0>> for btree_map::IntoIter<A, V>
165    where
166        B: Ord,
167    {
168        type Output = btree_map::IntoIter<B, V>;
169
170        fn func_map<F>(self, mut f: F) -> Self::Output
171        where
172            F: FnMut(A) -> B,
173        {
174            self.map(|(k, v)| (f(k), v))
175                .collect::<BTreeMap<_, _>>()
176                .into_iter()
177        }
178    }
179
180    impl<A, B, V> TryFuncMap<A, B, TypeParam<0>> for btree_map::IntoIter<A, V>
181    where
182        B: Ord,
183    {
184        type Output = btree_map::IntoIter<B, V>;
185
186        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
187        where
188            F: FnMut(A) -> Result<B, E>,
189        {
190            self.map(|(k, v)| Ok((f(k)?, v)))
191                .collect::<Result<BTreeMap<_, _>, _>>()
192                .map(IntoIterator::into_iter)
193        }
194    }
195
196    impl<K, A, B> FuncMap<A, B, TypeParam<1>> for btree_map::IntoIter<K, A>
197    where
198        K: Ord,
199    {
200        type Output = btree_map::IntoIter<K, B>;
201
202        fn func_map<F>(self, mut f: F) -> Self::Output
203        where
204            F: FnMut(A) -> B,
205        {
206            self.map(|(k, v)| (k, f(v)))
207                .collect::<BTreeMap<_, _>>()
208                .into_iter()
209        }
210    }
211
212    impl<K, A, B> TryFuncMap<A, B, TypeParam<1>> for btree_map::IntoIter<K, A>
213    where
214        K: Ord,
215    {
216        type Output = btree_map::IntoIter<K, B>;
217
218        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
219        where
220            F: FnMut(A) -> Result<B, E>,
221        {
222            self.map(|(k, v)| Ok((k, f(v)?)))
223                .collect::<Result<BTreeMap<_, _>, _>>()
224                .map(IntoIterator::into_iter)
225        }
226    }
227}
228
229/// Implementations for types in [`alloc::collections::btree_set`]
230mod btree_set {
231    extern crate alloc;
232
233    use crate::{FuncMap, TryFuncMap};
234
235    use alloc::collections::{btree_set, BTreeSet};
236
237    impl<A, B> FuncMap<A, B> for BTreeSet<A>
238    where
239        B: Ord,
240    {
241        type Output = BTreeSet<B>;
242
243        fn func_map<F>(self, f: F) -> Self::Output
244        where
245            F: FnMut(A) -> B,
246        {
247            self.into_iter().map(f).collect()
248        }
249    }
250
251    impl<A, B> TryFuncMap<A, B> for BTreeSet<A>
252    where
253        B: Ord,
254    {
255        type Output = BTreeSet<B>;
256
257        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
258        where
259            F: FnMut(A) -> Result<B, E>,
260        {
261            self.into_iter().map(f).collect()
262        }
263    }
264
265    impl<A, B> FuncMap<A, B> for btree_set::IntoIter<A>
266    where
267        B: Ord,
268    {
269        type Output = btree_set::IntoIter<B>;
270
271        fn func_map<F>(self, f: F) -> Self::Output
272        where
273            F: FnMut(A) -> B,
274        {
275            self.map(f).collect::<BTreeSet<_>>().into_iter()
276        }
277    }
278
279    impl<A, B> TryFuncMap<A, B> for btree_set::IntoIter<A>
280    where
281        B: Ord,
282    {
283        type Output = btree_set::IntoIter<B>;
284
285        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
286        where
287            F: FnMut(A) -> Result<B, E>,
288        {
289            self.map(f)
290                .collect::<Result<BTreeSet<_>, _>>()
291                .map(IntoIterator::into_iter)
292        }
293    }
294}
295
296/// Implementations for types in [`alloc::collections::linked_list`]
297mod linked_list {
298    extern crate alloc;
299
300    use crate::{FuncMap, TryFuncMap};
301
302    use alloc::collections::{linked_list, LinkedList};
303
304    impl<A, B> FuncMap<A, B> for LinkedList<A> {
305        type Output = LinkedList<B>;
306
307        fn func_map<F>(self, f: F) -> Self::Output
308        where
309            F: FnMut(A) -> B,
310        {
311            self.into_iter().map(f).collect()
312        }
313    }
314
315    impl<A, B> TryFuncMap<A, B> for LinkedList<A> {
316        type Output = LinkedList<B>;
317
318        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
319        where
320            F: FnMut(A) -> Result<B, E>,
321        {
322            self.into_iter().map(f).collect()
323        }
324    }
325
326    impl<A, B> FuncMap<A, B> for linked_list::IntoIter<A> {
327        type Output = linked_list::IntoIter<B>;
328
329        fn func_map<F>(self, f: F) -> Self::Output
330        where
331            F: FnMut(A) -> B,
332        {
333            self.map(f).collect::<LinkedList<_>>().into_iter()
334        }
335    }
336
337    impl<A, B> TryFuncMap<A, B> for linked_list::IntoIter<A> {
338        type Output = linked_list::IntoIter<B>;
339
340        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
341        where
342            F: FnMut(A) -> Result<B, E>,
343        {
344            self.map(f)
345                .collect::<Result<LinkedList<_>, _>>()
346                .map(IntoIterator::into_iter)
347        }
348    }
349}
350
351/// Implementations for types in [`alloc::vec`](mod@alloc::vec)
352mod vec {
353    extern crate alloc;
354
355    use crate::{FuncMap, TryFuncMap};
356
357    use alloc::vec;
358
359    impl<A, B> FuncMap<A, B> for Vec<A> {
360        type Output = Vec<B>;
361
362        fn func_map<F>(self, f: F) -> Self::Output
363        where
364            F: FnMut(A) -> B,
365        {
366            self.into_iter().map(f).collect()
367        }
368    }
369
370    impl<A, B> TryFuncMap<A, B> for Vec<A> {
371        type Output = Vec<B>;
372
373        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
374        where
375            F: FnMut(A) -> Result<B, E>,
376        {
377            self.into_iter().map(f).collect()
378        }
379    }
380
381    impl<A, B> FuncMap<A, B> for vec::IntoIter<A> {
382        type Output = vec::IntoIter<B>;
383
384        fn func_map<F>(self, f: F) -> Self::Output
385        where
386            F: FnMut(A) -> B,
387        {
388            self.map(f).collect::<Vec<_>>().into_iter()
389        }
390    }
391
392    impl<A, B> TryFuncMap<A, B> for vec::IntoIter<A> {
393        type Output = vec::IntoIter<B>;
394
395        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
396        where
397            F: FnMut(A) -> Result<B, E>,
398        {
399            self.map(f)
400                .collect::<Result<Vec<_>, _>>()
401                .map(IntoIterator::into_iter)
402        }
403    }
404}
405
406/// Implementations for types in [`alloc::collections::vec_deque`]
407mod vec_deque {
408    extern crate alloc;
409
410    use crate::{FuncMap, TryFuncMap};
411
412    use alloc::collections::{vec_deque, VecDeque};
413
414    impl<A, B> FuncMap<A, B> for VecDeque<A> {
415        type Output = VecDeque<B>;
416
417        fn func_map<F>(self, f: F) -> Self::Output
418        where
419            F: FnMut(A) -> B,
420        {
421            self.into_iter().map(f).collect()
422        }
423    }
424
425    impl<A, B> TryFuncMap<A, B> for VecDeque<A> {
426        type Output = VecDeque<B>;
427
428        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
429        where
430            F: FnMut(A) -> Result<B, E>,
431        {
432            self.into_iter().map(f).collect()
433        }
434    }
435
436    impl<A, B> FuncMap<A, B> for vec_deque::IntoIter<A> {
437        type Output = vec_deque::IntoIter<B>;
438
439        fn func_map<F>(self, f: F) -> Self::Output
440        where
441            F: FnMut(A) -> B,
442        {
443            self.map(f).collect::<VecDeque<_>>().into_iter()
444        }
445    }
446
447    impl<A, B> TryFuncMap<A, B> for vec_deque::IntoIter<A> {
448        type Output = vec_deque::IntoIter<B>;
449
450        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
451        where
452            F: FnMut(A) -> Result<B, E>,
453        {
454            self.map(f)
455                .collect::<Result<VecDeque<_>, _>>()
456                .map(IntoIterator::into_iter)
457        }
458    }
459}