1use core::fmt::{self, Debug};
2use core::marker::PhantomData;
3
4pub trait FnOnce1<A> {
5 type Output;
6 fn call_once(self, arg: A) -> Self::Output;
7}
8
9impl<T, A, R> FnOnce1<A> for T
10where
11 T: FnOnce(A) -> R,
12{
13 type Output = R;
14 fn call_once(self, arg: A) -> R {
15 self(arg)
16 }
17}
18
19pub trait FnMut1<A>: FnOnce1<A> {
20 fn call_mut(&mut self, arg: A) -> Self::Output;
21}
22
23impl<T, A, R> FnMut1<A> for T
24where
25 T: FnMut(A) -> R,
26{
27 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)]
34pub trait Fn1<A>: FnMut1<A> {
35 fn call(&self, arg: A) -> Self::Output;
36}
37
38impl<T, A, R> Fn1<A> for T
39where
40 T: Fn(A) -> R,
41{
42 fn call(&self, arg: A) -> R {
43 self(arg)
44 }
45}
46
47macro_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
74pub struct OkFn<E>(PhantomData<fn(E)>);
75
76impl<E> Default for OkFn<E> {
77 fn default() -> Self {
78 Self(PhantomData)
79 }
80}
81
82impl<A, E> FnOnce1<A> for OkFn<E> {
83 type Output = Result<A, E>;
84 fn call_once(self, arg: A) -> Self::Output {
85 Ok(arg)
86 }
87}
88
89trivial_fn_impls!(ok_fn <T> OkFn<T> = "Ok");
90
91#[derive(Debug, Copy, Clone, Default)]
92pub struct ChainFn<F, G>(F, G);
93
94impl<F, G, A> FnOnce1<A> for ChainFn<F, G>
95where
96 F: FnOnce1<A>,
97 G: FnOnce1<F::Output>,
98{
99 type Output = G::Output;
100 fn call_once(self, arg: A) -> Self::Output {
101 self.1.call_once(self.0.call_once(arg))
102 }
103}
104impl<F, G, A> FnMut1<A> for ChainFn<F, G>
105where
106 F: FnMut1<A>,
107 G: FnMut1<F::Output>,
108{
109 fn call_mut(&mut self, arg: A) -> Self::Output {
110 self.1.call_mut(self.0.call_mut(arg))
111 }
112}
113impl<F, G, A> Fn1<A> for ChainFn<F, G>
114where
115 F: Fn1<A>,
116 G: Fn1<F::Output>,
117{
118 fn call(&self, arg: A) -> Self::Output {
119 self.1.call(self.0.call(arg))
120 }
121}
122pub(crate) fn chain_fn<F, G>(f: F, g: G) -> ChainFn<F, G> {
123 ChainFn(f, g)
124}
125
126#[derive(Default)]
127pub struct MergeResultFn;
128
129impl<T> FnOnce1<Result<T, T>> for MergeResultFn {
130 type Output = T;
131 fn call_once(self, arg: Result<T, T>) -> Self::Output {
132 match arg {
133 Ok(x) => x,
134 Err(x) => x,
135 }
136 }
137}
138trivial_fn_impls!(merge_result_fn <> MergeResultFn = "merge_result");
139
140#[derive(Debug, Copy, Clone, Default)]
141pub struct InspectFn<F>(F);
142
143#[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058
144impl<F, A> FnOnce1<A> for InspectFn<F>
145where
146 F: for<'a> FnOnce1<&'a A, Output = ()>,
147{
148 type Output = A;
149 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
155impl<F, A> FnMut1<A> for InspectFn<F>
156where
157 F: for<'a> FnMut1<&'a A, Output = ()>,
158{
159 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
165impl<F, A> Fn1<A> for InspectFn<F>
166where
167 F: for<'a> Fn1<&'a A, Output = ()>,
168{
169 fn call(&self, arg: A) -> Self::Output {
170 self.0.call(&arg);
171 arg
172 }
173}
174pub(crate) fn inspect_fn<F>(f: F) -> InspectFn<F> {
175 InspectFn(f)
176}
177
178#[derive(Debug, Copy, Clone, Default)]
179pub struct MapOkFn<F>(F);
180
181impl<F, T, E> FnOnce1<Result<T, E>> for MapOkFn<F>
182where
183 F: FnOnce1<T>,
184{
185 type Output = Result<F::Output, E>;
186 fn call_once(self, arg: Result<T, E>) -> Self::Output {
187 arg.map(|x| self.0.call_once(x))
188 }
189}
190impl<F, T, E> FnMut1<Result<T, E>> for MapOkFn<F>
191where
192 F: FnMut1<T>,
193{
194 fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
195 arg.map(|x| self.0.call_mut(x))
196 }
197}
198impl<F, T, E> Fn1<Result<T, E>> for MapOkFn<F>
199where
200 F: Fn1<T>,
201{
202 fn call(&self, arg: Result<T, E>) -> Self::Output {
203 arg.map(|x| self.0.call(x))
204 }
205}
206pub(crate) fn map_ok_fn<F>(f: F) -> MapOkFn<F> {
207 MapOkFn(f)
208}
209
210#[derive(Debug, Copy, Clone, Default)]
211pub struct MapErrFn<F>(F);
212
213impl<F, T, E> FnOnce1<Result<T, E>> for MapErrFn<F>
214where
215 F: FnOnce1<E>,
216{
217 type Output = Result<T, F::Output>;
218 fn call_once(self, arg: Result<T, E>) -> Self::Output {
219 arg.map_err(|x| self.0.call_once(x))
220 }
221}
222impl<F, T, E> FnMut1<Result<T, E>> for MapErrFn<F>
223where
224 F: FnMut1<E>,
225{
226 fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
227 arg.map_err(|x| self.0.call_mut(x))
228 }
229}
230impl<F, T, E> Fn1<Result<T, E>> for MapErrFn<F>
231where
232 F: Fn1<E>,
233{
234 fn call(&self, arg: Result<T, E>) -> Self::Output {
235 arg.map_err(|x| self.0.call(x))
236 }
237}
238pub(crate) fn map_err_fn<F>(f: F) -> MapErrFn<F> {
239 MapErrFn(f)
240}
241
242#[derive(Debug, Copy, Clone)]
243pub struct InspectOkFn<F>(F);
244
245impl<'a, F, T, E> FnOnce1<&'a Result<T, E>> for InspectOkFn<F>
246where
247 F: FnOnce1<&'a T, Output = ()>,
248{
249 type Output = ();
250 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}
256impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectOkFn<F>
257where
258 F: FnMut1<&'a T, Output = ()>,
259{
260 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}
266impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectOkFn<F>
267where
268 F: Fn1<&'a T, Output = ()>,
269{
270 fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
271 if let Ok(x) = arg {
272 self.0.call(x)
273 }
274 }
275}
276pub(crate) fn inspect_ok_fn<F>(f: F) -> InspectOkFn<F> {
277 InspectOkFn(f)
278}
279
280#[derive(Debug, Copy, Clone)]
281pub struct InspectErrFn<F>(F);
282
283impl<'a, F, T, E> FnOnce1<&'a Result<T, E>> for InspectErrFn<F>
284where
285 F: FnOnce1<&'a E, Output = ()>,
286{
287 type Output = ();
288 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}
294impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectErrFn<F>
295where
296 F: FnMut1<&'a E, Output = ()>,
297{
298 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}
304impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectErrFn<F>
305where
306 F: Fn1<&'a E, Output = ()>,
307{
308 fn call(&self, arg: &'a Result<T, E>) -> Self::Output {
309 if let Err(x) = arg {
310 self.0.call(x)
311 }
312 }
313}
314pub(crate) fn inspect_err_fn<F>(f: F) -> InspectErrFn<F> {
315 InspectErrFn(f)
316}
317
318pub(crate) type MapOkOrElseFn<F, G> = ChainFn<MapOkFn<F>, ChainFn<MapErrFn<G>, MergeResultFn>>;
319pub(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)]
324pub struct UnwrapOrElseFn<F>(F);
325
326impl<F, T, E> FnOnce1<Result<T, E>> for UnwrapOrElseFn<F>
327where
328 F: FnOnce1<E, Output = T>,
329{
330 type Output = T;
331 fn call_once(self, arg: Result<T, E>) -> Self::Output {
332 arg.unwrap_or_else(|x| self.0.call_once(x))
333 }
334}
335impl<F, T, E> FnMut1<Result<T, E>> for UnwrapOrElseFn<F>
336where
337 F: FnMut1<E, Output = T>,
338{
339 fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output {
340 arg.unwrap_or_else(|x| self.0.call_mut(x))
341 }
342}
343impl<F, T, E> Fn1<Result<T, E>> for UnwrapOrElseFn<F>
344where
345 F: Fn1<E, Output = T>,
346{
347 fn call(&self, arg: Result<T, E>) -> Self::Output {
348 arg.unwrap_or_else(|x| self.0.call(x))
349 }
350}
351pub(crate) fn unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F> {
352 UnwrapOrElseFn(f)
353}
354
355pub struct IntoFn<T>(PhantomData<fn() -> T>);
356
357impl<T> Default for IntoFn<T> {
358 fn default() -> Self {
359 Self(PhantomData)
360 }
361}
362impl<A, T> FnOnce1<A> for IntoFn<T>
363where
364 A: Into<T>,
365{
366 type Output = T;
367 fn call_once(self, arg: A) -> Self::Output {
368 arg.into()
369 }
370}
371
372trivial_fn_impls!(into_fn <T> IntoFn<T> = "Into::into");
373