I want to create a trait that returns a different implementation of the trait in functions something like this:
trait Interface {
type Item;
fn transform<E, U:Interface<Item=E>>(&self,e: E) -> U;
}
struct Foo<T> {
t:T
}
impl <T: Send> Interface for Foo<T> {
type Item = T;
fn transform<E, U:Interface<Item=E>>(&self,e: E) -> U {
Foo{ t: e}
}
}
As far as I can tell it should be possible, because when implementing the interface the types are known, but unfortunately I am getting
error[E0308]: mismatched types
--> src/mod.rs:42:9
|
41 | fn transform<E, U:Interface<Item=E>>(&self,e: E) -> U {
| - expected `U` because of return type
42 | Foo{ t: e}
| ^^^^^^^^^^ expected type parameter, found struct `Foo`
|
= note: expected type `U`
found type `Foo<E>`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error: aborting due to previous error
Which makes no sense to me because U
is Foo<E>
which satisfies its conditions.
Am I doing wrong and is this possible at all in Rust?