funcmap/
impls_std.rs

1//! Implementations of [`FuncMap`](crate::FuncMap) and
2//! [`TryFuncMap`](crate::TryFuncMap) for types in [`std`]
3
4#![allow(clippy::mismatching_type_param_order)]
5
6/// Implementations for types in [`std::collections::hash_map`]
7mod hash_map {
8    use crate::{FuncMap, TryFuncMap, TypeParam};
9
10    use core::hash::Hash;
11    use std::collections::{hash_map, HashMap};
12
13    impl<A, B, V, S> FuncMap<A, B, TypeParam<0>> for HashMap<A, V, S>
14    where
15        B: Eq + Hash,
16    {
17        type Output = HashMap<B, V>;
18
19        fn func_map<F>(self, mut f: F) -> Self::Output
20        where
21            F: FnMut(A) -> B,
22        {
23            self.into_iter().map(|(k, v)| (f(k), v)).collect()
24        }
25    }
26
27    impl<A, B, V, S> TryFuncMap<A, B, TypeParam<0>> for HashMap<A, V, S>
28    where
29        B: Eq + Hash,
30    {
31        type Output = HashMap<B, V>;
32
33        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
34        where
35            F: FnMut(A) -> Result<B, E>,
36        {
37            self.into_iter().map(|(k, v)| Ok((f(k)?, v))).collect()
38        }
39    }
40
41    impl<K, A, B, S> FuncMap<A, B, TypeParam<1>> for HashMap<K, A, S>
42    where
43        K: Eq + Hash,
44    {
45        type Output = HashMap<K, B>;
46
47        fn func_map<F>(self, mut f: F) -> Self::Output
48        where
49            F: FnMut(A) -> B,
50        {
51            self.into_iter().map(|(k, v)| (k, f(v))).collect()
52        }
53    }
54
55    impl<K, A, B, S> TryFuncMap<A, B, TypeParam<1>> for HashMap<K, A, S>
56    where
57        K: Eq + Hash,
58    {
59        type Output = HashMap<K, B>;
60
61        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
62        where
63            F: FnMut(A) -> Result<B, E>,
64        {
65            self.into_iter().map(|(k, v)| Ok((k, f(v)?))).collect()
66        }
67    }
68
69    impl<A, B, V> FuncMap<A, B, TypeParam<0>> for hash_map::IntoIter<A, V>
70    where
71        B: Eq + Hash,
72    {
73        type Output = hash_map::IntoIter<B, V>;
74
75        fn func_map<F>(self, mut f: F) -> Self::Output
76        where
77            F: FnMut(A) -> B,
78        {
79            self.map(|(k, v)| (f(k), v))
80                .collect::<HashMap<_, _>>()
81                .into_iter()
82        }
83    }
84
85    impl<A, B, V> TryFuncMap<A, B, TypeParam<0>> for hash_map::IntoIter<A, V>
86    where
87        B: Eq + Hash,
88    {
89        type Output = hash_map::IntoIter<B, V>;
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            self.map(|(k, v)| Ok((f(k)?, v)))
96                .collect::<Result<HashMap<_, _>, _>>()
97                .map(IntoIterator::into_iter)
98        }
99    }
100
101    impl<K, A, B> FuncMap<A, B, TypeParam<1>> for hash_map::IntoIter<K, A>
102    where
103        K: Eq + Hash,
104    {
105        type Output = hash_map::IntoIter<K, B>;
106
107        fn func_map<F>(self, mut f: F) -> Self::Output
108        where
109            F: FnMut(A) -> B,
110        {
111            self.map(|(k, v)| (k, f(v)))
112                .collect::<HashMap<_, _>>()
113                .into_iter()
114        }
115    }
116
117    impl<K, A, B> TryFuncMap<A, B, TypeParam<1>> for hash_map::IntoIter<K, A>
118    where
119        K: Eq + Hash,
120    {
121        type Output = hash_map::IntoIter<K, B>;
122
123        fn try_func_map<E, F>(self, mut f: F) -> Result<Self::Output, E>
124        where
125            F: FnMut(A) -> Result<B, E>,
126        {
127            self.map(|(k, v)| Ok((k, f(v)?)))
128                .collect::<Result<HashMap<_, _>, _>>()
129                .map(IntoIterator::into_iter)
130        }
131    }
132}
133
134/// Implementations for types in [`std::collections::hash_set`]
135mod hash_set {
136    use crate::{FuncMap, TryFuncMap};
137
138    use core::hash::Hash;
139    use std::collections::{hash_set, HashSet};
140
141    impl<A, B, S> FuncMap<A, B> for HashSet<A, S>
142    where
143        B: Eq + Hash,
144    {
145        type Output = HashSet<B>;
146
147        fn func_map<F>(self, f: F) -> Self::Output
148        where
149            F: FnMut(A) -> B,
150        {
151            self.into_iter().map(f).collect()
152        }
153    }
154
155    impl<A, B, S> TryFuncMap<A, B> for HashSet<A, S>
156    where
157        B: Eq + Hash,
158    {
159        type Output = HashSet<B>;
160
161        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
162        where
163            F: FnMut(A) -> Result<B, E>,
164        {
165            self.into_iter().map(f).collect()
166        }
167    }
168
169    impl<A, B> FuncMap<A, B> for hash_set::IntoIter<A>
170    where
171        B: Eq + Hash,
172    {
173        type Output = hash_set::IntoIter<B>;
174
175        fn func_map<F>(self, f: F) -> Self::Output
176        where
177            F: FnMut(A) -> B,
178        {
179            self.map(f).collect::<HashSet<_>>().into_iter()
180        }
181    }
182
183    impl<A, B> TryFuncMap<A, B> for hash_set::IntoIter<A>
184    where
185        B: Eq + Hash,
186    {
187        type Output = hash_set::IntoIter<B>;
188
189        fn try_func_map<E, F>(self, f: F) -> Result<Self::Output, E>
190        where
191            F: FnMut(A) -> Result<B, E>,
192        {
193            self.map(f)
194                .collect::<Result<HashSet<_>, _>>()
195                .map(IntoIterator::into_iter)
196        }
197    }
198}