• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::fmt::{self, Debug};
2 use core::marker::PhantomData;
3 
4 pub trait FnOnce1<A> {
5     type Output;
call_once(self, arg: A) -> Self::Output6     fn call_once(self, arg: A) -> Self::Output;
7 }
8 
9 impl<T, A, R> FnOnce1<A> for T
10 where
11     T: FnOnce(A) -> R,
12 {
13     type Output = R;
call_once(self, arg: A) -> R14     fn call_once(self, arg: A) -> R {
15         self(arg)
16     }
17 }
18 
19 pub trait FnMut1<A>: FnOnce1<A> {
call_mut(&mut self, arg: A) -> Self::Output20     fn call_mut(&mut self, arg: A) -> Self::Output;
21 }
22 
23 impl<T, A, R> FnMut1<A> for T
24 where
25     T: FnMut(A) -> R,
26 {
call_mut(&mut self, arg: A) -> R27     fn call_mut(&mut self, arg: A) -> R {
28         self(arg)
29     }
30 }
31 
32 // Not used, but present for completeness
33 #[allow(unreachable_pub)]
34 pub trait Fn1<A>: FnMut1<A> {
call(&self, arg: A) -> Self::Output35     fn call(&self, arg: A) -> Self::Output;
36 }
37 
38 impl<T, A, R> Fn1<A> for T
39 where
40     T: Fn(A) -> R,
41 {
call(&self, arg: A) -> R42     fn call(&self, arg: A) -> R {
43         self(arg)
44     }
45 }
46 
47 macro_rules! trivial_fn_impls {
48     ($name:ident <$($arg:ident),*> $t:ty = $debug:literal) => {
49         impl<$($arg),*> Copy for $t {}
50         impl<$($arg),*> Clone for $t {
51             fn clone(&self) -> Self { *self }
52         }
53         impl<$($arg),*> Debug for $t {
54             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55                 f.write_str($debug)
56             }
57         }
58         impl<$($arg,)* A> FnMut1<A> for $t where Self: FnOnce1<A> {
59             fn call_mut(&mut self, arg: A) -> Self::Output {
60                 self.call_once(arg)
61             }
62         }
63         impl<$($arg,)* A> Fn1<A> for $t where Self: FnOnce1<A> {
64             fn call(&self, arg: A) -> Self::Output {
65                 self.call_once(arg)
66             }
67         }
68         pub(crate) fn $name<$($arg),*>() -> $t {
69             Default::default()
70         }
71     }
72 }
73 
74 pub struct OkFn<E>(PhantomData<fn(E)>);
75 
76 impl<E> Default for OkFn<E> {
default() -> Self77     fn default() -> Self {
78         Self(PhantomData)
79     }
80 }
81 
82 impl<A, E> FnOnce1<A> for OkFn<E> {
83     type Output = Result<A, E>;
call_once(self, arg: A) -> Self::Output84     fn call_once(self, arg: A) -> Self::Output {
85         Ok(arg)
86     }
87 }
88 
89 trivial_fn_impls!(ok_fn <T> OkFn<T> = "Ok");
90 
91 #[derive(Debug, Copy, Clone, Default)]
92 pub struct ChainFn<F, G>(F, G);
93 
94 impl<F, G, A> FnOnce1<A> for ChainFn<F, G>
95 where
96     F: FnOnce1<A>,
97     G: FnOnce1<F::Output>,
98 {
99     type Output = G::Output;
call_once(self, arg: A) -> Self::Output100     fn call_once(self, arg: A) -> Self::Output {
101         self.1.call_once(self.0.call_once(arg))
102     }
103 }
104 impl<F, G, A> FnMut1<A> for ChainFn<F, G>
105 where
106     F: FnMut1<A>,
107     G: FnMut1<F::Output>,
108 {
call_mut(&mut self, arg: A) -> Self::Output109     fn call_mut(&mut self, arg: A) -> Self::Output {
110         self.1.call_mut(self.0.call_mut(arg))
111     }
112 }
113 impl<F, G, A> Fn1<A> for ChainFn<F, G>
114 where
115     F: Fn1<A>,
116     G: Fn1<F::Output>,
117 {
call(&self, arg: A) -> Self::Output118     fn call(&self, arg: A) -> Self::Output {
119         self.1.call(self.0.call(arg))
120     }
121 }
chain_fn<F, G>(f: F, g: G) -> ChainFn<F, G>122 pub(crate) fn chain_fn<F, G>(f: F, g: G) -> ChainFn<F, G> {
123     ChainFn(f, g)
124 }
125 
126 #[derive(Default)]
127 pub struct MergeResultFn;
128 
129 impl<T> FnOnce1<Result<T, T>> for MergeResultFn {
130     type Output = T;
call_once(self, arg: Result<T, T>) -> Self::Output131     fn call_once(self, arg: Result<T, T>) -> Self::Output {
132         match arg {
133             Ok(x) => x,
134             Err(x) => x,
135         }
136     }
137 }
138 trivial_fn_impls!(merge_result_fn <> MergeResultFn = "merge_result");
139 
140 #[derive(Debug, Copy, Clone, Default)]
141 pub struct InspectFn<F>(F);
142 
143 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
144 impl<F, A> FnOnce1<A> for InspectFn<F>
145 where
146     F: for<'a> FnOnce1<&'a A, Output = ()>,
147 {
148     type Output = A;
call_once(self, arg: A) -> Self::Output149     fn call_once(self, arg: A) -> Self::Output {
150         self.0.call_once(&arg);
151         arg
152     }
153 }
154 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
155 impl<F, A> FnMut1<A> for InspectFn<F>
156 where
157     F: for<'a> FnMut1<&'a A, Output = ()>,
158 {
call_mut(&mut self, arg: A) -> Self::Output159     fn call_mut(&mut self, arg: A) -> Self::Output {
160         self.0.call_mut(&arg);
161         arg
162     }
163 }
164 #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
165 impl<F, A> Fn1<A> for InspectFn<F>
166 where
167     F: for<'a> Fn1<&'a A, Output = ()>,
168 {
call(&self, arg: A) -> Self::Output169     fn call(&self, arg: A) -> Self::Output {
170         self.0.call(&arg);
171         arg
172     }
173 }
inspect_fn<F>(f: F) -> InspectFn<F>174 pub(crate) fn inspect_fn<F>(f: F) -> InspectFn<F> {
175     InspectFn(f)
176 }
177 
178 #[derive(Debug, Copy, Clone, Default)]
179 pub struct MapOkFn<F>(F);
180 
181 impl<F, T, E> FnOnce1<Result<T, E>> for MapOkFn<F>
182 where
183     F: FnOnce1<T>,
184 {
185     type Output = Result<F::Output, E>;
call_once(self, arg: Result<T, E>) -> Self::Output186     fn call_once(self, arg: Result<T, E>) -> Self::Output {
187         arg.map(|x| self.0.call_once(x))
188     }
189 }
190 impl<F, T, E> FnMut1<Result<T, E>> for MapOkFn<F>
191 where
192     F: FnMut1<T>,
193 {
call_mut(&mut self, arg: Result<T, E>) -> Self::Output194     fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
195         arg.map(|x| self.0.call_mut(x))
196     }
197 }
198 impl<F, T, E> Fn1<Result<T, E>> for MapOkFn<F>
199 where
200     F: Fn1<T>,
201 {
call(&self, arg: Result<T, E>) -> Self::Output202     fn call(&self, arg: Result<T, E>) -> Self::Output {
203         arg.map(|x| self.0.call(x))
204     }
205 }
map_ok_fn<F>(f: F) -> MapOkFn<F>206 pub(crate) fn map_ok_fn<F>(f: F) -> MapOkFn<F> {
207     MapOkFn(f)
208 }
209 
210 #[derive(Debug, Copy, Clone, Default)]
211 pub struct MapErrFn<F>(F);
212 
213 impl<F, T, E> FnOnce1<Result<T, E>> for MapErrFn<F>
214 where
215     F: FnOnce1<E>,
216 {
217     type Output = Result<T, F::Output>;
call_once(self, arg: Result<T, E>) -> Self::Output218     fn call_once(self, arg: Result<T, E>) -> Self::Output {
219         arg.map_err(|x| self.0.call_once(x))
220     }
221 }
222 impl<F, T, E> FnMut1<Result<T, E>> for MapErrFn<F>
223 where
224     F: FnMut1<E>,
225 {
call_mut(&mut self, arg: Result<T, E>) -> Self::Output226     fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
227         arg.map_err(|x| self.0.call_mut(x))
228     }
229 }
230 impl<F, T, E> Fn1<Result<T, E>> for MapErrFn<F>
231 where
232     F: Fn1<E>,
233 {
call(&self, arg: Result<T, E>) -> Self::Output234     fn call(&self, arg: Result<T, E>) -> Self::Output {
235         arg.map_err(|x| self.0.call(x))
236     }
237 }
map_err_fn<F>(f: F) -> MapErrFn<F>238 pub(crate) fn map_err_fn<F>(f: F) -> MapErrFn<F> {
239     MapErrFn(f)
240 }
241 
242 #[derive(Debug, Copy, Clone)]
243 pub struct InspectOkFn<F>(F);
244 
245 impl<'a, F, T, E> FnOnce1<&'a Result<T, E>> for InspectOkFn<F>
246 where
247     F: FnOnce1<&'a T, Output = ()>,
248 {
249     type Output = ();
call_once(self, arg: &'a Result<T, E>) -> Self::Output250     fn call_once(self, arg: &'a Result<T, E>) -> Self::Output {
251         if let Ok(x) = arg {
252             self.0.call_once(x)
253         }
254     }
255 }
256 impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectOkFn<F>
257 where
258     F: FnMut1<&'a T, Output = ()>,
259 {
call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output260     fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output {
261         if let Ok(x) = arg {
262             self.0.call_mut(x)
263         }
264     }
265 }
266 impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectOkFn<F>
267 where
268     F: Fn1<&'a T, Output = ()>,
269 {
call(&self, arg: &'a Result<T, E>) -> Self::Output270     fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
271         if let Ok(x) = arg {
272             self.0.call(x)
273         }
274     }
275 }
inspect_ok_fn<F>(f: F) -> InspectOkFn<F>276 pub(crate) fn inspect_ok_fn<F>(f: F) -> InspectOkFn<F> {
277     InspectOkFn(f)
278 }
279 
280 #[derive(Debug, Copy, Clone)]
281 pub struct InspectErrFn<F>(F);
282 
283 impl<'a, F, T, E> FnOnce1<&'a Result<T, E>> for InspectErrFn<F>
284 where
285     F: FnOnce1<&'a E, Output = ()>,
286 {
287     type Output = ();
call_once(self, arg: &'a Result<T, E>) -> Self::Output288     fn call_once(self, arg: &'a Result<T, E>) -> Self::Output {
289         if let Err(x) = arg {
290             self.0.call_once(x)
291         }
292     }
293 }
294 impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectErrFn<F>
295 where
296     F: FnMut1<&'a E, Output = ()>,
297 {
call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output298     fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output {
299         if let Err(x) = arg {
300             self.0.call_mut(x)
301         }
302     }
303 }
304 impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectErrFn<F>
305 where
306     F: Fn1<&'a E, Output = ()>,
307 {
call(&self, arg: &'a Result<T, E>) -> Self::Output308     fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
309         if let Err(x) = arg {
310             self.0.call(x)
311         }
312     }
313 }
inspect_err_fn<F>(f: F) -> InspectErrFn<F>314 pub(crate) fn inspect_err_fn<F>(f: F) -> InspectErrFn<F> {
315     InspectErrFn(f)
316 }
317 
318 pub(crate) type MapOkOrElseFn<F, G> = ChainFn<MapOkFn<F>, ChainFn<MapErrFn<G>, MergeResultFn>>;
map_ok_or_else_fn<F, G>(f: F, g: G) -> MapOkOrElseFn<F, G>319 pub(crate) fn map_ok_or_else_fn<F, G>(f: F, g: G) -> MapOkOrElseFn<F, G> {
320     chain_fn(map_ok_fn(f), chain_fn(map_err_fn(g), merge_result_fn()))
321 }
322 
323 #[derive(Debug, Copy, Clone, Default)]
324 pub struct UnwrapOrElseFn<F>(F);
325 
326 impl<F, T, E> FnOnce1<Result<T, E>> for UnwrapOrElseFn<F>
327 where
328     F: FnOnce1<E, Output = T>,
329 {
330     type Output = T;
call_once(self, arg: Result<T, E>) -> Self::Output331     fn call_once(self, arg: Result<T, E>) -> Self::Output {
332         arg.unwrap_or_else(|x| self.0.call_once(x))
333     }
334 }
335 impl<F, T, E> FnMut1<Result<T, E>> for UnwrapOrElseFn<F>
336 where
337     F: FnMut1<E, Output = T>,
338 {
call_mut(&mut self, arg: Result<T, E>) -> Self::Output339     fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
340         arg.unwrap_or_else(|x| self.0.call_mut(x))
341     }
342 }
343 impl<F, T, E> Fn1<Result<T, E>> for UnwrapOrElseFn<F>
344 where
345     F: Fn1<E, Output = T>,
346 {
call(&self, arg: Result<T, E>) -> Self::Output347     fn call(&self, arg: Result<T, E>) -> Self::Output {
348         arg.unwrap_or_else(|x| self.0.call(x))
349     }
350 }
unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F>351 pub(crate) fn unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F> {
352     UnwrapOrElseFn(f)
353 }
354 
355 pub struct IntoFn<T>(PhantomData<fn() -> T>);
356 
357 impl<T> Default for IntoFn<T> {
default() -> Self358     fn default() -> Self {
359         Self(PhantomData)
360     }
361 }
362 impl<A, T> FnOnce1<A> for IntoFn<T>
363 where
364     A: Into<T>,
365 {
366     type Output = T;
call_once(self, arg: A) -> Self::Output367     fn call_once(self, arg: A) -> Self::Output {
368         arg.into()
369     }
370 }
371 
372 trivial_fn_impls!(into_fn <T> IntoFn<T> = "Into::into");
373