rune_alloc/
macros.rs

1// See the cfg-if crate.
2#[allow(unused_macro_rules)]
3macro_rules! cfg_if {
4    // match if/else chains with a final `else`
5    ($(
6        if #[cfg($($meta:meta),*)] { $($it:item)* }
7    ) else * else {
8        $($it2:item)*
9    }) => {
10        cfg_if! {
11            @__items
12            () ;
13            $( ( ($($meta),*) ($($it)*) ), )*
14            ( () ($($it2)*) ),
15        }
16    };
17
18    // match if/else chains lacking a final `else`
19    (
20        if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
21        $(
22            else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
23        )*
24    ) => {
25        cfg_if! {
26            @__items
27            () ;
28            ( ($($i_met),*) ($($i_it)*) ),
29            $( ( ($($e_met),*) ($($e_it)*) ), )*
30            ( () () ),
31        }
32    };
33
34    // Internal and recursive macro to emit all the items
35    //
36    // Collects all the negated cfgs in a list at the beginning and after the
37    // semicolon is all the remaining items
38    (@__items ($($not:meta,)*) ; ) => {};
39    (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
40        // Emit all items within one block, applying an appropriate #[cfg]. The
41        // #[cfg] will require all `$m` matchers specified and must also negate
42        // all previous matchers.
43        cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
44
45        // Recurse to emit all other items in `$rest`, and when we do so add all
46        // our `$m` matchers to the list of `$not` matchers as future emissions
47        // will have to negate everything we just matched as well.
48        cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
49    };
50
51    // Internal macro to Apply a cfg attribute to a list of items
52    (@__apply $m:meta, $($it:item)*) => {
53        $(#[$m] $it)*
54    };
55}
56
57/// Call the given macro with repeated type arguments and counts.
58macro_rules! repeat_macro {
59    ($macro:ident) => {
60        $macro!(0);
61        $macro!(1, A a 0);
62        $macro!(2, A a 0, B b 1);
63        $macro!(3, A a 0, B b 1, C c 2);
64        $macro!(4, A a 0, B b 1, C c 2, D d 3);
65        #[cfg(not(test))]
66        $macro!(5, A a 0, B b 1, C c 2, D d 3, E e 4);
67        #[cfg(not(test))]
68        $macro!(6, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5);
69        #[cfg(not(test))]
70        $macro!(7, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6);
71        #[cfg(not(test))]
72        $macro!(8, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7);
73        #[cfg(not(test))]
74        $macro!(9, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8);
75        #[cfg(not(test))]
76        $macro!(10, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9);
77        #[cfg(not(test))]
78        $macro!(11, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10);
79        #[cfg(not(test))]
80        $macro!(12, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10, L l 11);
81        #[cfg(not(test))]
82        $macro!(13, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10, L l 11, M m 12);
83        #[cfg(not(test))]
84        $macro!(14, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10, L l 11, M m 12, N n 13);
85        #[cfg(not(test))]
86        $macro!(15, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10, L l 11, M m 12, N n 13, O o 14);
87        #[cfg(not(test))]
88        $macro!(16, A a 0, B b 1, C c 2, D d 3, E e 4, F f 5, G g 6, H h 7, I i 8, J j 9, K k 10, L l 11, M m 12, N n 13, O o 14, P p 15);
89    };
90}
91
92// Helper macro for specialization. This also helps avoid parse errors if the
93// default fn syntax for specialization changes in the future.
94#[cfg(rune_nightly)]
95macro_rules! default_fn {
96    ($(#[$meta:meta])* unsafe fn $($tt:tt)*) => {
97        $(#[$meta])*
98        default unsafe fn $($tt)*
99    };
100
101    ($(#[$meta:meta])* fn $($tt:tt)*) => {
102        $(#[$meta])*
103        default fn $($tt)*
104    }
105}
106
107#[cfg(not(rune_nightly))]
108macro_rules! default_fn {
109    ($($tt:tt)*) => {
110        $($tt)*
111    }
112}