redstone_ml/
macros.rs

1// TODO make sure this doesn't export the macro to the public API
2
3#[macro_export(local_inner_macros)]
4macro_rules! recursive_trait_base_cases {
5    ( $macro_name: ident ) => {
6        $macro_name!(i8);
7        $macro_name!(i16);
8        $macro_name!(i32);
9        $macro_name!(i64);
10        $macro_name!(i128);
11        $macro_name!(isize);
12
13        $macro_name!(u8);
14        $macro_name!(u16);
15        $macro_name!(u32);
16        $macro_name!(u64);
17        $macro_name!(u128);
18        $macro_name!(usize);
19
20        $macro_name!(f32);
21        $macro_name!(f64);
22
23        $macro_name!(bool);
24    };
25}
26
27#[macro_export]
28macro_rules! implement_test_for_dtypes {
29    ($name:ident, $body:block, $($t:ty),*) => {
30        $(
31            paste! {
32                #[test]
33                fn [<$name _ $t>]() {
34                    type T = $t;
35                    $body
36                }
37            }
38        )*
39    };
40}
41
42#[macro_export]
43macro_rules! test_for_float_dtypes {
44    ($name:ident, $body:tt) => {
45        implement_test_for_dtypes!($name, $body,
46            f32, f64
47        );
48    };
49}
50
51#[macro_export]
52macro_rules! test_for_common_signed_int_dtypes {
53    ($name:ident, $body:tt) => {
54        implement_test_for_dtypes!($name, $body,
55            i32, i64, i128, isize
56        );
57    };
58}
59
60#[macro_export]
61macro_rules! test_for_common_unsigned_int_dtypes {
62    ($name:ident, $body:tt) => {
63        implement_test_for_dtypes!($name, $body,
64            u32, u64, u128
65        );
66    };
67}
68
69#[macro_export]
70macro_rules! test_for_signed_int_dtypes {
71    ($name:ident, $body:tt) => {
72        test_for_common_signed_int_dtypes!($name, $body);
73        implement_test_for_dtypes!($name, $body,
74            i8, i16
75        );
76    };
77}
78
79#[macro_export]
80macro_rules! test_for_unsigned_int_dtypes {
81    ($name:ident, $body:tt) => {
82        test_for_common_unsigned_int_dtypes!($name, $body);
83        implement_test_for_dtypes!($name, $body,
84            u8, u16
85        );
86    };
87}
88
89#[macro_export]
90macro_rules! test_for_signed_dtypes {
91    ($name:ident, $body:tt) => {
92        test_for_float_dtypes!($name, $body);
93        test_for_signed_int_dtypes!($name, $body);
94    };
95}
96
97#[macro_export]
98macro_rules! test_for_unsigned_dtypes {
99    ($name:ident, $body:tt) => {
100        test_for_unsigned_int_dtypes!($name, $body);
101    };
102}
103
104#[macro_export]
105macro_rules! test_for_integer_dtypes {
106    ($name:ident, $body:tt) => {
107        test_for_signed_int_dtypes!($name, $body);
108        test_for_unsigned_int_dtypes!($name, $body);
109    };
110}
111
112#[macro_export]
113macro_rules! test_for_common_integer_dtypes {
114    ($name:ident, $body:tt) => {
115        test_for_common_signed_int_dtypes!($name, $body);
116        test_for_common_unsigned_int_dtypes!($name, $body);
117    };
118}
119
120#[macro_export]
121macro_rules! test_for_all_numeric_dtypes {
122    ($name:ident, $body:tt) => {
123        test_for_integer_dtypes!($name, $body);
124        test_for_float_dtypes!($name, $body);
125    };
126}
127
128#[macro_export]
129macro_rules! test_for_common_numeric_dtypes {
130    ($name:ident, $body:tt) => {
131        test_for_common_integer_dtypes!($name, $body);
132        test_for_float_dtypes!($name, $body);
133    };
134}
135
136
137#[macro_export]
138macro_rules! test_for_all_dtypes {
139    ($name:ident, $body:tt) => {
140        test_for_all_numeric_dtypes!($name, $body);
141        implement_test_for_dtypes!($name, $body,
142            bool
143        );
144    };
145}
146
147#[macro_export]
148macro_rules! assert_almost_eq {
149    ($left:expr, $right:expr, $tol:expr) => {
150        if ((&($left) - &($right)).max().flatiter().next().unwrap() as f64).abs() > $tol {
151            assert_eq!($left, $right);
152        }
153    };
154
155    ($left:expr, $right:expr) => {
156        assert_almost_eq!($left, $right, 1e-5);
157    };
158}
159
160#[macro_export]
161macro_rules! first_n_elements {
162    ($arr:expr, $n:expr) => {{
163        &$arr[0..$n].try_into().unwrap()
164    }};
165}
166
167#[macro_export]
168macro_rules! impl_default_trait_for_dtypes {
169    ($trait_name:ident, $($default_dtypes:ty),*) => {
170        $(
171            impl $trait_name for $default_dtypes {}
172        )*
173    };
174}