impl-template
impl-template
is a procedural macro for the Rust programming language that allows you to define templates for "impl"-items and have them expanded to several instances depending on the configuration.
In a way, you can think of it as compile-time blanket impls if you know all types you want to implement it for upfront.
Usage
;
;
This will generate the following code:
Advanced usage
impl-template
looks for patterns of double tuples.
Those are syntactically valid Rust code but AFAIK fairly useless and should thus not appear in day-to-day Rust-code.
Several double tuple patterns
You can have as many of those double-tuples as you want.
impl-template
will create a cartesian product out of all of them and generate the impl-blocks accordingly.
;
;
;
;
;
;
;
The above snippet will expand to 12 impl blocks (2 * 3 * 2).
Referring to types
impl-template
allows you to refer to types within the template block.
It generates a dummy identifier for every double-tuple in the scheme of __TYPE{}__
with {}
being replaced with a 0-based index.
We can extend GenericFoo
with a method where we have to name the type parameters:
;
;
;
;
;
;
;
The above code expands to the following (non-exhaustive list):
In other words, __TYPE0__
is an iterator-like placeholder for the first double-tuple ((Bar, Baz))
, __TYPE1__
for the second one, etc.