我想创建一个抽象特征,该特征指定一个索引类型和一个值类型,其中实现该特征的任何结构都必须实现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
。
答案 0 :(得分:2)
您需要在超级特征上放置相关类型的限制:
trait LightMap: Index<Coord, Output = LightValue> + IndexMut<Coord> {}
另请参阅: