Code Monkey home page Code Monkey logo

comptime-rs's Introduction

Lightweight compile-time expression evaluation. This crate is inspired by Zig's comptime.

The expression returned by the contents of the comptime macro invocation will be parsed as Rust source code and inserted back into the call site.

tl;dr: comptime! gives you no-context anonynmous proc macros.

Example

#![feature(proc_macro_hygiene)]
fn main() {
    println!(concat!(
        "The program was compiled on ",
        comptime::comptime! {
            chrono::Utc::now().format("%Y-%m-%d").to_string()
        },
        "."
    )); // The program was compiled on 2019-08-30.
}

Limitations

Unlike the real comptime, comptime! does not have access to the scope in which it is invoked. The code in comptime! is run as its own script. Though, technically, you could interpolate static values using quote!.

Also, comptime! requires you to run cargo build at least once before cargo (clippy|check) will work since comptime! does not compile dependencies.

Contributing

Please do! Ideally, rustc would also have (real) comptime which would have access to type information and other static values. In the meantime, this should be a nice way to approximate and experiment with such functionality.

comptime-rs's People

Contributors

alexpyattaev avatar nhynes avatar

Forkers

andrewlowndes

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.