如何消除特征对象边界中关联类型的歧义?

Ros*_*ght 5 generics rust

如果我尝试定义IEvent像这样的盒装字段:

use stdweb::private::ConversionError;
use stdweb::web::event::IEvent;

struct Foo {
    bar: Box<IEvent<Error = ConversionError>>,
}
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误:

error[E0221]: ambiguous associated type `Error` in bounds of `stdweb::traits::IEvent`
  --> src/events.rs:16:21
   |
16 |     bar: Box<IEvent<Error = ConversionError>>,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^ ambiguous associated type `Error`
   |
note: associated type `stdweb::traits::IEvent` could derive from `stdweb::unstable::TryFrom<stdweb::Reference>`
  --> src/events.rs:16:21
   |
16 |     bar: Box<IEvent<Error = ConversionError>>,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^
note: associated type `stdweb::traits::IEvent` could derive from `stdweb::unstable::TryFrom<stdweb::Value>`
  --> src/events.rs:16:21
   |
16 |     bar: Box<IEvent<Error = ConversionError>>,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^

If you want more information on this error, try using "rustc --explain E0221"
Run Code Online (Sandbox Code Playgroud)

如何编写语法来设置相关Error类型(对于特征TryFrom<Value>TryFrom<Reference>)?

DK.*_*DK. 4

我不相信你可以。

检查我认为编译器中的相关类型(TypeBindinginlibsyntax)表明它只支持关联类型的单个标识符。所以我认为没有任何方法可以从字段类型指定关联类型。

定义自己的中间特征没有帮助,因为它使用相同的语法来约束关联的类型。即使修改 in 的特征stdweb似乎也不起作用,因为尝试将TryFrom::Error类型限制为关联类型 inReferenceType会产生被编译器拒绝的循环依赖关系。更改ReferenceType为接受用于直接约束Error类型的泛型类型参数也不能满足它。

这可能只是一种边缘情况,该语言目前还无法处理。如果其他人没有提出解决方案,我建议在编译器的问题跟踪器中打开一个问题,并提供一个完整的激励示例。

我能想到的唯一其他解决方案是修改stdweb为不使用多个TryFrom约束。