簡體   English   中英

使用關聯類型時不滿足特征邊界

[英]Trait bound is not satisfied when using associated type

我正在嘗試設計一個道路跟蹤應用程序,並提出了以下特征結構(可在操場上獲得):

pub trait Source: Sized {
    type Path: Path<Source = Self>;
}

pub trait Destination<S: Source>{ }

pub trait Path {
    type Source: Source;
    type Destination: Destination<Self::Source>;
}

問題是它無法編譯錯誤

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `<<Self as Source>::Path as Path>::Destination: Destination<Self>` is not satisfied
 --> src/lib.rs:2:16
  |
2 |     type Path: Path<Source = Self>;
  |                ^^^^^^^^^^^^^^^^^^^ the trait `Destination<Self>` is not implemented for `<<Self as Source>::Path as Path>::Destination`
...
7 | pub trait Path {
  |           ---- required by a bound in this
8 |     type Source: Source;
9 |     type Destination: Destination<Self::Source>;
  |                       ------------------------- required by this bound in `Path`
  |
help: consider further restricting the associated type
  |
1 | pub trait Source: Sized where <<Self as Source>::Path as Path>::Destination: Destination<Self> {
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

這還不清楚。 我將關聯類型指定為type Path: Path<Source = Self>; 這意味着對錯誤消息中指定的關聯類型的限制。

但這應該禁止使用錯誤的類型實現,而不是按原樣聲明它。 有沒有辦法解決這個問題?

我找到了一種通過指定更多關聯類型來修復它的方法,但原始錯誤的原因尚不清楚。 這是工作示例:

pub trait Source: Sized {
    type Path: Path<Source=Self, Destination=Self::Destination>;
    type Destination: Destination<Self>; // Added associated type
}

pub trait Destination<S: Source>{ }

pub trait Path {
    type Source: Source;
    type Destination: Destination<Self::Source>;
}

暫無
暫無

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

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