1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#![allow(non_snake_case)]

use core::future::Future;

/// Same as `std::ops::Fn` trait but for async output.
///
/// It is necessary in the the HRTB bounds for async fn's with reference parameters because it
/// allows the output future to be bound to the parameter lifetime.
///     `F: for<'a> AsyncClosure<(&'a u8,) Output=u8>`
pub trait AsyncClosure<Arg> {
    type Output;
    type Future: Future<Output = Self::Output>;

    fn call(&self, arg: Arg) -> Self::Future;
}

macro_rules! async_closure_impl {
    ($($arg: ident),*) => {
        impl<Func, Fut, $($arg,)*> AsyncClosure<($($arg,)*)> for Func
        where
            Func: Fn($($arg),*) -> Fut,
            Fut: Future,
        {
            type Output = Fut::Output;
            type Future = Fut;

            #[inline]
            fn call(&self, ($($arg,)*): ($($arg,)*)) -> Self::Future {
                self($($arg,)*)
            }
        }
    }
}

async_closure_impl! {}
async_closure_impl! { A }
async_closure_impl! { A, B }
async_closure_impl! { A, B, C }
async_closure_impl! { A, B, C, D }
async_closure_impl! { A, B, C, D, E }
async_closure_impl! { A, B, C, D, E, F }
async_closure_impl! { A, B, C, D, E, F, G }
async_closure_impl! { A, B, C, D, E, F, G, H }
async_closure_impl! { A, B, C, D, E, F, G, H, I }