Crates.io | code-product |
lib.rs | code-product |
version | |
source | src |
created_at | 2023-12-26 02:32:17.356372 |
updated_at | 2024-12-31 22:13:14.068699 |
description | macro producing multiple expansions |
homepage | |
repository | https://git.pipapo.org/cehteh/code-product.git |
max_upload_size | |
id | 1080579 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
*** This crate is superseded by the xmacro/xmacro_lib crate ***
Migrate your projects to xmacros.
Don't use it for new Code.
This crate provides two things:
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. The Syntax is there as well.
The standalone product!{}
and product_items!{}
macros to
generate code using the library as it is useful on its own.
This macro system is useful to generate boilerplate code that repeats in similar ways.
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 code_product::product;
# trait Trait<T>{}
# struct This<T>(T); struct That<T>(T);
# struct Foo; struct Bar;
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
# use code_product::product;
# trait Trait<T>{}
# struct This<T>(T); struct That<T>(T);
# struct Foo; struct Bar;
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:
# 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> {}
In linear
expansion scopes in square brackets each definition has to define the same number
of itens. These are then iterated together. This gives more control over the expansions as
each possible combination has to be defined manually.
Exmaple: Pair Substr with &str and with CowStr and its reversed forms.
product! {
$[
$(Lhs: (SubStr)(&str)(SubStr)(CowStr))
$(Rhs: (&str)(SubStr)(CowStr)(SubStr))
impl PartialOrd<$Rhs> for $Lhs {
fn partial_cmp(&self, other: &$Rhs) -> Option<std::cmp::Ordering> {
(**self).partial_cmp(other)
}
}
]
}