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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/// A macro producing the invocations of the specified generic function with specific types, depending on the value available in runtime.
/// 
/// Examples:
/// ```rust
/// 
/// struct A<const I: usize>();
/// struct B<const I: usize>();
/// struct C<const I: usize>();
/// 
/// #[derive(Debug)]
/// enum Select<const I: usize> {
///     A,
///     B,
///     C,
/// }
/// 
/// use core::any::type_name as tn;
/// 
/// #[test]
/// fn specialize_call_1() {
///     // there is a function `do_it` that takes one runtime argument, and one type-parameter.
///     fn do_it<T>(_arg: usize) -> &'static str {
///         tn::<T>()
///     }
/// 
///     assert_eq!(
///         specialize_call!(
///             // invoke `do_it::<?>`
///             do_it, 
///             // use `(1)` as the arguments list
///             (1), 
///             // choose the type-parameter depending on this value
///             Select::<1>::A, 
///             // the choice map
///             [
///                 // if the provided value is `Select::A`, then invoke `do_it::<A>`
///                 (Select::<1>::A => A::<1>),
///                 // if the provided value is `Select::B`, then invoke `do_it::<B>`
///                 (Select::<1>::B => B::<1>),
///             ]),
///         Some(tn::<A<1>>())
///     );
/// 
///     assert_eq!(
///         specialize_call!(do_it, (2), Select::<1>::B, [
///                 (Select::<1>::A => A::<1>),
///                 (Select::<1>::B => B::<1>),
///             ]),
///         Some(tn::<B<1>>()),
///     );
/// 
///     assert_eq!(
///         specialize_call!(do_it, (3), Select::<1>::C, [
///                 (Select::<1>::A => A::<1>),
///                 (Select::<1>::B => B::<1>),
///             ]),
///         None::<&str>,
///     );
/// }
/// ```
#[macro_export]
macro_rules! specialize_call {
    (@single_mapping,
        ( $($acc_p:pat),* ),
        ( $($acc_t:ty),* ),
        $func:tt, $args:tt, $select:tt,
        [ ($p:pat => $($t:ty),+ ) $(, $p_t:tt)* $(,)* ],
        $mappings:tt
    ) => {
        specialize_call!(@specialize_call,
            ( $($acc_p,)* $p ),
            ( $($acc_t,)* $($t),+ ),

            $func, $args, $select,
            $mappings
        );

        specialize_call!(@single_mapping,
            ( $($acc_p),* ),
            ( $($acc_t),* ),
            $func, $args, $select,
            [ $($p_t),* ],
            $mappings
        );
    };
    (@single_mapping,
        $acc_p:tt, $acc_t: tt,
        $func:tt, $args:tt, $select:tt,
        [],
        $mappings:tt
    ) => {};

    (@specialize_call,
        $acc_p:tt,
        $acc_t:tt,
        $func:tt,
        $args:tt,
        $select:tt,

        ($head:tt $(, $tail:tt)*)
    ) => {
        specialize_call!(@single_mapping,
            $acc_p, $acc_t,
            $func, $args, $select,
            $head,
            ( $($tail),* )
        );
    };
    (@specialize_call,
        $acc_p:tt,
        $acc_t:tt,
        $func:tt,
        $args:tt,
        $select:tt,

        ()
    ) => {
        specialize_call!(@maybe_invoke,
            $acc_p,
            $acc_t,
            $select,
            $func,
            $args
        );
    };

    (@maybe_invoke,
        ( $($acc_p:pat),* ),
        ( $($acc_t:ty),* ),
        $select:expr,
        $func:ident,
        ($($arg:expr),*)
    ) => {
        #[allow(unused_parens)]
        if matches!($select, ( $($acc_p),* )) { break Some($func::<$($acc_t),*>( $($arg),* )); }
    };

    ($func:ident, ($($arg:expr),* $(,)*), $select:expr, $($mapping:tt),+ $(,)*) => {
        loop {
            specialize_call!(@specialize_call,
                (), (),
                $func,
                ($($arg),*),
                $select,
                ( $($mapping),* )
            );
            break None
        }
    };
}