Rust 使用关联类型时不满足特征绑定

Rust 使用关联类型时不满足特征绑定,rust,traits,generic-programming,type-safety,Rust,Traits,Generic Programming,Type Safety,我正在尝试设计一个道路跟踪应用程序,并提出了以下特征结构(可在上获得): pub特征源:大小{ 输入路径:路径; } 发布特性目标{} pub特征路径{ 类型来源:来源; 类型目的地:目的地; } 问题是,它无法编译并出现错误 Compiling playground v0.0.1 (/playground) error[E0277]: the trait bound `<<Self as Source>::Path as Path>::Destination:

我正在尝试设计一个道路跟踪应用程序,并提出了以下特征结构(可在上获得):

pub特征源:大小{
输入路径:路径;
}
发布特性目标{}
pub特征路径{
类型来源:来源;
类型目的地:目的地;
}
问题是,它无法编译并出现错误

   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
编译操场v0.0.1(/playerd)
错误[E0277]:未满足特征绑定“::目标:目标”
-->src/lib.rs:2:16
|
2 |类型路径:路径;
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^未为`::Destination实现特征“destin`
...
7 |公共特征路径{
|----本节中的绑定要求
8 |类型源:源;
9 |类型目的地:目的地;
|----------------------------此绑定在'Path'中需要`
|
帮助:考虑进一步限制关联类型
|
1 |发布特征源:大小在哪里::目的地:目的地{
|                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
错误:由于上一个错误而中止
这不清楚。我将关联的类型绑定指定为
type Path:Path;
,这意味着在错误消息中指定的关联类型上有一个绑定


但是,这应该禁止使用错误的类型实现,而不是按原样声明。是否有办法解决此问题?

我找到了一种方法,通过指定多个关联类型来解决此问题,但原始错误的原因尚不清楚。以下是工作示例:

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>;
}
pub特征源:大小{
输入路径:路径;
类型目的地:目的地;//添加了关联的类型
}
发布特性目标{}
pub特征路径{
类型来源:来源;
类型目的地:目的地;
}
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>;
}