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.
```rust trait Foo {}
struct Bar; struct Baz;
impl Foo for ((Bar, Baz)) {
} ```
This will generate the following code:
```rust impl Foo for Bar {
}
impl Foo for Baz {
} ```
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.
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.
```rust
trait GenericFoo
struct Bar; struct Baz;
struct One; struct Two; struct Three;
struct Alpha; struct Beta;
impl GenericFoo<((Bar, Baz)), ((Alpha, Beta))> for ((One, Two, Three)) { } ``` The above snippet will expand to 12 impl blocks (2 * 3 * 2).
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:
```rust
trait GenericFoo
struct Bar; struct Baz;
struct One; struct Two; struct Three;
struct Alpha; struct Beta;
impl GenericFoo<((Bar, Baz)), ((Alpha, Beta))> for ((One, Two, Three)) { fn myfn(arg1: TYPE0, _arg2: TYPE1) -> TYPE2 { unimplemented!() } } ```
The above code expands to the following (non-exhaustive list):
```rust
impl GenericFoo
impl GenericFoo
In other words, __TYPE0__
is an iterator-like placeholder for the first double-tuple ((Bar, Baz))
, __TYPE1__
for the second one, etc.