Ho un tratto con un tipo associato e una struttura generica ::corrispondenza di un parametro generico a un tipo associato in un impl
trait Generator {
type Foo;
fn generate(&self) -> Self::Foo;
}
struct Baz<A, B>
where
A: Generator,
{
generator: A, // will be some struct implementing Generator, but the exact type will vary
vec: Vec<B>, // Each element will be A::Foo
}
voglio generate
e mettila nel mio vettore:
impl<A: Generator, B> Baz<A, B> {
fn addFoo(&mut self) {
self.vec.push(self.generator.generate());
}
}
Uh-oh! Errore di compilazione:
error[E0308]: mismatched types
--> src/main.rs:16:27
|
16 | self.vec.push(self.generator.generate());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter, found associated type
|
= note: expected type `B`
found type `<A as Generator>::Foo`
Va bene, devo spiegare al compilatore che B
è lo stesso di A::Foo
; proviamo con where
:
impl<A: Generator, B> Baz<A, B>
where
A::Foo = B,
{
che non aiuta:
error: equality constraints are not yet supported in where clauses (#20041)
--> src/main.rs:16:5
|
16 | A::Foo = B,
| ^^^^^^^^^^
Hmm, eguali. Forse posso farlo con l'operatore del colon, invece?
impl<A: Generator, B> Baz<A, B>
where
B: A::Foo,
{
error[E0405]: cannot find trait `Foo` in `A`
--> src/main.rs:16:11
|
16 | B: A::Foo,
| ^^^ not found in `A`
No, ora è lamentarsi A
. Forse dovrei dire Generator
?
impl<A: Generator, B> Baz<A, B>
where
B: Generator::Foo,
{
error[E0404]: expected trait, found associated type `Generator::Foo`
--> src/main.rs:16:8
|
16 | B: Generator::Foo,
| ^^^^^^^^^^^^^^ not a trait
Bene buon lavoro, compilatore - è non un tratto; è un tipo associato, ma questo non mi dice come scrivere una clausola where che la abbini.
Sei sicuro è necessario sottotipo Generator? – llogiq