繁体   English   中英

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

[英]How to disambiguate associated types in trait object bounds?

如果我尝试定义一个盒装的IEvent字段,如下所示:

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

struct Foo {
    bar: Box<IEvent<Error = ConversionError>>,
}

我收到这样的错误:

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"

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

我不相信你能。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM