Skip to content

Instantly share code, notes, and snippets.

@loverdos
Created July 14, 2024 20:54
Show Gist options
  • Save loverdos/e78bb60a8c4b06b4a93ef93775432e23 to your computer and use it in GitHub Desktop.
Save loverdos/e78bb60a8c4b06b4a93ef93775432e23 to your computer and use it in GitHub Desktop.
async_f_type.rs
use core::future::Future;
pub trait AsyncFn<T, R> {
fn call(&self, t: T) -> impl Future<Output = R>;
}
impl<F, T, R, Fut> AsyncFn<T, R> for F
where
F: Fn(T) -> Fut,
Fut: Future<Output = R>,
{
fn call(&self, t: T) -> impl Future<Output = R> {
self(t)
}
}
#[allow(dead_code)]
async fn whatever<F, T, R>(handler: F, t: &T) -> R
where
F: for<'aa> AsyncFn<&'aa T, R>,
{
handler.call(t).await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment