2 releases

Uses new Rust 2024

0.1.1 Apr 12, 2026
0.1.0 Apr 12, 2026

#373 in Procedural macros


Used in 2 crates

MIT license

10KB
229 lines

Macro-data

Utitlies to transfer data between procedural macros in rust.

Example

This example shows how to do data transfer between procedural macros in rust.

#[derive(macro_data::Save)]
struct Wow {
    a: usize,
}

#[derive(macro_data::Save)]
struct Cool {
    b: i32,
}

/// Merge `Wow` and `Cool` members, this is done in a procedural macro.
macro_data::combine!(Wow, Cool);

fn main() {
    let wow = Wow { a: 1 };
    let cool = Cool { b: 2 };

    let wow_cool = WowCool {
        a: wow.a,
        b: cool.b,
    };

    println!("{} {}", wow_cool.a, wow_cool.b);
}

Overview

  1. Save data as tokens in a macro (macro_rules!)
  2. Your procedural macro creates a call to the transfer macro
  3. The transfer macro replaces all instances of @load(<path>) with the saved tokens
  4. Another procedural macro is invoked with the transformed tokens
  5. This macro implements the wanted functionality

Usage

  • Add macro-data as a dependency
  • Use the proc-macro feature in your proc-macro = true crate
    • This enables utilities to save, transfer and load data between procedural macros
    • Create a procedural macro to save the data with macro_data::save
    • Create a public procedural macro to parse the input and call macro_data::transfer
    • Create a public hidden procedural macro for the final logic
      • This macro is called with the transferred data, which includes saved data
  • Reexport your macros in your public crate
  • Reexport macro-data::macro_data_transfer! in your public crate
    • The end-user only needs a single dependency
    • The macro needs to be public, but is only called in generated code

Dependencies

~110KB