Skip to main content

bevy_reflect/func/
macros.rs

1/// Helper macro to implement the necessary traits for function reflection.
2///
3/// This macro calls the following macros:
4/// - [`impl_get_ownership`](crate::func::args::impl_get_ownership)
5/// - [`impl_from_arg`](crate::func::args::impl_from_arg)
6/// - [`impl_into_return`](crate::func::impl_into_return)
7///
8/// # Syntax
9///
10/// For non-generic types, the macro simply expects the type:
11///
12/// ```ignore
13/// impl_function_traits!(foo::bar::Baz);
14/// ```
15///
16/// For generic types, however, the generic type parameters must also be given in angle brackets (`<` and `>`):
17///
18/// ```ignore
19/// impl_function_traits!(foo::bar::Baz<T, U>; <T: Clone, U>);
20/// ```
21///
22/// For generic const parameters, they must be given in square brackets (`[` and `]`):
23///
24/// ```ignore
25/// impl_function_traits!(foo::bar::Baz<T, N>; <T> [const N: usize]);
26/// ```
27macro_rules! impl_function_traits {
28    (
29        $ty: ty
30        $(;
31            < $($T: ident $(: $T1: tt $(+ $T2: tt)*)?),* >
32        )?
33        $(
34            [ $(const $N: ident : $size: ident),* ]
35        )?
36        $(
37            where $($U: ty $(: $U1: tt $(+ $U2: tt)*)?),*
38        )?
39    ) => {
40        $crate::func::args::impl_get_ownership!(
41            $ty
42            $(;
43                < $($T $(: $T1 $(+ $T2)*)?),* >
44            )?
45            $(
46                [ $(const $N : $size),* ]
47            )?
48            $(
49                where $($U $(: $U1 $(+ $U2)*)?),*
50            )?
51        );
52        $crate::func::args::impl_from_arg!(
53            $ty
54            $(;
55                < $($T $(: $T1 $(+ $T2)*)?),* >
56            )?
57            $(
58                [ $(const $N : $size),* ]
59            )?
60            $(
61                where $($U $(: $U1 $(+ $U2)*)?),*
62            )?
63        );
64        $crate::func::impl_into_return!(
65            $ty
66            $(;
67                < $($T $(: $T1 $(+ $T2)*)?),* >
68            )?
69            $(
70                [ $(const $N : $size),* ]
71            )?
72            $(
73                where $($U $(: $U1 $(+ $U2)*)?),*
74            )?
75        );
76    };
77}
78
79pub(crate) use impl_function_traits;
80
81/// Helper macro that returns the number of tokens it receives.
82///
83/// See [here] for details.
84///
85/// [here]: https://veykril.github.io/tlborm/decl-macros/building-blocks/counting.html#bit-twiddling
86macro_rules! count_tokens {
87    () => { 0 };
88    ($odd:tt $($a:tt $b:tt)*) => { ($crate::func::macros::count_tokens!($($a)*) << 1) | 1 };
89    ($($a:tt $even:tt)*) => { $crate::func::macros::count_tokens!($($a)*) << 1 };
90}
91
92pub(crate) use count_tokens;