Suppose that I have a
trait Happy {}
I can implement Happy
for whatever struct I might want, for example:
struct Dog;
struct Cat;
struct Alligator;
impl Happy for Dog {}
impl Happy for Cat {}
impl Happy for Alligator {}
Now, I would like to automatically impl
my Happy
trait for whatever tuple is made up of types that all implement the Happy
trait. Intuitively, a tuple of all happy is happy as well.
Is it possible to do such a thing? For example, I can trivially extend the implementation of Happy
to whatever tuple of two Happy
types:
impl <T, Q> Happy for (T, Q) where T: Happy, Q: Happy {}
As a result, this compiles perfectly:
fn f(_: impl Happy) {
}
fn main() {
f((Dog{}, Alligator{}));
}
But how could I generalize that to any tuple, of any length? As far as my understanding goes, we don't have variadic generics in Rust. Is there a workaround?
Correct.
You use a macro:
This now compiles:
This isn't generally seen as a big problem because long tuples are basically unreadable and you can always nest tuples if really needed.
See also: