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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Macros for dispatching based on generic types.

#![warn(missing_docs)]

use crate::types::{dispatch_const_impl, dispatch_fn_impl, dispatch_type_impl};
use proc_macro::{TokenStream, TokenTree};
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;

use crate::impls::impl_dispatch_impl;

mod impls;
mod types;

/// Dispatches a trait implementation to a specified set of generic types.
///
/// # Syntax
///
/// Sets are delimited with curly braces (`{}`) and have semicolon-separated elements. Sets
/// are used to enumerate types to be assigned to a generic argument or set of generic arguments.
///
/// Comma-separated elements are combined using a cartesian product, making it easier to
/// implement traits on any combination of the provided types.
///
/// # Semantics
///
/// The supplied dispatch types are dispatched starting from the first generic type argument
/// of the trait implementation.
///
/// # Examples
///
/// ```
/// # use type_dispatch_macros::impl_dispatch;
/// struct GenericStruct<A, B>(A, B);
///
/// // Creates 4 trait implementations.
/// #[impl_dispatch({u64; u16}, {u32, usize; u8, u64})]
/// impl<A, B, C> Into<C> for GenericStruct<A, B> {
///     fn into(self) -> C {
///        self.0 as C + self.1 as C
///    }
/// }
///
/// let x: usize = GenericStruct(1u64, 3u32).into();
/// assert_eq!(x, 4);
/// let x: u64 = GenericStruct(1u64, 3u8).into();
/// assert_eq!(x, 4);
/// let x: usize = GenericStruct(1u16, 3u32).into();
/// assert_eq!(x, 4);
/// let x: u64 = GenericStruct(1u16, 3u8).into();
/// assert_eq!(x, 4);
///
/// // The following two lines will not compile as `GenericStruct` does not implement
/// // these particular type combinations:
/// // ```
/// // let x: u64 = GenericStruct(1u64, 3u32).into();
/// // let x: usize = GenericStruct(1u64, 3u8).into();
/// // ```
/// ```
#[proc_macro_attribute]
pub fn impl_dispatch(args: TokenStream, input: TokenStream) -> TokenStream {
    impl_dispatch_impl(args, input)
}

/// A function-like variant of [`macro@impl_dispatch`].
///
/// # Syntax
///
/// The syntax is the same as [`macro@impl_dispatch`], but the contents of the attribute must now
/// be contained by brackets (`[]`) or braces (`{}`).
///
/// # Examples
///
/// ```
/// # use type_dispatch_macros::dispatch_impl;
/// struct GenericStruct<A, B>(A, B);
///
/// // Creates 4 trait implementations.
/// dispatch_impl!{
///     [{u64; u16}, {u32, usize; u8, u64}]
///     impl<A, B, C> Into<C> for GenericStruct<A, B> {
///         fn into(self) -> C {
///            self.0 as C + self.1 as C
///        }
///     }
/// }
///
/// let x: usize = GenericStruct(1u64, 3u32).into();
/// assert_eq!(x, 4);
/// let x: u64 = GenericStruct(1u64, 3u8).into();
/// assert_eq!(x, 4);
/// let x: usize = GenericStruct(1u16, 3u32).into();
/// assert_eq!(x, 4);
/// let x: u64 = GenericStruct(1u16, 3u8).into();
/// assert_eq!(x, 4);
/// ```
#[proc_macro]
pub fn dispatch_impl(input: TokenStream) -> TokenStream {
    let mut iter = input.into_iter();
    impl_dispatch_impl(
        if let TokenTree::Group(g) = iter.next().unwrap() {
            g.stream()
        } else {
            panic!()
        },
        iter.collect(),
    )
}

/// Dispatches a constant based on a given generic type.
///
/// # Syntax
///
/// The syntax is effectively the same as a match statement, but the patterns are instead
/// simply comma-separated lists of types.
///
/// # Semantics
///
/// Unlike normal match statements, duplicate arms are not allowed. Only the constant
/// corresponding to the unique matching arm will be dispatched.
///
/// [`dispatch_const!`] internally uses the `DispatchConst` trait to dispatch constant values,
/// meaning that types do not have to match exactly (i.e. [`macro@impl_dispatch`] might have
/// `std::vec::Vec` while the [`dispatch_const!`] arm has `Vec`).
///
/// # Examples
///
/// ```
/// # use type_dispatch_macros::{dispatch_const, impl_dispatch};
/// struct GenericStruct<A, B>(A, B);
///
/// // Creates 4 trait implementations.
/// #[impl_dispatch({u64; u16}, {u32, usize; u8, u64})]
/// impl<A, B, C> Into<C> for GenericStruct<A, B> {
///     fn into(self) -> C {
///        self.0 as C + self.1 as C + dispatch_const!(
///             match A, B {
///                 u64, u32 => 1: C,
///                 u64, u8 => 2: C,
///                 u16, u32 => 3: C,
///                 u16, u8 => 4: C,
///             }           
///         )
///    }
/// }
///
/// let x: usize = GenericStruct(1u64, 3u32).into();
/// assert_eq!(x, 5);
/// let x: u64 = GenericStruct(1u64, 3u8).into();
/// assert_eq!(x, 6);
/// let x: usize = GenericStruct(1u16, 3u32).into();
/// assert_eq!(x, 7);
/// let x: u64 = GenericStruct(1u16, 3u8).into();
/// assert_eq!(x, 8);
/// ```
#[proc_macro]
pub fn dispatch_const(input: TokenStream) -> TokenStream {
    dispatch_const_impl(input)
}

