• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::marker::PhantomData;
2 use core::fmt::{self, Debug};
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 { self.0.call_once(x) }
252     }
253 }
254 impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectOkFn<F>
255 where
256     F: FnMut1<&'a T, Output=()>,
257 {
call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output258     fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output {
259         if let Ok(x) = arg { self.0.call_mut(x) }
260     }
261 }
262 impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectOkFn<F>
263 where
264     F: Fn1<&'a T, Output=()>,
265 {
call(&self, arg: &'a Result<T, E>) -> Self::Output266     fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
267         if let Ok(x) = arg { self.0.call(x) }
268     }
269 }
inspect_ok_fn<F>(f: F) -> InspectOkFn<F>270 pub(crate) fn inspect_ok_fn<F>(f: F) -> InspectOkFn<F> {
271     InspectOkFn(f)
272 }
273 
274 #[derive(Debug, Copy, Clone)]
275 pub struct InspectErrFn<F>(F);
276 
277 impl<'a, F, T, E> FnOnce1<&'a Result<T, E>> for InspectErrFn<F>
278 where
279     F: FnOnce1<&'a E, Output=()>
280 {
281     type Output = ();
call_once(self, arg: &'a Result<T, E>) -> Self::Output282     fn call_once(self, arg: &'a Result<T, E>) -> Self::Output {
283         if let Err(x) = arg { self.0.call_once(x) }
284     }
285 }
286 impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectErrFn<F>
287 where
288     F: FnMut1<&'a E, Output=()>,
289 {
call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output290     fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output {
291         if let Err(x) = arg { self.0.call_mut(x) }
292     }
293 }
294 impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectErrFn<F>
295 where
296     F: Fn1<&'a E, Output=()>,
297 {
call(&self, arg: &'a Result<T, E>) -> Self::Output298     fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
299         if let Err(x) = arg { self.0.call(x) }
300     }
301 }
inspect_err_fn<F>(f: F) -> InspectErrFn<F>302 pub(crate) fn inspect_err_fn<F>(f: F) -> InspectErrFn<F> {
303     InspectErrFn(f)
304 }
305 
306 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>307 pub(crate) fn map_ok_or_else_fn<F, G>(f: F, g: G) -> MapOkOrElseFn<F, G> {
308     chain_fn(map_ok_fn(f), chain_fn(map_err_fn(g), merge_result_fn()))
309 }
310 
311 #[derive(Debug, Copy, Clone, Default)]
312 pub struct UnwrapOrElseFn<F>(F);
313 
314 impl<F, T, E> FnOnce1<Result<T, E>> for UnwrapOrElseFn<F>
315 where
316     F: FnOnce1<E, Output=T>,
317 {
318     type Output = T;
call_once(self, arg: Result<T, E>) -> Self::Output319     fn call_once(self, arg: Result<T, E>) -> Self::Output {
320         arg.unwrap_or_else(|x| self.0.call_once(x))
321     }
322 }
323 impl<F, T, E> FnMut1<Result<T, E>> for UnwrapOrElseFn<F>
324 where
325     F: FnMut1<E, Output=T>,
326 {
call_mut(&mut self, arg: Result<T, E>) -> Self::Output327     fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
328         arg.unwrap_or_else(|x| self.0.call_mut(x))
329     }
330 }
331 impl<F, T, E> Fn1<Result<T, E>> for UnwrapOrElseFn<F>
332 where
333     F: Fn1<E, Output=T>,
334 {
call(&self, arg: Result<T, E>) -> Self::Output335     fn call(&self, arg: Result<T, E>) -> Self::Output {
336         arg.unwrap_or_else(|x| self.0.call(x))
337     }
338 }
unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F>339 pub(crate) fn unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F> {
340     UnwrapOrElseFn(f)
341 }
342 
343 pub struct IntoFn<T>(PhantomData<fn() -> T>);
344 
345 impl<T> Default for IntoFn<T> {
default() -> Self346     fn default() -> Self {
347         Self(PhantomData)
348     }
349 }
350 impl<A, T> FnOnce1<A> for IntoFn<T> where A: Into<T> {
351     type Output = T;
call_once(self, arg: A) -> Self::Output352     fn call_once(self, arg: A) -> Self::Output {
353         arg.into()
354     }
355 }
356 
357 trivial_fn_impls!(into_fn <T> IntoFn<T> = "Into::into");
358