Code Monkey home page Code Monkey logo

rust-expr-macros's Introduction

Expr macros

An experiment of generating expression structures/enums in macros so they can be used directly in programs. If the properties of the struct are const-compatible the result from the macro can be used in a const context.

How it works

A trait ToExpr is defined in crates/core with implementation for std types. The signaure includes a function to_expr to print itself as tokens.

trait ToExpr {
    fn to_expr(&self) -> TokenStream;
}

There is a helper derive macro ToExpr defined in crates/macro that auto-implements the trait on structs and enums when defined.

Example usage

  1. Define a library of structs/enums you will create and use (see crates/example_lib)
  2. Define a macro that creats object instances and returns their expressions (see crates/example_macro)
  3. Use the macro and the library in your rust code (see crates/example_bin)

Run the demo via cargo run -p to-expr-example-bin.

Where this may be useful

This may prove useful where you want to use const expressions but generate the values for these in macros. Possible other usage would involve parsing config/json files via serde at compile-time.

rust-expr-macros's People

Contributors

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