Crate code_product

source ·
Expand description

§Code Product

This crate provides two things:

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

§Product expansion

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

product!{
    // 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

product!{
    // 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:

impl Trait<Foo> for This<Foo> {}
impl Trait<Foo> for That<Foo> {}
impl Trait<Bar> for This<Bar> {}
impl Trait<Bar> for That<Bar> {}

§Product Syntax

The detailed syntax for product definitions is described at: The product syntax.

Re-exports§

Macros§

  • Macro doing product expansions.
  • Macro doing product expansions per item. This means that (on toplevel) automatic scopes are generated up and including to the first braced block or semicolon. Then these scopes become independently expanded.