简体   繁体   English

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

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

If I try to define a boxed IEvent field like this: 如果我尝试定义一个盒装的IEvent字段,如下所示:

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

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

I get an error like this: 我收到这样的错误:

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"

How do I write the syntax to set the associated Error types (for traits TryFrom<Value> and TryFrom<Reference> )? 如何编写语法来设置相关的Error类型(对于特征TryFrom<Value>TryFrom<Reference> )?

I don't believe you can. 我不相信你能。

Examining what I believe to be the relevant type in the compiler ( TypeBinding in libsyntax ) shows that it only supports a single identifier for the associated type. 检查我认为是编译器中的相关类型( TypeBinding中的libsyntax )表明它只支持关联类型的单个标识符。 So I don't think there's any way to specify the associated types from the field type. 所以我认为没有办法从字段类型中指定相关类型。

Defining your own intermediate trait doesn't help, since it uses the same syntax to constrain associated types. 定义自己的中间特征没有帮助,因为它使用相同的语法来约束关联的类型。 Even modifying the traits in stdweb doesn't appear to work, as trying to constrain the TryFrom::Error types to an associated type in, say, ReferenceType produces a cyclic dependency that is rejected by the compiler. 甚至修改stdweb中的特征似乎也不起作用,因为尝试将TryFrom::Error类型约束到相关类型,例如, ReferenceType会产生一个被编译器拒绝的循环依赖。 Changing ReferenceType to accept a generic type parameter that is used to directly constrain the Error types also doesn't satisfy it. 更改ReferenceType以接受用于直接约束Error类型的泛型类型参数也不满足它。

It's possible this is just an edge case that the language simply can't deal with as yet. 这可能只是语言根本无法处理的边缘情况。 If someone else doesn't come up with a solution, I would recommend opening an issue in the compiler's issue tracker with a complete motivating example. 如果其他人没有提出解决方案,我建议在编译器的问题跟踪器中打开一个问题,并提供一个完整的激励示例。

The only other solution I can think of is to modify stdweb to not use multiple TryFrom constraints. 我能想到的唯一其他解决方案是修改stdweb以不使用多个TryFrom约束。

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

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