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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
pub trait CallOnce< Args > {
type Output;
fn call_once( self, args: Args ) -> Self::Output;
fn expected_argument_count() -> usize;
}
pub trait CallMut< Args >: CallOnce< Args > {
fn call_mut( &mut self, args: Args ) -> Self::Output;
}
pub trait Call< Args >: CallMut< Args > {
fn call( &self, args: Args ) -> Self::Output;
}
macro_rules! noop {
($token:tt) => {}
}
macro_rules! define {
($next:tt => $($kind:ident),*) => {
impl< R, $($kind,)* F: FnOnce( $($kind,)* ) -> R > CallOnce< ($($kind,)*) > for F {
type Output = R;
#[inline]
fn call_once( self, args: ($($kind,)*) ) -> Self::Output {
#[allow(non_snake_case)]
let ($($kind,)*) = args;
self( $($kind),* )
}
#[inline]
fn expected_argument_count() -> usize {
let mut count = 0;
$(
count += 1;
noop!( $kind );
)*
$crate::private::noop( &mut count );
count
}
}
impl< R, $($kind,)* F: FnMut( $($kind,)* ) -> R > CallMut< ($($kind,)*) > for F {
#[inline]
fn call_mut( &mut self, args: ($($kind,)*) ) -> Self::Output {
#[allow(non_snake_case)]
let ($($kind,)*) = args;
self( $($kind),* )
}
}
impl< R, $($kind,)* F: Fn( $($kind,)* ) -> R > Call< ($($kind,)*) > for F {
#[inline]
fn call( &self, args: ($($kind,)*) ) -> Self::Output {
#[allow(non_snake_case)]
let ($($kind,)*) = args;
self( $($kind),* )
}
}
next! { $next }
}
}
loop_through_identifiers!( define );