Skip to main content

tract_data/
macros.rs

1#[macro_export]
2macro_rules! tvec {
3    // count helper: transform any expression into 1
4    (@one $x:expr) => (1usize);
5    ($elem:expr; $n:expr) => ({
6        $crate::TVec::from_elem($elem, $n)
7    });
8    ($($x:expr),*$(,)*) => ({
9        let count = 0usize $(+ tvec!(@one $x))*;
10        #[allow(unused_mut)]
11        let mut vec = $crate::TVec::new();
12        if count <= vec.inline_size() {
13            $(vec.push($x);)*
14            vec
15        } else {
16            $crate::TVec::from_vec(vec![$($x,)*])
17        }
18    });
19}
20
21#[macro_export]
22macro_rules! dispatch_datum {
23    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
24        use $crate::prelude::DatumType;
25        #[allow(unexpected_cfgs)]
26        match $dt {
27            DatumType::Bool => $($path)::*::<bool>($($args),*),
28            DatumType::U8   => $($path)::*::<u8>($($args),*),
29            DatumType::U16  => $($path)::*::<u16>($($args),*),
30            DatumType::U32  => $($path)::*::<u32>($($args),*),
31            DatumType::U64  => $($path)::*::<u64>($($args),*),
32            DatumType::I8   => $($path)::*::<i8>($($args),*),
33            DatumType::I16  => $($path)::*::<i16>($($args),*),
34            DatumType::I32  => $($path)::*::<i32>($($args),*),
35            DatumType::I64  => $($path)::*::<i64>($($args),*),
36            DatumType::F16  => $($path)::*::<f16>($($args),*),
37            DatumType::F32  => $($path)::*::<f32>($($args),*),
38            DatumType::F64  => $($path)::*::<f64>($($args),*),
39            DatumType::Blob => $($path)::*::<$crate::prelude::Blob>($($args),*),
40            DatumType::TDim => $($path)::*::<TDim>($($args),*),
41            DatumType::String => $($path)::*::<String>($($args),*),
42            DatumType::QI8(_) => $($path)::*::<i8>($($args),*),
43            DatumType::QU8(_) => $($path)::*::<u8>($($args),*),
44            DatumType::QI32(_) => $($path)::*::<i32>($($args),*),
45            #[cfg(feature = "complex")]
46            DatumType::ComplexI16 => $($path)::*::<Complex<i16>>($($args),*),
47            #[cfg(feature = "complex")]
48            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
49            #[cfg(feature = "complex")]
50            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
51            #[cfg(feature = "complex")]
52            DatumType::ComplexF16 => $($path)::*::<Complex<f16>>($($args),*),
53            #[cfg(feature = "complex")]
54            DatumType::ComplexF32 => $($path)::*::<Complex<f32>>($($args),*),
55            #[cfg(feature = "complex")]
56            DatumType::ComplexF64 => $($path)::*::<Complex<f64>>($($args),*),
57        }
58    } }
59}
60
61#[macro_export]
62macro_rules! dispatch_datum_by_size {
63    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
64        use $crate::prelude::DatumType;
65        #[allow(unexpected_cfgs)]
66        match $dt {
67            DatumType::Bool => $($path)::*::<i8>($($args),*),
68            DatumType::U8   => $($path)::*::<i8>($($args),*),
69            DatumType::U16  => $($path)::*::<i16>($($args),*),
70            DatumType::U32  => $($path)::*::<i32>($($args),*),
71            DatumType::U64  => $($path)::*::<i64>($($args),*),
72            DatumType::I8   => $($path)::*::<i8>($($args),*),
73            DatumType::I16  => $($path)::*::<i16>($($args),*),
74            DatumType::I32  => $($path)::*::<i32>($($args),*),
75            DatumType::I64  => $($path)::*::<i64>($($args),*),
76            DatumType::F16  => $($path)::*::<i16>($($args),*),
77            DatumType::F32  => $($path)::*::<i32>($($args),*),
78            DatumType::F64  => $($path)::*::<i64>($($args),*),
79            DatumType::Blob => $($path)::*::<Blob>($($args),*),
80            DatumType::TDim => $($path)::*::<TDim>($($args),*),
81            DatumType::String => $($path)::*::<String>($($args),*),
82            DatumType::QI8(_)   => $($path)::*::<i8>($($args),*),
83            DatumType::QU8(_)   => $($path)::*::<u8>($($args),*),
84            DatumType::QI32(_)   => $($path)::*::<i32>($($args),*),
85            #[cfg(feature = "complex")]
86            DatumType::ComplexI16 => $($path)::*::<Complex<i16>>($($args),*),
87            #[cfg(feature = "complex")]
88            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
89            #[cfg(feature = "complex")]
90            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
91            #[cfg(feature = "complex")]
92            DatumType::ComplexF16 => $($path)::*::<Complex<f16>>($($args),*),
93            #[cfg(feature = "complex")]
94            DatumType::ComplexF32 => $($path)::*::<Complex<f32>>($($args),*),
95            #[cfg(feature = "complex")]
96            DatumType::ComplexF64 => $($path)::*::<Complex<f64>>($($args),*),
97        }
98    } }
99}
100
101#[macro_export]
102macro_rules! dispatch_copy {
103    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
104        use $crate::prelude::DatumType;
105        #[allow(unexpected_cfgs)]
106        match $dt {
107            DatumType::Bool => $($path)::*::<bool>($($args),*),
108            DatumType::U8   => $($path)::*::<u8>($($args),*),
109            DatumType::U16  => $($path)::*::<u16>($($args),*),
110            DatumType::U32  => $($path)::*::<u32>($($args),*),
111            DatumType::U64  => $($path)::*::<u64>($($args),*),
112            DatumType::I8   => $($path)::*::<i8>($($args),*),
113            DatumType::I16  => $($path)::*::<i16>($($args),*),
114            DatumType::I32  => $($path)::*::<i32>($($args),*),
115            DatumType::I64  => $($path)::*::<i64>($($args),*),
116            DatumType::F16  => $($path)::*::<f16>($($args),*),
117            DatumType::F32  => $($path)::*::<f32>($($args),*),
118            DatumType::F64  => $($path)::*::<f64>($($args),*),
119            DatumType::QI8(_)  => $($path)::*::<i8>($($args),*),
120            DatumType::QU8(_)  => $($path)::*::<u8>($($args),*),
121            DatumType::QI32(_)  => $($path)::*::<u8>($($args),*),
122            #[cfg(feature = "complex")]
123            DatumType::ComplexI16 => $($path)::*::<Complex<i16>>($($args),*),
124            #[cfg(feature = "complex")]
125            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
126            #[cfg(feature = "complex")]
127            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
128            #[cfg(feature = "complex")]
129            DatumType::ComplexF16 => $($path)::*::<Complex<f16>>($($args),*),
130            #[cfg(feature = "complex")]
131            DatumType::ComplexF32 => $($path)::*::<Complex<f32>>($($args),*),
132            #[cfg(feature = "complex")]
133            DatumType::ComplexF64 => $($path)::*::<Complex<f64>>($($args),*),
134            _ => panic!("{:?} is not Copy", $dt)
135        }
136    } }
137}
138
139#[macro_export]
140macro_rules! dispatch_copy_by_size {
141    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
142        use $crate::prelude::DatumType;
143        #[allow(unexpected_cfgs)]
144        match $dt {
145            DatumType::Bool => $($path)::*::<i8>($($args),*),
146            DatumType::U8   => $($path)::*::<i8>($($args),*),
147            DatumType::U16  => $($path)::*::<i16>($($args),*),
148            DatumType::U32  => $($path)::*::<i32>($($args),*),
149            DatumType::U64  => $($path)::*::<i64>($($args),*),
150            DatumType::I8   => $($path)::*::<i8>($($args),*),
151            DatumType::I16  => $($path)::*::<i16>($($args),*),
152            DatumType::I32  => $($path)::*::<i32>($($args),*),
153            DatumType::I64  => $($path)::*::<i64>($($args),*),
154            DatumType::F16  => $($path)::*::<i16>($($args),*),
155            DatumType::F32  => $($path)::*::<i32>($($args),*),
156            DatumType::F64  => $($path)::*::<i64>($($args),*),
157            DatumType::QI8(_)  => $($path)::*::<i8>($($args),*),
158            DatumType::QU8(_)  => $($path)::*::<u8>($($args),*),
159            DatumType::QI32(_)  => $($path)::*::<i32>($($args),*),
160            #[cfg(feature = "complex")]
161            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
162            #[cfg(feature = "complex")]
163            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
164            #[cfg(feature = "complex")]
165            DatumType::ComplexF16 => $($path)::*::<Complex<f16>>($($args),*),
166            #[cfg(feature = "complex")]
167            DatumType::ComplexF32 => $($path)::*::<Complex<f32>>($($args),*),
168            #[cfg(feature = "complex")]
169            DatumType::ComplexF64 => $($path)::*::<Complex<f64>>($($args),*),
170            _ => panic!("{:?} is not Copy", $dt)
171        }
172    } }
173}
174
175#[macro_export]
176macro_rules! dispatch_numbers {
177    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
178        use $crate::prelude::DatumType;
179        match $dt {
180            DatumType::U8   => $($path)::*::<u8>($($args),*),
181            DatumType::U16  => $($path)::*::<u16>($($args),*),
182            DatumType::U32  => $($path)::*::<u32>($($args),*),
183            DatumType::U64  => $($path)::*::<u64>($($args),*),
184            DatumType::I8   => $($path)::*::<i8>($($args),*),
185            DatumType::I16  => $($path)::*::<i16>($($args),*),
186            DatumType::I32  => $($path)::*::<i32>($($args),*),
187            DatumType::I64  => $($path)::*::<i64>($($args),*),
188            DatumType::F16  => $($path)::*::<f16>($($args),*),
189            DatumType::F32  => $($path)::*::<f32>($($args),*),
190            DatumType::F64  => $($path)::*::<f64>($($args),*),
191            DatumType::QI8(_)  => $($path)::*::<i8>($($args),*),
192            DatumType::QU8(_)  => $($path)::*::<u8>($($args),*),
193            DatumType::QI32(_)  => $($path)::*::<i32>($($args),*),
194            _ => $crate::internal::bail!("{:?} is not a number", $dt)
195        }
196    } }
197}
198
199#[macro_export]
200macro_rules! dispatch_zerolike {
201    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
202        use $crate::prelude::DatumType;
203        match $dt {
204            DatumType::TDim => $($path)::*::<TDim>($($args),*),
205            DatumType::U8   => $($path)::*::<u8>($($args),*),
206            DatumType::U16  => $($path)::*::<u16>($($args),*),
207            DatumType::U32  => $($path)::*::<u32>($($args),*),
208            DatumType::U64  => $($path)::*::<u64>($($args),*),
209            DatumType::I8   => $($path)::*::<i8>($($args),*),
210            DatumType::I16  => $($path)::*::<i16>($($args),*),
211            DatumType::I32  => $($path)::*::<i32>($($args),*),
212            DatumType::I64  => $($path)::*::<i64>($($args),*),
213            DatumType::F16  => $($path)::*::<f16>($($args),*),
214            DatumType::F32  => $($path)::*::<f32>($($args),*),
215            DatumType::F64  => $($path)::*::<f64>($($args),*),
216            DatumType::QI8(_)  => $($path)::*::<i8>($($args),*),
217            DatumType::QU8(_)  => $($path)::*::<u8>($($args),*),
218            DatumType::QI32(_)  => $($path)::*::<i32>($($args),*),
219            #[cfg(feature = "complex")]
220            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
221            #[cfg(feature = "complex")]
222            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
223            #[cfg(feature = "complex")]
224            DatumType::ComplexF16 => $($path)::*::<Complex<f16>>($($args),*),
225            #[cfg(feature = "complex")]
226            DatumType::ComplexF32 => $($path)::*::<Complex<f32>>($($args),*),
227            #[cfg(feature = "complex")]
228            DatumType::ComplexF64 => $($path)::*::<Complex<f64>>($($args),*),
229            _ => $crate::internal::bail!("{:?} doesn't implement num_traits::Zero", $dt)
230        }
231    } }
232}
233
234#[macro_export]
235macro_rules! dispatch_floatlike {
236    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
237        use $crate::prelude::DatumType;
238        match $dt {
239            DatumType::F16  => $($path)::*::<f16>($($args),*),
240            DatumType::F32  => $($path)::*::<f32>($($args),*),
241            DatumType::F64  => $($path)::*::<f64>($($args),*),
242            _ => $crate::internal::bail!("{:?} is not float-like", $dt)
243        }
244    } }
245}
246
247#[macro_export]
248macro_rules! dispatch_signed {
249    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
250        use $crate::prelude::DatumType;
251        match $dt {
252            DatumType::F16  => $($path)::*::<f16>($($args),*),
253            DatumType::F32  => $($path)::*::<f32>($($args),*),
254            DatumType::F64  => $($path)::*::<f64>($($args),*),
255            DatumType::I8   => $($path)::*::<i8>($($args),*),
256            DatumType::I16  => $($path)::*::<i16>($($args),*),
257            DatumType::I32  => $($path)::*::<i32>($($args),*),
258            DatumType::I64  => $($path)::*::<i64>($($args),*),
259            DatumType::TDim => $($path)::*::<TDim>($($args),*),
260            _ => $crate::internal::bail!("{:?} is not signed", $dt)
261        }
262    } }
263}
264
265#[macro_export]
266macro_rules! dispatch_hash {
267    ($($path:ident)::* ($dt:expr) ($($args:expr),*)) => { {
268        use $crate::prelude::DatumType;
269        #[allow(unexpected_cfgs)]
270        match $dt {
271            DatumType::Bool => $($path)::*::<bool>($($args),*),
272            DatumType::U8   => $($path)::*::<u8>($($args),*),
273            DatumType::U16  => $($path)::*::<u16>($($args),*),
274            DatumType::U32  => $($path)::*::<u32>($($args),*),
275            DatumType::U64  => $($path)::*::<u64>($($args),*),
276            DatumType::I8   => $($path)::*::<i8>($($args),*),
277            DatumType::I16  => $($path)::*::<i16>($($args),*),
278            DatumType::I32  => $($path)::*::<i32>($($args),*),
279            DatumType::I64  => $($path)::*::<i64>($($args),*),
280            DatumType::Blob => $($path)::*::<Blob>($($args),*),
281            DatumType::TDim => $($path)::*::<TDim>($($args),*),
282            DatumType::String => $($path)::*::<String>($($args),*),
283            #[cfg(feature="complex")]
284            DatumType::ComplexI16 => $($path)::*::<Complex<i16>>($($args),*),
285            #[cfg(feature="complex")]
286            DatumType::ComplexI32 => $($path)::*::<Complex<i32>>($($args),*),
287            #[cfg(feature="complex")]
288            DatumType::ComplexI64 => $($path)::*::<Complex<i64>>($($args),*),
289            _ => $crate::internal::bail!("{:?} is not Hash", $dt)
290        }
291    } }
292}