簡體   English   中英

如何創建一個指定超特征關聯類型的特征?

[英]How to create a trait that specifies the associated type of a supertrait?

我想創建一個抽象特征,它指定一個索引類型和一個值類型,其中實現該特征的任何結構都必須實現Index<IndexType>IndexMut<IndexType>並定義Output類型,該類型在實現特征。

我嘗試創建一個特征,但似乎我無法指定 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;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

如果我沒有指定 output 類型,那么在我嘗試使用該特征的任何地方associated type Output must be specified

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM