如何解决使用关联类型时不满足特质界限
我正在尝试设计一个道路跟踪应用程序,并提出了以下特征结构(可在playground上使用):
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>;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。