rust - Implementing Nested Traits -
i have traits (after removing functions , parameter bloat) like:
trait foo { } trait boo { } trait bar<t: foo> { } trait baz { } if u implements bar<t> t implementing foo and u implements boo, 1 able derive implementation of baz u. however, wasn't able write valid rust code doing this.
a few tries were:
impl<t: foo, u: bar<t> + boo> baz u { } which gives
error: type parameter
tnot constrained impl trait, self type, or predicates [e0207]
whereas
impl<u: bar<t> + boo> baz u { } yields
error: type name
tundefined or not in scope [e0412]
could one/how 1 in (stable) rust (hopefully without dynamic dispatch)?
edit: people hinted @ similar questions there 2 approaches (and find both of them unsuitable situation):
- using associated types. don't want because want keep track of
t, e.g. want write functions have signaturefn bla<t: foo, u: bar<t>, v: bar<t>>()want knowu,vimplementbar<t>samet. (or there way of doing associated types?) - using kind of wrapping putting
u,tin struct. don't want use either because have several levels of such "trait dependencies", wrapping things in each level bloat code lot.
so updated question be: is there solution problem without using associated types or wrappers?
you can making t associated type:
trait foo { } trait boo { } trait bar { type t: foo; } trait baz { } impl<u: bar + boo> baz u // clause not necessary (this bound true) // u::t: foo { } i don't want because want keep track of
t, e.g. want write functions have signaturefn bla<t: foo, u: bar<t>, v: bar<t>>()want knowu,vimplementbar<t>samet. (or there way of doing associated types?)
yes, can associated types:
fn bla<u: bar, v: bar<t = u::t>>() { }
Comments
Post a Comment