/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/fns.rs
Line | Count | Source |
1 | | use core::fmt::{self, Debug}; |
2 | | use core::marker::PhantomData; |
3 | | |
4 | | pub trait FnOnce1<A> { |
5 | | type Output; |
6 | | 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; |
14 | 0 | fn call_once(self, arg: A) -> R { |
15 | 0 | self(arg) |
16 | 0 | } |
17 | | } |
18 | | |
19 | | pub trait FnMut1<A>: FnOnce1<A> { |
20 | | 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 | | { |
27 | 0 | fn call_mut(&mut self, arg: A) -> R { |
28 | 0 | self(arg) |
29 | 0 | } |
30 | | } |
31 | | |
32 | | // Not used, but present for completeness |
33 | | #[allow(unreachable_pub)] |
34 | | pub trait Fn1<A>: FnMut1<A> { |
35 | | 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 | | { |
42 | 0 | fn call(&self, arg: A) -> R { |
43 | 0 | self(arg) |
44 | 0 | } |
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 | 0 | fn clone(&self) -> Self { *self } Unexecuted instantiation: <futures_util::fns::MergeResultFn as core::clone::Clone>::clone Unexecuted instantiation: <futures_util::fns::IntoFn<_> as core::clone::Clone>::clone Unexecuted instantiation: <futures_util::fns::OkFn<_> as core::clone::Clone>::clone |
52 | | } |
53 | | impl<$($arg),*> Debug for $t { |
54 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
55 | 0 | f.write_str($debug) |
56 | 0 | } Unexecuted instantiation: <futures_util::fns::MergeResultFn as core::fmt::Debug>::fmt Unexecuted instantiation: <futures_util::fns::IntoFn<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <futures_util::fns::OkFn<_> as core::fmt::Debug>::fmt |
57 | | } |
58 | | impl<$($arg,)* A> FnMut1<A> for $t where Self: FnOnce1<A> { |
59 | 0 | fn call_mut(&mut self, arg: A) -> Self::Output { |
60 | 0 | self.call_once(arg) |
61 | 0 | } Unexecuted instantiation: <futures_util::fns::IntoFn<_> as futures_util::fns::FnMut1<_>>::call_mut Unexecuted instantiation: <futures_util::fns::MergeResultFn as futures_util::fns::FnMut1<_>>::call_mut Unexecuted instantiation: <futures_util::fns::OkFn<_> as futures_util::fns::FnMut1<_>>::call_mut |
62 | | } |
63 | | impl<$($arg,)* A> Fn1<A> for $t where Self: FnOnce1<A> { |
64 | 0 | fn call(&self, arg: A) -> Self::Output { |
65 | 0 | self.call_once(arg) |
66 | 0 | } Unexecuted instantiation: <futures_util::fns::IntoFn<_> as futures_util::fns::Fn1<_>>::call Unexecuted instantiation: <futures_util::fns::MergeResultFn as futures_util::fns::Fn1<_>>::call Unexecuted instantiation: <futures_util::fns::OkFn<_> as futures_util::fns::Fn1<_>>::call |
67 | | } |
68 | 0 | pub(crate) fn $name<$($arg),*>() -> $t { |
69 | 0 | Default::default() |
70 | 0 | } Unexecuted instantiation: futures_util::fns::merge_result_fn Unexecuted instantiation: futures_util::fns::ok_fn::<_> Unexecuted instantiation: futures_util::fns::into_fn::<_> |
71 | | } |
72 | | } |
73 | | |
74 | | pub struct OkFn<E>(PhantomData<fn(E)>); |
75 | | |
76 | | impl<E> Default for OkFn<E> { |
77 | 0 | fn default() -> Self { |
78 | 0 | Self(PhantomData) |
79 | 0 | } |
80 | | } |
81 | | |
82 | | impl<A, E> FnOnce1<A> for OkFn<E> { |
83 | | type Output = Result<A, E>; |
84 | 0 | fn call_once(self, arg: A) -> Self::Output { |
85 | 0 | Ok(arg) |
86 | 0 | } |
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; |
100 | 0 | fn call_once(self, arg: A) -> Self::Output { |
101 | 0 | self.1.call_once(self.0.call_once(arg)) |
102 | 0 | } |
103 | | } |
104 | | impl<F, G, A> FnMut1<A> for ChainFn<F, G> |
105 | | where |
106 | | F: FnMut1<A>, |
107 | | G: FnMut1<F::Output>, |
108 | | { |
109 | 0 | fn call_mut(&mut self, arg: A) -> Self::Output { |
110 | 0 | self.1.call_mut(self.0.call_mut(arg)) |
111 | 0 | } |
112 | | } |
113 | | impl<F, G, A> Fn1<A> for ChainFn<F, G> |
114 | | where |
115 | | F: Fn1<A>, |
116 | | G: Fn1<F::Output>, |
117 | | { |
118 | 0 | fn call(&self, arg: A) -> Self::Output { |
119 | 0 | self.1.call(self.0.call(arg)) |
120 | 0 | } |
121 | | } |
122 | 0 | pub(crate) fn chain_fn<F, G>(f: F, g: G) -> ChainFn<F, G> { |
123 | 0 | ChainFn(f, g) |
124 | 0 | } |
125 | | |
126 | | #[derive(Default)] |
127 | | pub struct MergeResultFn; |
128 | | |
129 | | impl<T> FnOnce1<Result<T, T>> for MergeResultFn { |
130 | | type Output = T; |
131 | 0 | fn call_once(self, arg: Result<T, T>) -> Self::Output { |
132 | 0 | match arg { |
133 | 0 | Ok(x) => x, |
134 | 0 | Err(x) => x, |
135 | | } |
136 | 0 | } |
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; |
149 | 0 | fn call_once(self, arg: A) -> Self::Output { |
150 | 0 | self.0.call_once(&arg); |
151 | 0 | arg |
152 | 0 | } |
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 | | { |
159 | 0 | fn call_mut(&mut self, arg: A) -> Self::Output { |
160 | 0 | self.0.call_mut(&arg); |
161 | 0 | arg |
162 | 0 | } |
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 | | { |
169 | 0 | fn call(&self, arg: A) -> Self::Output { |
170 | 0 | self.0.call(&arg); |
171 | 0 | arg |
172 | 0 | } |
173 | | } |
174 | 0 | pub(crate) fn inspect_fn<F>(f: F) -> InspectFn<F> { |
175 | 0 | InspectFn(f) |
176 | 0 | } |
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>; |
186 | 0 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
187 | 0 | arg.map(|x| self.0.call_once(x)) |
188 | 0 | } |
189 | | } |
190 | | impl<F, T, E> FnMut1<Result<T, E>> for MapOkFn<F> |
191 | | where |
192 | | F: FnMut1<T>, |
193 | | { |
194 | 0 | fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output { |
195 | 0 | arg.map(|x| self.0.call_mut(x)) |
196 | 0 | } |
197 | | } |
198 | | impl<F, T, E> Fn1<Result<T, E>> for MapOkFn<F> |
199 | | where |
200 | | F: Fn1<T>, |
201 | | { |
202 | 0 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
203 | 0 | arg.map(|x| self.0.call(x)) |
204 | 0 | } |
205 | | } |
206 | 0 | pub(crate) fn map_ok_fn<F>(f: F) -> MapOkFn<F> { |
207 | 0 | MapOkFn(f) |
208 | 0 | } |
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>; |
218 | 0 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
219 | 0 | arg.map_err(|x| self.0.call_once(x)) |
220 | 0 | } |
221 | | } |
222 | | impl<F, T, E> FnMut1<Result<T, E>> for MapErrFn<F> |
223 | | where |
224 | | F: FnMut1<E>, |
225 | | { |
226 | 0 | fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output { |
227 | 0 | arg.map_err(|x| self.0.call_mut(x)) |
228 | 0 | } |
229 | | } |
230 | | impl<F, T, E> Fn1<Result<T, E>> for MapErrFn<F> |
231 | | where |
232 | | F: Fn1<E>, |
233 | | { |
234 | 0 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
235 | 0 | arg.map_err(|x| self.0.call(x)) |
236 | 0 | } |
237 | | } |
238 | 0 | pub(crate) fn map_err_fn<F>(f: F) -> MapErrFn<F> { |
239 | 0 | MapErrFn(f) |
240 | 0 | } |
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 = (); |
250 | 0 | fn call_once(self, arg: &'a Result<T, E>) -> Self::Output { |
251 | 0 | if let Ok(x) = arg { |
252 | 0 | self.0.call_once(x) |
253 | 0 | } |
254 | 0 | } |
255 | | } |
256 | | impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectOkFn<F> |
257 | | where |
258 | | F: FnMut1<&'a T, Output = ()>, |
259 | | { |
260 | 0 | fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output { |
261 | 0 | if let Ok(x) = arg { |
262 | 0 | self.0.call_mut(x) |
263 | 0 | } |
264 | 0 | } |
265 | | } |
266 | | impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectOkFn<F> |
267 | | where |
268 | | F: Fn1<&'a T, Output = ()>, |
269 | | { |
270 | 0 | fn call(&self, arg: &'a Result<T, E>) -> Self::Output { |
271 | 0 | if let Ok(x) = arg { |
272 | 0 | self.0.call(x) |
273 | 0 | } |
274 | 0 | } |
275 | | } |
276 | 0 | pub(crate) fn inspect_ok_fn<F>(f: F) -> InspectOkFn<F> { |
277 | 0 | InspectOkFn(f) |
278 | 0 | } |
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 = (); |
288 | 0 | fn call_once(self, arg: &'a Result<T, E>) -> Self::Output { |
289 | 0 | if let Err(x) = arg { |
290 | 0 | self.0.call_once(x) |
291 | 0 | } |
292 | 0 | } |
293 | | } |
294 | | impl<'a, F, T, E> FnMut1<&'a Result<T, E>> for InspectErrFn<F> |
295 | | where |
296 | | F: FnMut1<&'a E, Output = ()>, |
297 | | { |
298 | 0 | fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output { |
299 | 0 | if let Err(x) = arg { |
300 | 0 | self.0.call_mut(x) |
301 | 0 | } |
302 | 0 | } |
303 | | } |
304 | | impl<'a, F, T, E> Fn1<&'a Result<T, E>> for InspectErrFn<F> |
305 | | where |
306 | | F: Fn1<&'a E, Output = ()>, |
307 | | { |
308 | 0 | fn call(&self, arg: &'a Result<T, E>) -> Self::Output { |
309 | 0 | if let Err(x) = arg { |
310 | 0 | self.0.call(x) |
311 | 0 | } |
312 | 0 | } |
313 | | } |
314 | 0 | pub(crate) fn inspect_err_fn<F>(f: F) -> InspectErrFn<F> { |
315 | 0 | InspectErrFn(f) |
316 | 0 | } |
317 | | |
318 | | pub(crate) type MapOkOrElseFn<F, G> = ChainFn<MapOkFn<F>, ChainFn<MapErrFn<G>, MergeResultFn>>; |
319 | 0 | pub(crate) fn map_ok_or_else_fn<F, G>(f: F, g: G) -> MapOkOrElseFn<F, G> { |
320 | 0 | chain_fn(map_ok_fn(f), chain_fn(map_err_fn(g), merge_result_fn())) |
321 | 0 | } |
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; |
331 | 0 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
332 | 0 | arg.unwrap_or_else(|x| self.0.call_once(x)) |
333 | 0 | } |
334 | | } |
335 | | impl<F, T, E> FnMut1<Result<T, E>> for UnwrapOrElseFn<F> |
336 | | where |
337 | | F: FnMut1<E, Output = T>, |
338 | | { |
339 | 0 | fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output { |
340 | 0 | arg.unwrap_or_else(|x| self.0.call_mut(x)) |
341 | 0 | } |
342 | | } |
343 | | impl<F, T, E> Fn1<Result<T, E>> for UnwrapOrElseFn<F> |
344 | | where |
345 | | F: Fn1<E, Output = T>, |
346 | | { |
347 | 0 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
348 | 0 | arg.unwrap_or_else(|x| self.0.call(x)) |
349 | 0 | } |
350 | | } |
351 | 0 | pub(crate) fn unwrap_or_else_fn<F>(f: F) -> UnwrapOrElseFn<F> { |
352 | 0 | UnwrapOrElseFn(f) |
353 | 0 | } |
354 | | |
355 | | pub struct IntoFn<T>(PhantomData<fn() -> T>); |
356 | | |
357 | | impl<T> Default for IntoFn<T> { |
358 | 0 | fn default() -> Self { |
359 | 0 | Self(PhantomData) |
360 | 0 | } |
361 | | } |
362 | | impl<A, T> FnOnce1<A> for IntoFn<T> |
363 | | where |
364 | | A: Into<T>, |
365 | | { |
366 | | type Output = T; |
367 | 0 | fn call_once(self, arg: A) -> Self::Output { |
368 | 0 | arg.into() |
369 | 0 | } |
370 | | } |
371 | | |
372 | | trivial_fn_impls!(into_fn <T> IntoFn<T> = "Into::into"); |