Skip to content

Instantly share code, notes, and snippets.

@Venryx
Created December 21, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Venryx/f4602fc1fbbd353b7839ed87b0f4e147 to your computer and use it in GitHub Desktop.
Save Venryx/f4602fc1fbbd353b7839ed87b0f4e147 to your computer and use it in GitHub Desktop.
Extensions of the `AsyncFn` trait for use with output-types and variable numbers of arguments (see: https://stackoverflow.com/a/72851012)
use futures::Future;
pub trait AsyncFn_Args0<OutputType>: Fn() -> Self::Future {
type Future: Future<Output = OutputType>;
}
impl<OutputType, F, Fut> AsyncFn_Args0<OutputType> for F
where F: Fn() -> Fut, Fut: Future<Output = OutputType> {
type Future = Fut;
}
pub trait AsyncFn_Args1<OutputType, Arg1>: Fn(Arg1) -> Self::Future {
type Future: Future<Output = OutputType>;
}
impl<OutputType, Arg1, F, Fut> AsyncFn_Args1<OutputType, Arg1> for F
where F: Fn(Arg1) -> Fut, Fut: Future<Output = OutputType> {
type Future = Fut;
}
pub trait AsyncFn_Args2<OutputType, Arg1, Arg2>: Fn(Arg1, Arg2) -> Self::Future {
type Future: Future<Output = OutputType>;
}
impl<OutputType, Arg1, Arg2, F, Fut> AsyncFn_Args2<OutputType, Arg1, Arg2> for F
where F: Fn(Arg1, Arg2) -> Fut, Fut: Future<Output = OutputType> {
type Future = Fut;
}
pub trait AsyncFn_Args3<OutputType, Arg1, Arg2, Arg3>: Fn(Arg1, Arg2, Arg3) -> Self::Future {
type Future: Future<Output = OutputType>;
}
impl<OutputType, Arg1, Arg2, Arg3, F, Fut> AsyncFn_Args3<OutputType, Arg1, Arg2, Arg3> for F
where F: Fn(Arg1, Arg2, Arg3) -> Fut, Fut: Future<Output = OutputType> {
type Future = Fut;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment