如何创建一个指定超级特征关联类型的特征?

jm4*_*ier 6 rust

我想创建一个抽象特征,它指定索引类型和值类型,其中实现该特征的任何结构都必须实现Index<IndexType>IndexMut<IndexType>定义Output在实现该特征的每个结构中保持相同的类型。

我尝试创建一个特征,但似乎无法指定输出类型:

use std::ops::{Index, IndexMut};

struct Coord;
struct LightValue;

trait LightMap: Index<Coord> + IndexMut<Coord> {}

impl LightMap {
    type Output = LightValue;
}
Run Code Online (Sandbox Code Playgroud)
warning: trait objects without an explicit `dyn` are deprecated
 --> src/lib.rs:8:6
  |
8 | impl LightMap {
  |      ^^^^^^^^ help: use `dyn`: `dyn LightMap`
  |
  = note: `#[warn(bare_trait_objects)]` on by default

error[E0191]: the value of the associated type `Output` (from the trait `std::ops::Index`) must be specified
 --> src/lib.rs:8:6
  |
8 | impl LightMap {
  |      ^^^^^^^^ associated type `Output` must be specified

error[E0202]: associated types are not yet supported in inherent impls (see #8995)
 --> src/lib.rs:9:5
  |
9 |     type Output = LightValue;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

如果我不指定输出类型,那么associated type Output must be specified无论我尝试使用该特征,都会发生这种情况。

归档时间:

查看次数:

951 次

最近记录:

6 年,4 月 前