5

我想创建一个抽象特征,它指定一个索引类型和一个值类型,其中实现该特征的任何结构都必须实现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;
}
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;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

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

4