GithubHelp home page GithubHelp logo

metamatch's Introduction

metamatch!

githubgithub-buildcrates-iomsrvdocs-rs

A rust proc-macro for generating repetitive match arms.

Unless the enum variant type remains unused, match arms for different variants cannot be combined, even if the match arm bodies are syntactically identical.

This macro implements a simple templating attribute (#[expand]) to automatically stamp out the neccessary copies.

Rustfmt and rust-analyzer are fully able to reason about this macro. Even auto refactorings that affect the #[expand] (like changing the name of an enum variant) work correctly.

Example

use metamatch::metamatch;

enum DynVec {
    I32(Vec<i32>),
    I64(Vec<i64>),
    F32(Vec<f32>),
    F64(Vec<f64>),
}

enum DynSlice<'a> {
    I32(&'a [i32]),
    I64(&'a [i64]),
    F32(&'a [f32]),
    F64(&'a [f64]),
}

impl DynVec {
    fn as_slice(&self) -> DynSlice<'_> {
        metamatch!(match self {
            #[expand(T in [I32, I64, F32, F64])]
            DynVec::T(v) => DynSlice::T(v),
        })
    }

    fn promote_to_64(&mut self) {
        metamatch!(match self {
            // multiple replacement expressions supported
            #[expand((SRC, TGT, TYPE) in [
                (I32, I64, i64),
                (F32, F64, f64),
            ])]
            DynVec::SRC(v) => {
                *self = DynVec::TGT(
                    std::mem::take(v).into_iter().map(|v| v as TYPE).collect(),
                );
            }

            // the types are unused, the match body can be shared
            #[expand_pattern(T in [I64, F64])]
            DynVec::T(_) => (),
        })
    }
}

License

MIT

metamatch's People

Contributors

cmrschwarz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.