| 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 | fn call_once(self, arg: A) -> R { |
| 15 | self(arg) |
| 16 | } |
| 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 | 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> { |
| 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 | 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> { |
| 77 | fn default() -> Self { |
| 78 | Self(PhantomData) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl<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 | |
| 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 | 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 | { |
| 109 | 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 | { |
| 118 | fn call(&self, arg: A) -> Self::Output { |
| 119 | self.1.call(self.0.call(arg)) |
| 120 | } |
| 121 | } |
| 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; |
| 131 | fn call_once(self, arg: Result<T, T>) -> Self::Output { |
| 132 | match arg { |
| 133 | Ok(x: T) => x, |
| 134 | Err(x: T) => 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> dynFnOnce1<&'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 |
| 155 | impl<F, A> FnMut1<A> for InspectFn<F> |
| 156 | where |
| 157 | F: for<'a> dynFnMut1<&'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 |
| 165 | impl<F, A> Fn1<A> for InspectFn<F> |
| 166 | where |
| 167 | F: for<'a> dynFn1<&'a A, Output = ()>, |
| 168 | { |
| 169 | fn call(&self, arg: A) -> Self::Output { |
| 170 | self.0.call(&arg); |
| 171 | arg |
| 172 | } |
| 173 | } |
| 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>; |
| 186 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
| 187 | arg.map(|x: T| self.0.call_once(arg:x)) |
| 188 | } |
| 189 | } |
| 190 | impl<F, T, E> FnMut1<Result<T, E>> for MapOkFn<F> |
| 191 | where |
| 192 | F: FnMut1<T>, |
| 193 | { |
| 194 | fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output { |
| 195 | arg.map(|x: T| self.0.call_mut(arg:x)) |
| 196 | } |
| 197 | } |
| 198 | impl<F, T, E> Fn1<Result<T, E>> for MapOkFn<F> |
| 199 | where |
| 200 | F: Fn1<T>, |
| 201 | { |
| 202 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
| 203 | arg.map(|x: T| self.0.call(arg:x)) |
| 204 | } |
| 205 | } |
| 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>; |
| 218 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
| 219 | arg.map_err(|x: E| self.0.call_once(arg:x)) |
| 220 | } |
| 221 | } |
| 222 | impl<F, T, E> FnMut1<Result<T, E>> for MapErrFn<F> |
| 223 | where |
| 224 | F: FnMut1<E>, |
| 225 | { |
| 226 | fn call_mut(&mut self, arg: Result<T, E>) -> Self::Output { |
| 227 | arg.map_err(|x: E| self.0.call_mut(arg:x)) |
| 228 | } |
| 229 | } |
| 230 | impl<F, T, E> Fn1<Result<T, E>> for MapErrFn<F> |
| 231 | where |
| 232 | F: Fn1<E>, |
| 233 | { |
| 234 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
| 235 | arg.map_err(|x: E| self.0.call(arg:x)) |
| 236 | } |
| 237 | } |
| 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 = (); |
| 250 | fn call_once(self, arg: &'a Result<T, E>) -> Self::Output { |
| 251 | if let Ok(x: &T) = arg { |
| 252 | self.0.call_once(arg: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 | { |
| 260 | fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output { |
| 261 | if let Ok(x: &T) = arg { |
| 262 | self.0.call_mut(arg: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 | { |
| 270 | fn call(&self, arg: &'a Result<T, E>) -> Self::Output { |
| 271 | if let Ok(x: &T) = arg { |
| 272 | self.0.call(arg:x) |
| 273 | } |
| 274 | } |
| 275 | } |
| 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 = (); |
| 288 | fn call_once(self, arg: &'a Result<T, E>) -> Self::Output { |
| 289 | if let Err(x: &E) = arg { |
| 290 | self.0.call_once(arg: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 | { |
| 298 | fn call_mut(&mut self, arg: &'a Result<T, E>) -> Self::Output { |
| 299 | if let Err(x: &E) = arg { |
| 300 | self.0.call_mut(arg: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 | { |
| 308 | fn call(&self, arg: &'a Result<T, E>) -> Self::Output { |
| 309 | if let Err(x: &E) = arg { |
| 310 | self.0.call(arg:x) |
| 311 | } |
| 312 | } |
| 313 | } |
| 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>>; |
| 319 | pub(crate) fn map_ok_or_else_fn<F, G>(f: F, g: G) -> MapOkOrElseFn<F, G> { |
| 320 | chain_fn(f:map_ok_fn(f), g:chain_fn(f:map_err_fn(g), 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; |
| 331 | fn call_once(self, arg: Result<T, E>) -> Self::Output { |
| 332 | arg.unwrap_or_else(|x: E| self.0.call_once(arg:x)) |
| 333 | } |
| 334 | } |
| 335 | impl<F, T, E> FnMut1<Result<T, E>> for UnwrapOrElseFn<F> |
| 336 | where |
| 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: E| self.0.call_mut(arg:x)) |
| 341 | } |
| 342 | } |
| 343 | impl<F, T, E> Fn1<Result<T, E>> for UnwrapOrElseFn<F> |
| 344 | where |
| 345 | F: Fn1<E, Output = T>, |
| 346 | { |
| 347 | fn call(&self, arg: Result<T, E>) -> Self::Output { |
| 348 | arg.unwrap_or_else(|x: E| self.0.call(arg:x)) |
| 349 | } |
| 350 | } |
| 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> { |
| 358 | 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; |
| 367 | 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 | |