/// Dispatches a function body based on a given generic type.
///
/// # Syntax
///
/// The syntax is effectively the same as a match statement, but the patterns are instead
/// simply comma-separated lists of types.
///
/// # Semantics
///
/// Unlike normal match statements, duplicate arms are not allowed. Only the constant
/// corresponding to the unique matching arm will be dispatched.
///
/// [`dispatch_fn!`] internally uses the `DispatchFn` trait to dispatch functions,
/// meaning that types do not have to match exactly (i.e. [`macro@impl_dispatch`] might have
/// `std::vec::Vec` while the [`dispatch_const!`] arm has `Vec`).
///
/// # Examples
///
/// ```
/// # use type_dispatch_macros::{dispatch_fn, impl_dispatch};
/// struct GenericStruct<A, B>(A, B);
///
/// // Creates 4 trait implementations.
/// #[impl_dispatch({u64; u16}, {u32, usize; u8, u64})]
/// impl<A, B, C> Into<C> for GenericStruct<A, B> {
///     fn into(self) -> C {
///        self.0 as C + self.1 as C + dispatch_fn!(
///             match A, B {
///                 u64, u32 => vec![()]: Vec<()>,
///                 u64, u8 => vec![1, 2]: Vec<u32>,
///                 u16, u32 => "ABC".to_string(): String,
///                 u16, u8 => "ABCD": &'static str,
///             }           
///         ).len() as C
///    }
/// }
///
/// let x: usize = GenericStruct(1u64, 3u32).into();
/// assert_eq!(x, 5);
/// let x: u64 = GenericStruct(1u64, 3u8).into();
/// assert_eq!(x, 6);
/// let x: usize = GenericStruct(1u16, 3u32).into();
/// assert_eq!(x, 7);
/// let x: u64 = GenericStruct(1u16, 3u8).into();
/// assert_eq!(x, 8);
/// ```
#[proc_macro]
pub fn dispatch_fn(input: TokenStream) -> TokenStream {
    dispatch_fn_impl(input)
}

/// Dispatches a constant based on a given generic type.
///
/// # Syntax
///
/// The syntax is effectively the same as a match statement, but the patterns are instead
/// simply comma-separated lists of types.
///
/// # Semantics
///
/// Unlike normal match statements, duplicate arms are not allowed. Only the constant
/// corresponding to the unique matching arm will be dispatched.
///
/// [`dispatch_type!`] matches based on the raw parsed type, meaning that types must match exactly
/// (i.e. [`macro@impl_dispatch`] cannot have `std::vec::Vec` while the [`dispatch_const!`] arm has `Vec`).
///
/// # Examples
///
/// ```
/// # use type_dispatch_macros::{dispatch_type, impl_dispatch};
/// struct GenericStruct<A, B>(A, B);
///
/// // Creates 4 trait implementations.
/// #[impl_dispatch({u64; u16}, {u32, usize; u8, u64})]
/// impl<A, B, C> Into<C> for GenericStruct<A, B> {
///     fn into(self) -> C {
///        self.0 as C + self.1 as C + dispatch_type!(
///             match A, B {
///                 u64, u32 => 0..self.1,
///                 u64, u8 => vec![self.0 + self.1 as u64],
///                 u16, u32 => "ABC".to_string(),
///                 u16, u8 => "ABCD",
///             }           
///         ).len() as C
///    }
/// }
///
/// let x: usize = GenericStruct(1u64, 3u32).into();
/// assert_eq!(x, 7);
/// let x: u64 = GenericStruct(1u64, 3u8).into();
/// assert_eq!(x, 5);
/// let x: usize = GenericStruct(1u16, 3u32).into();
/// assert_eq!(x, 7);
/// let x: u64 = GenericStruct(1u16, 3u8).into();
/// assert_eq!(x, 8);
/// ```
#[proc_macro]
pub fn dispatch_type(input: TokenStream) -> TokenStream {
    dispatch_type_impl(input)
}

pub(crate) fn type_dispatch_ident() -> TokenStream2 {
    match crate_name("type_dispatch") {
        Ok(FoundCrate::Itself) => quote!(::type_dispatch),
        Ok(FoundCrate::Name(name)) => {
            let ident = Ident::new(&name, Span::call_site());
            quote!(::#ident)
        }
        Err(_) => match crate_name("substrate").expect("type_dispatch not found in Cargo.toml") {
            FoundCrate::Itself => quote!(::substrate::type_dispatch),
            FoundCrate::Name(name) => {
                let ident = Ident::new(&name, Span::call_site());
                quote!(::#ident::type_dispatch)
            }
        },
    }
}