繁体   English   中英

将通用参数与impl中的关联类型匹配

[英]Matching a generic parameter to an associated type in an 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
}

我想generate并将其放入我的向量中:

impl<A: Generator, B> Baz<A, B> {
    fn addFoo(&mut self) {
        self.vec.push(self.generator.generate());
    }
}

嗯,哦! 编译错误:

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`

很公平,我必须向编译器解释BA::Foo相同; 让我们尝试在where

impl<A: Generator, B> Baz<A, B>
where
    A::Foo = B,
{

这没有帮助:

error: equality constraints are not yet supported in where clauses (#20041)
  --> src/main.rs:16:5
   |
16 |     A::Foo = B,
   |     ^^^^^^^^^^

嗯,不等于。 也许我可以用冒号操作符代替这个?

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`

不,现在它抱怨A 也许我应该说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

好的工作,编译器 - 它不是特性; 它是一个关联类型,但这并没有告诉我如何编写匹配它的where子句。

我必须向编译器解释BA::Foo相同

它有一个特殊的语法:

impl<A, B> Baz<A, B>
where
    A: Generator<Foo = B>,
{
    fn add_foo(&mut self) {
        self.vec.push(self.generator.generate());
    }
}

诀窍是只有一个通用参数:

trait Generator {
    type Foo;
    fn generate(&self) -> Self::Foo;
}

struct Baz<G>
where
    G: Generator,
{
    generator: G,
    vec: Vec<G::Foo>,
}

impl<G> Baz<G>
where
    G: Generator,
{
    fn add_foo(&mut self) {
        self.vec.push(self.generator.generate());
    }
}

由于向量将包含G::Foo ,我们实际上可以这么说。

Rust风格是snake_case ,所以我更新了它,并制作了类型参数G来帮助读者。

您可以摆脱泛型参数B而不是约束B ,直接将A::Foo作为第二个泛型参数传递给Baz ,但我不确定您的实际问题是否与您展示的简化示例匹配。

impl<A: Generator> Baz<A, A::Foo> {
    fn addFoo(&mut self)  {
        self.vec.push(self.generator.generate());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM