I want to achieve the following:
assert_eq!((1, 2).flatten(), (1, 2));
assert_eq!((1, (2, 3)).flatten(), (1, 2, 3));
assert_eq!((1, (2, (3, 4))).flatten(), (1, 2, 3, 4));
// ...
Due to conflicting trait implementations issues (stackoverflow) I tried to use #![feature(specialization)]
and came up with this code (playground):
#![feature(specialization)]
trait Flatten {
type Output;
fn flatten(self) -> Self::Output;
}
impl<T, U> Flatten for (T, U) {
default type Output = (T, U);
default fn flatten(self) -> Self::Output {
self
}
}
impl<A, B, T, U> Flatten for (A, B) where B: Flatten<Output = (T, U)> {
type Output = (A, T, U);
fn flatten(self) -> Self::Output {
let (a, b) = self;
let (t, u) = b.flatten();
(a, t, u)
}
}
but then I get this error I'm not able to resolve:
expected associated type, found tuple
How could I make the tests run through?