use core::fmt::{self, Debug}; use core::marker::PhantomData; pub trait FnOnce1 { type Output; fn call_once(self, arg: A) -> Self::Output; } impl FnOnce1 for T where T: FnOnce(A) -> R, { type Output = R; fn call_once(self, arg: A) -> R { self(arg) } } pub trait FnMut1: FnOnce1 { fn call_mut(&mut self, arg: A) -> Self::Output; } impl FnMut1 for T where T: FnMut(A) -> R, { fn call_mut(&mut self, arg: A) -> R { self(arg) } } // Not used, but present for completeness #[allow(unreachable_pub)] pub trait Fn1: FnMut1 { fn call(&self, arg: A) -> Self::Output; } impl Fn1 for T where T: Fn(A) -> R, { fn call(&self, arg: A) -> R { self(arg) } } macro_rules! trivial_fn_impls { ($name:ident <$($arg:ident),*> $t:ty = $debug:literal) => { impl<$($arg),*> Copy for $t {} impl<$($arg),*> Clone for $t { fn clone(&self) -> Self { *self } } impl<$($arg),*> Debug for $t { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str($debug) } } impl<$($arg,)* A> FnMut1 for $t where Self: FnOnce1 { fn call_mut(&mut self, arg: A) -> Self::Output { self.call_once(arg) } } impl<$($arg,)* A> Fn1 for $t where Self: FnOnce1 { fn call(&self, arg: A) -> Self::Output { self.call_once(arg) } } pub(crate) fn $name<$($arg),*>() -> $t { Default::default() } } } pub struct OkFn(PhantomData); impl Default for OkFn { fn default() -> Self { Self(PhantomData) } } impl FnOnce1 for OkFn { type Output = Result; fn call_once(self, arg: A) -> Self::Output { Ok(arg) } } trivial_fn_impls!(ok_fn OkFn = "Ok"); #[derive(Debug, Copy, Clone, Default)] pub struct ChainFn(F, G); impl FnOnce1 for ChainFn where F: FnOnce1, G: FnOnce1, { type Output = G::Output; fn call_once(self, arg: A) -> Self::Output { self.1.call_once(self.0.call_once(arg)) } } impl FnMut1 for ChainFn where F: FnMut1, G: FnMut1, { fn call_mut(&mut self, arg: A) -> Self::Output { self.1.call_mut(self.0.call_mut(arg)) } } impl Fn1 for ChainFn where F: Fn1, G: Fn1, { fn call(&self, arg: A) -> Self::Output { self.1.call(self.0.call(arg)) } } pub(crate) fn chain_fn(f: F, g: G) -> ChainFn { ChainFn(f, g) } #[derive(Default)] pub struct MergeResultFn; impl FnOnce1> for MergeResultFn { type Output = T; fn call_once(self, arg: Result) -> Self::Output { match arg { Ok(x) => x, Err(x) => x, } } } trivial_fn_impls!(merge_result_fn <> MergeResultFn = "merge_result"); #[derive(Debug, Copy, Clone, Default)] pub struct InspectFn(F); #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 impl FnOnce1 for InspectFn where F: for<'a> FnOnce1<&'a A, Output = ()>, { type Output = A; fn call_once(self, arg: A) -> Self::Output { self.0.call_once(&arg); arg } } #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 impl FnMut1 for InspectFn where F: for<'a> FnMut1<&'a A, Output = ()>, { fn call_mut(&mut self, arg: A) -> Self::Output { self.0.call_mut(&arg); arg } } #[allow(single_use_lifetimes)] // https://github.com/rust-lang/rust/issues/55058 impl Fn1 for InspectFn where F: for<'a> Fn1<&'a A, Output = ()>, { fn call(&self, arg: A) -> Self::Output { self.0.call(&arg); arg } } pub(crate) fn inspect_fn(f: F) -> InspectFn { InspectFn(f) } #[derive(Debug, Copy, Clone, Default)] pub struct MapOkFn(F); impl FnOnce1> for MapOkFn where F: FnOnce1, { type Output = Result; fn call_once(self, arg: Result) -> Self::Output { arg.map(|x| self.0.call_once(x)) } } impl FnMut1> for MapOkFn where F: FnMut1, { fn call_mut(&mut self, arg: Result) -> Self::Output { arg.map(|x| self.0.call_mut(x)) } } impl Fn1> for MapOkFn where F: Fn1, { fn call(&self, arg: Result) -> Self::Output { arg.map(|x| self.0.call(x)) } } pub(crate) fn map_ok_fn(f: F) -> MapOkFn { MapOkFn(f) } #[derive(Debug, Copy, Clone, Default)] pub struct MapErrFn(F); impl FnOnce1> for MapErrFn where F: FnOnce1, { type Output = Result; fn call_once(self, arg: Result) -> Self::Output { arg.map_err(|x| self.0.call_once(x)) } } impl FnMut1> for MapErrFn where F: FnMut1, { fn call_mut(&mut self, arg: Result) -> Self::Output { arg.map_err(|x| self.0.call_mut(x)) } } impl Fn1> for MapErrFn where F: Fn1, { fn call(&self, arg: Result) -> Self::Output { arg.map_err(|x| self.0.call(x)) } } pub(crate) fn map_err_fn(f: F) -> MapErrFn { MapErrFn(f) } #[derive(Debug, Copy, Clone)] pub struct InspectOkFn(F); impl<'a, F, T, E> FnOnce1<&'a Result> for InspectOkFn where F: FnOnce1<&'a T, Output = ()>, { type Output = (); fn call_once(self, arg: &'a Result) -> Self::Output { if let Ok(x) = arg { self.0.call_once(x) } } } impl<'a, F, T, E> FnMut1<&'a Result> for InspectOkFn where F: FnMut1<&'a T, Output = ()>, { fn call_mut(&mut self, arg: &'a Result) -> Self::Output { if let Ok(x) = arg { self.0.call_mut(x) } } } impl<'a, F, T, E> Fn1<&'a Result> for InspectOkFn where F: Fn1<&'a T, Output = ()>, { fn call(&self, arg: &'a Result) -> Self::Output { if let Ok(x) = arg { self.0.call(x) } } } pub(crate) fn inspect_ok_fn(f: F) -> InspectOkFn { InspectOkFn(f) } #[derive(Debug, Copy, Clone)] pub struct InspectErrFn(F); impl<'a, F, T, E> FnOnce1<&'a Result> for InspectErrFn where F: FnOnce1<&'a E, Output = ()>, { type Output = (); fn call_once(self, arg: &'a Result) -> Self::Output { if let Err(x) = arg { self.0.call_once(x) } } } impl<'a, F, T, E> FnMut1<&'a Result> for InspectErrFn where F: FnMut1<&'a E, Output = ()>, { fn call_mut(&mut self, arg: &'a Result) -> Self::Output { if let Err(x) = arg { self.0.call_mut(x) } } } impl<'a, F, T, E> Fn1<&'a Result> for InspectErrFn where F: Fn1<&'a E, Output = ()>, { fn call(&self, arg: &'a Result) -> Self::Output { if let Err(x) = arg { self.0.call(x) } } } pub(crate) fn inspect_err_fn(f: F) -> InspectErrFn { InspectErrFn(f) } pub(crate) type MapOkOrElseFn = ChainFn, ChainFn, MergeResultFn>>; pub(crate) fn map_ok_or_else_fn(f: F, g: G) -> MapOkOrElseFn { chain_fn(map_ok_fn(f), chain_fn(map_err_fn(g), merge_result_fn())) } #[derive(Debug, Copy, Clone, Default)] pub struct UnwrapOrElseFn(F); impl FnOnce1> for UnwrapOrElseFn where F: FnOnce1, { type Output = T; fn call_once(self, arg: Result) -> Self::Output { arg.unwrap_or_else(|x| self.0.call_once(x)) } } impl FnMut1> for UnwrapOrElseFn where F: FnMut1, { fn call_mut(&mut self, arg: Result) -> Self::Output { arg.unwrap_or_else(|x| self.0.call_mut(x)) } } impl Fn1> for UnwrapOrElseFn where F: Fn1, { fn call(&self, arg: Result) -> Self::Output { arg.unwrap_or_else(|x| self.0.call(x)) } } pub(crate) fn unwrap_or_else_fn(f: F) -> UnwrapOrElseFn { UnwrapOrElseFn(f) } pub struct IntoFn(PhantomData T>); impl Default for IntoFn { fn default() -> Self { Self(PhantomData) } } impl FnOnce1 for IntoFn where A: Into, { type Output = T; fn call_once(self, arg: A) -> Self::Output { arg.into() } } trivial_fn_impls!(into_fn IntoFn = "Into::into");