match_vector_pair

Macro match_vector_pair 

Source
macro_rules! match_vector_pair {
    ($left:expr, $right:expr, | $a:ident : Vector, $b:ident : Vector | $body:expr) => { ... };
    ($left:expr, $right:expr, | $a:ident : Vector, $b:ident : VectorMut | $body:expr) => { ... };
    ($left:expr, $right:expr, | $a:ident : VectorMut, $b:ident : Vector | $body:expr) => { ... };
    ($left:expr, $right:expr, | $a:ident : VectorMut, $b:ident : VectorMut | $body:expr) => { ... };
}
Expand description

Matches on pairs of vector variants and executes the same code for matching variant pairs.

This macro eliminates repetitive match statements when implementing operations that need to work with pairs of vectors where the variants must match.

Specify the types of the left and right vectors (either Vector or VectorMut) and the macro generates the appropriate match arms.

The macro binds the matched inner values to identifiers in the closure that can be used in the body expression.

ยงExamples

use vortex_vector::{Vector, VectorMut, VectorMutOps, match_vector_pair};
use vortex_vector::bool::{BoolVector, BoolVectorMut};

fn extend_vector(left: &mut VectorMut, right: &Vector) {
    match_vector_pair!(left, right, |a: VectorMut, b: Vector| {
        a.extend_from_vector(b);
    })
}

let mut mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();

extend_vector(&mut mut_vec, &vec);
assert_eq!(mut_vec.len(), 5);

Note that the vectors can also be owned:

use vortex_vector::{Vector, VectorMut, VectorMutOps, match_vector_pair};
use vortex_vector::bool::{BoolVector, BoolVectorMut};

fn extend_vector_owned(mut dest: VectorMut, src: Vector) -> VectorMut {
    match_vector_pair!(&mut dest, src, |a: VectorMut, b: Vector| {
        a.extend_from_vector(&b);
        dest
    })
}

let mut_vec: VectorMut = BoolVectorMut::from_iter([true, false, true]).into();
let vec: Vector = BoolVectorMut::from_iter([false, true]).freeze().into();

let new_bool_mut = extend_vector_owned(mut_vec, vec);
assert_eq!(new_bool_mut.len(), 5);