match-template

Crates.iomatch-template
lib.rsmatch-template
version1.0.0
created_at2022-07-19 08:50:00.898923+00
updated_at2025-05-06 14:51:59.056835+00
descriptionmatch-template is a procedural macro that generates repeated match arms by pattern.
homepage
repositoryhttps://github.com/tisonkun/match-template
max_upload_size
id628188
size26,215
tison (tisonkun)

documentation

README

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.

Commit count: 4

cargo fmt