vortex_vector/decimal/
macros.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Helper macros for working with the different variants of [`DecimalVector`], [`DecimalScalar`],
5//! and [`DecimalVectorMut`].
6//!
7//! [`DecimalVector`]: super::DecimalVector
8//! [`DecimalScalar`]: super::DecimalScalar
9//! [`DecimalVectorMut`]: super::DecimalVectorMut
10
11/// Matches on all decimal type variants of [`DecimalVector`] and executes the same code for
12/// each variant branch.
13///
14/// This macro eliminates repetitive match statements when implementing operations that need to work
15/// uniformly across all decimal type variants (`D8`, `D16`, `D32`, `D64`, `D128`, `D256`).
16///
17/// [`DecimalVector`]: super::DecimalVector
18#[macro_export]
19macro_rules! match_each_dvector {
20    ($self:expr, | $vec:ident | $body:block) => {{
21        match $self {
22            $crate::decimal::DecimalVector::D8($vec) => $body,
23            $crate::decimal::DecimalVector::D16($vec) => $body,
24            $crate::decimal::DecimalVector::D32($vec) => $body,
25            $crate::decimal::DecimalVector::D64($vec) => $body,
26            $crate::decimal::DecimalVector::D128($vec) => $body,
27            $crate::decimal::DecimalVector::D256($vec) => $body,
28        }
29    }};
30}
31
32/// Matches on all decimal type variants of [`DecimalVectorMut`] and executes the same code
33/// for each variant branch.
34///
35/// This macro eliminates repetitive match statements when implementing mutable operations that need
36/// to work uniformly across all decimal type variants (`D8`, `D16`, `D32`, `D64`, `D128`, `D256`).
37///
38/// [`DecimalVectorMut`]: super::DecimalVectorMut
39#[macro_export]
40macro_rules! match_each_dvector_mut {
41    ($self:expr, | $vec:ident | $body:block) => {{
42        match $self {
43            $crate::decimal::DecimalVectorMut::D8($vec) => $body,
44            $crate::decimal::DecimalVectorMut::D16($vec) => $body,
45            $crate::decimal::DecimalVectorMut::D32($vec) => $body,
46            $crate::decimal::DecimalVectorMut::D64($vec) => $body,
47            $crate::decimal::DecimalVectorMut::D128($vec) => $body,
48            $crate::decimal::DecimalVectorMut::D256($vec) => $body,
49        }
50    }};
51}
52
53/// Matches on all decimal type variants of [`DecimalScalar`] and executes the same code for
54/// each variant branch.
55///
56/// This macro eliminates repetitive match statements when implementing operations that need to work
57/// uniformly across all decimal type variants (`D8`, `D16`, `D32`, `D64`, `D128`, `D256`).
58///
59/// Works with both owned `DecimalScalar` and `&DecimalScalar` (the bound variable will be
60/// `DScalar<D>` or `&DScalar<D>` respectively due to Rust's match ergonomics).
61///
62/// [`DecimalScalar`]: super::DecimalScalar
63#[macro_export]
64macro_rules! match_each_dscalar {
65    ($self:expr, | $scalar:ident | $body:block) => {{
66        match $self {
67            $crate::decimal::DecimalScalar::D8($scalar) => $body,
68            $crate::decimal::DecimalScalar::D16($scalar) => $body,
69            $crate::decimal::DecimalScalar::D32($scalar) => $body,
70            $crate::decimal::DecimalScalar::D64($scalar) => $body,
71            $crate::decimal::DecimalScalar::D128($scalar) => $body,
72            $crate::decimal::DecimalScalar::D256($scalar) => $body,
73        }
74    }};
75}
76
77/// Matches on pairs of [`DecimalVector`] with the same type and executes the provided code.
78///
79/// This macro matches two decimal vectors when they have the same underlying type.
80/// For type mismatches, the `$else` block is executed.
81///
82/// [`DecimalVector`]: super::DecimalVector
83#[macro_export]
84macro_rules! match_each_dvector_pair {
85    (($left:expr, $right:expr), | $l:ident, $r:ident | $body:block, $else:block) => {{
86        match ($left, $right) {
87            ($crate::decimal::DecimalVector::D8($l), $crate::decimal::DecimalVector::D8($r)) => {
88                $body
89            }
90            ($crate::decimal::DecimalVector::D16($l), $crate::decimal::DecimalVector::D16($r)) => {
91                $body
92            }
93            ($crate::decimal::DecimalVector::D32($l), $crate::decimal::DecimalVector::D32($r)) => {
94                $body
95            }
96            ($crate::decimal::DecimalVector::D64($l), $crate::decimal::DecimalVector::D64($r)) => {
97                $body
98            }
99            (
100                $crate::decimal::DecimalVector::D128($l),
101                $crate::decimal::DecimalVector::D128($r),
102            ) => $body,
103            (
104                $crate::decimal::DecimalVector::D256($l),
105                $crate::decimal::DecimalVector::D256($r),
106            ) => $body,
107            _ => $else,
108        }
109    }};
110}
111
112/// Matches on pairs of [`DecimalScalar`] with the same type and executes the provided code.
113///
114/// This macro matches two decimal scalars when they have the same underlying type.
115/// For type mismatches, the `$else` block is executed.
116///
117/// [`DecimalScalar`]: super::DecimalScalar
118#[macro_export]
119macro_rules! match_each_dscalar_pair {
120    (($left:expr, $right:expr), | $l:ident, $r:ident | $body:block, $else:block) => {{
121        match ($left, $right) {
122            ($crate::decimal::DecimalScalar::D8($l), $crate::decimal::DecimalScalar::D8($r)) => {
123                $body
124            }
125            ($crate::decimal::DecimalScalar::D16($l), $crate::decimal::DecimalScalar::D16($r)) => {
126                $body
127            }
128            ($crate::decimal::DecimalScalar::D32($l), $crate::decimal::DecimalScalar::D32($r)) => {
129                $body
130            }
131            ($crate::decimal::DecimalScalar::D64($l), $crate::decimal::DecimalScalar::D64($r)) => {
132                $body
133            }
134            (
135                $crate::decimal::DecimalScalar::D128($l),
136                $crate::decimal::DecimalScalar::D128($r),
137            ) => $body,
138            (
139                $crate::decimal::DecimalScalar::D256($l),
140                $crate::decimal::DecimalScalar::D256($r),
141            ) => $body,
142            _ => $else,
143        }
144    }};
145}