简体   繁体   English

使用关联类型时不满足特征边界

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

I'm trying to design a road-tracking application and came up with the following traits structure (available on 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>;
}

The thing is it fails to compile with the error问题是它无法编译错误

   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

This is not clear.这还不清楚。 I specified the associated type bound as type Path: Path<Source = Self>;我将关联类型指定为type Path: Path<Source = Self>; which imply a bound on the associated type specified in the error message.这意味着对错误消息中指定的关联类型的限制。

But that should prohibit from implementing with wrong types, not declare it the way it is.但这应该禁止使用错误的类型实现,而不是按原样声明它。 Is there a way to fix that?有没有办法解决这个问题?

I found a way to fix it by specifying one more associated type, but the reason for the original error is unclear.我找到了一种通过指定更多关联类型来修复它的方法,但原始错误的原因尚不清楚。 Here is the working example:这是工作示例:

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.

相关问题 使用TcpConnectionNew时,不满足特征绑定`():futures :: Future` - The trait bound `(): futures::Future` is not satisfied when using TcpConnectionNew 要求在继承特征的关联类型上绑定特征 - Requiring a trait bound on the associated type of an inherited trait 当使用参数的生命周期作为特征参数时,“预期的关联类型,找到`u32`” - “Expected associated type, found `u32`” when using the lifetime of a parameter as trait parameter in where bound Rust中不满足特征限制 - The trait bound is not satisfied in Rust 特质界限不满足 - The trait bound is not satisfied 为什么在使用 .await 或在像 `join!` 这样的宏中会出现“不满足 trait bound Future”? - Why do I get "the trait bound Future is not satisfied" when using .await or in a macro like `join!`? 使用Rust DBUS库时,不满足特性绑定`dbus :: arg :: Get` - The trait bound `dbus::arg::Get` is not satisfied when using the Rust DBUS library 即使类型已派生(序列化),也不满足特征绑定序列化 - Trait bound Serialize is not satisfied even though the type has derive(Serialize) 为什么我的Result类型别名不满足failure :: Fail特质的约束? - Why is the failure::Fail trait bound not satisfied by my Result type alias? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM