I had a go at implementing variadic tuples using structs Cons
and Nil
implementing a Tuple
trait, and it seemed to work for some basic cases. However, when trying to write a zip method, I ran into an overflow (playground). Why does Zip
cause an overflow when LastTwo
does not?
2 Likes
- Don't put trait bounds on types, doubly so if you are doing type level computations
- Make your traits more focused, i.e. split off as_ref and as_mut_ref out of Tuple into their own seperate trait.
- Complex trait bounds often don't work because they make the compiler go into an infinite loop, and that's detected and halts the compiler. It's hard to tell what you are are doing, so I can't really say why your specific code doesn't work
-
Zip
should probably be defined
struct Cons<V, Rest>(V, Rest);
struct Nil;
trait Zip<T> {
type Output;
fn zip(self, other: T) -> Self::Output;
}
impl Zip<Nil> for Nil {
type Output = Nil;
fn zip(self, other: Nil) -> Self::Output {
Nil
}
}
impl<T, TR, U, UR> Zip<Cons<U, UR>> for Cons< T, TR>
where
TR: Zip<UR>,
{
type Output = Cons<(T, U), TR::Output>;
fn zip(self, other: Cons<U, UR>) -> Self::Output {
Cons((self.0, other.0), self.1.zip(other.1))
}
}
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.