I'm trying to implement a trait for a type implementing a generic trait with a bound on the inner type. The compiler shows me an error. Are there any known workarounds for this?
trait MyTrait1 {}
trait MyTrait2 {}
trait MyTrait3<T> {}
impl <T: MyTrait1, U: MyTrait3<T>> MyTrait2 for Vec<U> {}
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:5:7
|
5 | impl <T: MyTrait1, U: MyTrait3<T>> MyTrait2 for Vec<U> {}
| ^ unconstrained type parameter
For more information about this error, try `rustc --explain E0207`.
error: could not compile `playground` due to previous error
Not quite sure what you are trying to do but as @Sven Marnach said you can use associated types. That way it will not be unconstrained because only one type can be associated with the trait implementation.
trait MyTrait1 {}
trait MyTrait2 {}
trait MyTrait3 {
type AssociatedType;
}
impl<T, U> MyTrait2 for Vec<U>
where
U: MyTrait3<AssociatedType = T>,
T: MyTrait1,
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.