Crate edg

source ·
Expand description

Compile-time expression evaluation. This crate is inspired by Zig’s comptime.

The passed closure will be evaluated at compile time.

Example

println!(
    "The program was compiled on {}.",
    // note how chrono::Utc is transported
    edg::r! { || -> chrono::DateTime<chrono::Utc> { chrono::Utc::now() } }.format("%Y-%m-%d").to_string()
); // The program was compiled on 2023-11-16.

Limitations

  • Unlike Zig, edg::r! does not have access to the scope in which it is invoked, as the closure in edg::r! is run as its own script.
  • Unfortunately, as serde is not const, you cant have const X: _ = edg::r! { .. }.
  • Each block must be compiled sequentially.

How it works

edg::r!:

  • adds serde_json::to_string to your code
  • creates a file edg-{hash}.rs, with your new code, in your target directory
  • compiles the file with rustc
  • executes the file
  • emits code to deserialize the json output.
Predecessor

Much of the code is from the comptime crate.

Macros