#pattern-match #macro #template

macro match-template

match-template is a procedural macro that generates repeated match arms by pattern

2 releases (1 stable)

new 1.0.0 May 6, 2025
0.0.1 Jul 19, 2022

#237 in Procedural macros

Download history 644/week @ 2025-01-17 375/week @ 2025-01-24 482/week @ 2025-01-31 859/week @ 2025-02-07 866/week @ 2025-02-14 733/week @ 2025-02-21 635/week @ 2025-02-28 817/week @ 2025-03-07 826/week @ 2025-03-14 710/week @ 2025-03-21 782/week @ 2025-03-28 900/week @ 2025-04-04 941/week @ 2025-04-11 1045/week @ 2025-04-18 747/week @ 2025-04-25 565/week @ 2025-05-02

3,463 downloads per month

Apache-2.0

13KB
196 lines

match-template

Crates.io Documentation Apache 2.0 licensed

Overview

match-template is a procedural macro that generates repeated match arms by pattern.

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 substituted 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, the following code

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).

License and Origins

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

This repository is a fork of tikv/match-template since the upstream is unmaintained. The original project is licensed under the Apache License, Version 2.0.

Dependencies

~220–660KB
~16K SLoC