Roxy Light
Roxy Light

Reputation: 5147

How to disambiguate associated types in trait object bounds?

If I try to define a boxed IEvent field like this:

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>)?

Upvotes: 5

Views: 373

Answers (1)

DK.
DK.

Reputation: 59155

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. 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. Changing ReferenceType to accept a generic type parameter that is used to directly constrain the Error types also doesn't satisfy it.

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.

Upvotes: 4

Related Questions