Skip to content

Commit e1a7962

Browse files
committed
implement match_vector_pair
Signed-off-by: Connor Tsui <[email protected]>
1 parent e00961b commit e1a7962

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

vortex-vector/src/macros.rs

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ macro_rules! match_each_vector {
6767
}};
6868
}
6969

70-
pub(crate) use match_each_vector;
71-
7270
/// Matches on all variants of [`VectorMut`] and executes the same code for each variant branch.
7371
///
7472
/// This macro eliminates repetitive match statements when implementing operations that need to work
@@ -132,4 +130,119 @@ macro_rules! match_each_vector_mut {
132130
}};
133131
}
134132

135-
pub(crate) use match_each_vector_mut;
133+
/// Internal macro to generate match arms for vector pairs.
134+
#[doc(hidden)]
135+
#[macro_export]
136+
macro_rules! __match_vector_pair_arms {
137+
(
138+
$left:expr,
139+
$right:expr,
140+
$enum_left:ident,
141+
$enum_right:ident,
142+
$a:ident,
143+
$b:ident,
144+
$body:expr
145+
) => {{
146+
match ($left, $right) {
147+
($crate::$enum_left::Null(left), $crate::$enum_right::Null(right)) => {
148+
let $a = left;
149+
let $b = right;
150+
$body
151+
}
152+
($crate::$enum_left::Bool(left), $crate::$enum_right::Bool(right)) => {
153+
let $a = left;
154+
let $b = right;
155+
$body
156+
}
157+
($crate::$enum_left::Primitive(left), $crate::$enum_right::Primitive(right)) => {
158+
let $a = left;
159+
let $b = right;
160+
$body
161+
}
162+
($crate::$enum_left::String(left), $crate::$enum_right::String(right)) => {
163+
let $a = left;
164+
let $b = right;
165+
$body
166+
}
167+
($crate::$enum_left::Binary(left), $crate::$enum_right::Binary(right)) => {
168+
let $a = left;
169+
let $b = right;
170+
$body
171+
}
172+
($crate::$enum_left::Struct(left), $crate::$enum_right::Struct(right)) => {
173+
let $a = left;
174+
let $b = right;
175+
$body
176+
}
177+
_ => ::vortex_error::vortex_panic!("Mismatched vector types"),
178+
}
179+
}};
180+
}
181+
182+
/// Matches on pairs of vector variants and executes the same code for matching variant pairs.
183+
///
184+
/// This macro eliminates repetitive match statements when implementing operations that need to work
185+
/// with pairs of vectors where the variants must match.
186+
///
187+
/// Specify the types of the left and right vectors (either `Vector` or `VectorMut`) and the macro
188+
/// generates the appropriate match arms.
189+
///
190+
/// The macro binds the matched inner values to identifiers in the closure that can be used in the
191+
/// body expression.
192+
///
193+
/// # Examples
194+
///
195+
/// ```
196+
/// use vortex_vector::{
197+
/// BoolVector, BoolVectorMut, Vector, VectorMut, VectorMutOps, match_vector_pair
198+
/// };
199+
///
200+
/// fn extend_vector(left: &mut VectorMut, right: &Vector) {
201+
/// match_vector_pair!(left, right, |a: VectorMut, b: Vector| {
202+
/// a.extend_from_vector(b);
203+
/// })
204+
/// }
205+
///
206+
/// let mut mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
207+
/// let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();
208+
///
209+
/// extend_vector(&mut mut_vec, &vec);
210+
/// assert_eq!(mut_vec.len(), 5);
211+
/// ```
212+
///
213+
/// Note that the vectors can also be owned:
214+
///
215+
/// ```
216+
/// use vortex_vector::{
217+
/// BoolVector, BoolVectorMut, Vector, VectorMut, VectorMutOps, match_vector_pair
218+
/// };
219+
///
220+
/// fn extend_vector_owned(mut dest: VectorMut, src: Vector) -> VectorMut {
221+
/// match_vector_pair!(&mut dest, src, |a: VectorMut, b: Vector| {
222+
/// a.extend_from_vector(&b);
223+
/// dest
224+
/// })
225+
/// }
226+
///
227+
/// let mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
228+
/// let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();
229+
///
230+
/// let new_bool_mut = extend_vector_owned(mut_vec, vec);
231+
/// assert_eq!(new_bool_mut.len(), 5);
232+
/// ```
233+
#[rustfmt::skip]
234+
#[macro_export]
235+
macro_rules! match_vector_pair {
236+
($left:expr, $right:expr, | $a:ident: Vector, $b:ident: Vector | $body:expr) => {{
237+
$crate::__match_vector_pair_arms!($left, $right, Vector, Vector, $a, $b, $body)
238+
}};
239+
($left:expr, $right:expr, | $a:ident: Vector, $b:ident: VectorMut | $body:expr) => {{
240+
$crate::__match_vector_pair_arms!($left, $right, Vector, VectorMut, $a, $b, $body)
241+
}};
242+
($left:expr, $right:expr, | $a:ident: VectorMut, $b:ident: Vector | $body:expr) => {{
243+
$crate::__match_vector_pair_arms!($left, $right, VectorMut, Vector, $a, $b, $body)
244+
}};
245+
($left:expr, $right:expr, | $a:ident: VectorMut, $b:ident: VectorMut | $body:expr) => {{
246+
$crate::__match_vector_pair_arms!($left, $right, VectorMut, VectorMut, $a, $b, $body)
247+
}};
248+
}

0 commit comments

Comments
 (0)