match_template!() { /* proc-macro */ }
Expand description

This crate provides a macro that can be used to append a match expression with multiple arms, where the tokens in the first arm, as a template, can be subsitituted and the template arm will be expanded into multiple arms.

For example, the following code

match_template! {
    T = [Int, Real, Double],
    match Foo {
        EvalType::T => { panic!("{}", EvalType::T); },
        EvalType::Other => unreachable!(),
    }
}

generates

match Foo {
    EvalType::Int => { panic!("{}", EvalType::Int); },
    EvalType::Real => { panic!("{}", EvalType::Real); },
    EvalType::Double => { panic!("{}", EvalType::Double); },
    EvalType::Other => unreachable!(),
}

In addition, substitution can vary on two sides of the arms.

For example,

match_template! {
    T = [Foo, Bar => Baz],
    match Foo {
        EvalType::T => { panic!("{}", EvalType::T); },
    }
}

generates

match Foo {
    EvalType::Foo => { panic!("{}", EvalType::Foo); },
    EvalType::Bar => { panic!("{}", EvalType::Baz); },
}

Wildcard match arm is also supported (but there will be no substitution).