vortex_vector/primitive/
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 [`PrimitiveVector`] and
5//! [`PrimitiveVectorMut`].
6//!
7//! [`PrimitiveVector`]: crate::primitive::PrimitiveVector
8//! [`PrimitiveVectorMut`]: crate::primitive::PrimitiveVectorMut
9
10/// Matches on all primitive type variants of [`PrimitiveVector`] and executes the same code for
11/// each variant branch.
12///
13/// This macro eliminates repetitive match statements when implementing operations that need to work
14/// uniformly across all primitive type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`, `I32`,
15/// `I64`, `F16`, `F32`, `F64`).
16///
17/// # Examples
18///
19/// ```
20/// use vortex_vector::primitive::{PrimitiveVector, PVectorMut};
21/// use vortex_vector::{VectorOps, VectorMutOps, match_each_pvector};
22///
23/// fn get_primitive_len(vector: &PrimitiveVector) -> usize {
24///     match_each_pvector!(vector, |v| { v.len() })
25/// }
26///
27/// // Works with `I32` primitive vectors.
28/// let i32_vec: PrimitiveVector = PVectorMut::<i32>::from_iter([1, 2, 3].map(Some))
29///     .freeze()
30///     .into();
31/// assert_eq!(get_primitive_len(&i32_vec), 3);
32///
33/// // Works with `F64` primitive vectors.
34/// let f64_vec: PrimitiveVector = PVectorMut::<f64>::from_iter([1.0, 2.5].map(Some))
35///     .freeze()
36///     .into();
37/// assert_eq!(get_primitive_len(&f64_vec), 2);
38/// ```
39///
40/// Note: The `len` method is already provided by the [`VectorOps`] trait implementation.
41///
42/// [`PrimitiveVector`]: crate::primitive::PrimitiveVector
43/// [`VectorOps`]: crate::VectorOps
44#[macro_export]
45macro_rules! match_each_pvector {
46    ($self:expr, | $vec:ident | $body:block) => {{
47        match $self {
48            $crate::primitive::PrimitiveVector::U8($vec) => $body,
49            $crate::primitive::PrimitiveVector::U16($vec) => $body,
50            $crate::primitive::PrimitiveVector::U32($vec) => $body,
51            $crate::primitive::PrimitiveVector::U64($vec) => $body,
52            $crate::primitive::PrimitiveVector::I8($vec) => $body,
53            $crate::primitive::PrimitiveVector::I16($vec) => $body,
54            $crate::primitive::PrimitiveVector::I32($vec) => $body,
55            $crate::primitive::PrimitiveVector::I64($vec) => $body,
56            $crate::primitive::PrimitiveVector::F16($vec) => $body,
57            $crate::primitive::PrimitiveVector::F32($vec) => $body,
58            $crate::primitive::PrimitiveVector::F64($vec) => $body,
59        }
60    }};
61}
62
63/// Matches on all integer type variants of [`PrimitiveVector`] and executes the same code for each
64/// of the integer variant branches.
65///
66/// This macro eliminates repetitive match statements when implementing operations that need to work
67/// uniformly across all integer type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`, `I32`,
68/// `I64`).
69///
70/// See [`match_each_pvector`] for similar usage.
71///
72/// [`PrimitiveVector`]: crate::primitive::PrimitiveVector
73///
74/// # Panics
75///
76/// Panics if the vector passed in to the macro is a float vector variant.
77#[macro_export]
78macro_rules! match_each_integer_pvector {
79    ($self:expr, | $vec:ident | $body:block) => {{
80        match $self {
81            $crate::primitive::PrimitiveVector::U8($vec) => $body,
82            $crate::primitive::PrimitiveVector::U16($vec) => $body,
83            $crate::primitive::PrimitiveVector::U32($vec) => $body,
84            $crate::primitive::PrimitiveVector::U64($vec) => $body,
85            $crate::primitive::PrimitiveVector::I8($vec) => $body,
86            $crate::primitive::PrimitiveVector::I16($vec) => $body,
87            $crate::primitive::PrimitiveVector::I32($vec) => $body,
88            $crate::primitive::PrimitiveVector::I64($vec) => $body,
89            $crate::primitive::PrimitiveVector::F16(_)
90            | $crate::primitive::PrimitiveVector::F32(_)
91            | $crate::primitive::PrimitiveVector::F64(_) => {
92                ::vortex_error::vortex_panic!(
93                    "Tried to match a float vector in an integer match statement"
94                )
95            }
96        }
97    }};
98}
99
100/// Matches on all primitive type variants of [`PrimitiveVectorMut`] and executes the same code
101/// for each variant branch.
102///
103/// This macro eliminates repetitive match statements when implementing mutable operations that need
104/// to work uniformly across all primitive type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`,
105/// `I32`, `I64`, `F16`, `F32`, `F64`).
106///
107/// # Examples
108///
109/// ```
110/// use vortex_vector::primitive::{PrimitiveVectorMut, PVectorMut};
111/// use vortex_vector::{VectorMutOps, match_each_pvector_mut};
112///
113/// fn reserve_primitive_space(vector: &mut PrimitiveVectorMut, additional: usize) {
114///     match_each_pvector_mut!(vector, |v| { v.reserve(additional) })
115/// }
116///
117/// // Works with `U8` mutable primitive vectors.
118/// let mut u8_vec: PrimitiveVectorMut = PVectorMut::<u8>::from_iter([1, 2].map(Some)).into();
119/// reserve_primitive_space(&mut u8_vec, 10);
120/// assert!(u8_vec.capacity() >= 12);
121///
122/// // Works with `I64` mutable primitive vectors.
123/// let mut i64_vec: PrimitiveVectorMut = PVectorMut::<i64>::from_iter([100].map(Some)).into();
124/// reserve_primitive_space(&mut i64_vec, 5);
125/// assert!(i64_vec.capacity() >= 6);
126/// ```
127///
128/// Note: The `reserve` method is already provided by the [`VectorMutOps`] trait implementation.
129///
130/// [`PrimitiveVectorMut`]: crate::primitive::PrimitiveVectorMut
131/// [`VectorMutOps`]: crate::VectorMutOps
132#[macro_export]
133macro_rules! match_each_pvector_mut {
134    ($self:expr, | $vec:ident | $body:block) => {{
135        match $self {
136            $crate::primitive::PrimitiveVectorMut::U8($vec) => $body,
137            $crate::primitive::PrimitiveVectorMut::U16($vec) => $body,
138            $crate::primitive::PrimitiveVectorMut::U32($vec) => $body,
139            $crate::primitive::PrimitiveVectorMut::U64($vec) => $body,
140            $crate::primitive::PrimitiveVectorMut::I8($vec) => $body,
141            $crate::primitive::PrimitiveVectorMut::I16($vec) => $body,
142            $crate::primitive::PrimitiveVectorMut::I32($vec) => $body,
143            $crate::primitive::PrimitiveVectorMut::I64($vec) => $body,
144            $crate::primitive::PrimitiveVectorMut::F16($vec) => $body,
145            $crate::primitive::PrimitiveVectorMut::F32($vec) => $body,
146            $crate::primitive::PrimitiveVectorMut::F64($vec) => $body,
147        }
148    }};
149}
150
151/// Matches on all integer type variants of [`PrimitiveVectorMut`] and executes the same code for
152/// each of the integer variant branches.
153///
154/// This macro eliminates repetitive match statements when implementing operations that need to work
155/// uniformly across all integer type variants (`U8`, `U16`, `U32`, `U64`, `I8`, `I16`, `I32`,
156/// `I64`).
157///
158/// See [`match_each_pvector_mut`] for similar usage.
159///
160/// [`PrimitiveVectorMut`]: crate::primitive::PrimitiveVectorMut
161///
162/// # Panics
163///
164/// Panics if the vector passed in to the macro is a float vector variant.
165#[macro_export]
166macro_rules! match_each_integer_pvector_mut {
167    ($self:expr, | $vec:ident | $body:block) => {{
168        match $self {
169            $crate::primitive::PrimitiveVectorMut::U8($vec) => $body,
170            $crate::primitive::PrimitiveVectorMut::U16($vec) => $body,
171            $crate::primitive::PrimitiveVectorMut::U32($vec) => $body,
172            $crate::primitive::PrimitiveVectorMut::U64($vec) => $body,
173            $crate::primitive::PrimitiveVectorMut::I8($vec) => $body,
174            $crate::primitive::PrimitiveVectorMut::I16($vec) => $body,
175            $crate::primitive::PrimitiveVectorMut::I32($vec) => $body,
176            $crate::primitive::PrimitiveVectorMut::I64($vec) => $body,
177            $crate::primitive::PrimitiveVectorMut::F16(_)
178            | $crate::primitive::PrimitiveVectorMut::F32(_)
179            | $crate::primitive::PrimitiveVectorMut::F64(_) => {
180                ::vortex_error::vortex_panic!(
181                    "Tried to match a mutable float vector in an integer match statement"
182                )
183            }
184        }
185    }};
186}