xmacro 0.1.0

rust macro producing multiple expansions
Documentation

XMacro

This crate provides two things:

  1. A library to generate code by repetively expanding macros. This is the primary objective of this crate as it enables code generation in a convenient way from other proc macros.
  2. The standalone xmacro!{} and xmacro_items!{} macros to generate code using the library as it is useful on its own.

This crate supersedes the code_product crate.

XMacro expansion example

The name product is because it expands to the product (each by each) of all defined sets. For example given are the two sets of defintions 'Foo and Bar' and 'This and That', showing different syntactic variants:

# use xmacro::xmacro;
# trait Trait<T>{}
# struct This<T>(T); struct That<T>(T);
# struct Foo; struct Bar;
xmacro!{
    // Rather elaborate form with named definitions:
    // define `Type` to expand to `This` and `That`
    $(Type: (This) (That))
    // and inline define `T` to expand to `Foo` and `Bar`
    impl Trait<$($T: (Foo)(Bar))> for $Type<$T> {}
}

or

# use xmacro::xmacro;
# trait Trait<T>{}
# struct This<T>(T); struct That<T>(T);
# struct Foo; struct Bar;
xmacro!{
    // Alternative form inlining definition and reference by index:
    impl Trait<$((Foo)(Bar))> for $((This)(That))<$0> {}
}

either of the above will expand four times to:

# trait Trait<T>{}
# struct This<T>(T); struct That<T>(T);
# struct Foo; struct Bar;
impl Trait<Foo> for This<Foo> {}
impl Trait<Foo> for That<Foo> {}
impl Trait<Bar> for This<Bar> {}
impl Trait<Bar> for That<Bar> {}

Please refer to the README of the xmacro_lib crate for a elaborate description of the macro syntax and semantic.