risingwave_common

Macro dispatch_array_variants

source
macro_rules! dispatch_array_variants {
    ($impl:expr, [$($k:ident = $v:ident),*], $body:tt) => { ... };
    ($impl:expr, $inner:pat, $body:tt) => { ... };
    ($impl:expr, $inner:pat, [$($k:ident = $v:ident),*], $body:tt) => { ... };
}
Expand description

Dispatch the code block to all variants of ArrayImpl.

§Usage

The basic usage to access the inner concrete impl Array value is:

fn do_stuff<A: Array>(array: &A) { .. }

fn do_stuff_dispatch(array_impl: &ArrayImpl) {
    dispatch_array_variants!(array_impl, array, {
        do_stuff(array)
    })
}

One can also bind the inner concrete impl Array type to an alias:

fn do_stuff<A: Array>() { .. }

fn do_stuff_dispatch(array_impl: &ArrayImpl) {
    dispatch_array_variants!(array_impl, [A = Array], {
        do_stuff::<A>()
    })
}

There’re more to bind, including type aliases of associated ArrayBuilder, Scalar, and ScalarRef, or even the constant string of the variant name VARIANT_NAME. This can be achieved by writing one or more of them in the square brackets. Due to the limitation of macro, the order of the bindings matters.

fn do_stuff_dispatch(array_impl: &ArrayImpl) {
    dispatch_array_variants!(
        array_impl,
        [A = Array, B = ArrayBuilder, S = Scalar, R = ScalarRef, N = VARIANT_NAME],
        { .. }
    )
}

Alias bindings can also be used along with the inner value accessing:

fn do_stuff<A: Array>(array: &A) { .. }

fn do_stuff_dispatch(array_impl: &ArrayImpl) {
    dispatch_array_variants!(array_impl, array, [A = Array], {
        do_stuff::<A>(array)
    })
}