I have some traits which (after removing functions and some parameter bloat) look like:
trait Foo { }
trait Boo { }
trait Bar<T: Foo> { }
trait Baz { }
If U
implements Bar<T>
for some T
implementing Foo
and U
implements Boo
, then one is able to derive an implementation of Baz
for U
. However, I wasn't able to write valid Rust code doing this.
A few tries were:
impl<T: Foo, U: Bar<T> + Boo> Baz for U { }
which gives
error: the type parameter
T
is not constrained by the impl trait, self type, or predicates [E0207]
whereas
impl<U: Bar<T> + Boo> Baz for U { }
yields
error: type name
T
is undefined or not in scope [E0412]
Could one/how could one do this in (stable) Rust (hopefully without any dynamic dispatch)?
Edit: Some people hinted at some similar questions for which there were essentially two approaches (and I find both of them unsuitable for my situation):
- Using associated types. I don't want to do this because I want to keep track of
T
, e.g. I want to write some functions which have a signature likefn bla<T: Foo, U: Bar<T>, V: Bar<T>>()
where I want to know thatU
andV
implementBar<T>
for the sameT
. (Or is there way of doing this with associated types?) - Using some kind of wrapping by putting
U
andT
in a struct. I don't want to use this either because I have several levels of such "trait dependencies", so wrapping things in each level would bloat the code a lot.
So the updated question would be: Is there a solution to this problem without using associated types or wrappers?