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>
)?
Traits can't have fields. If you want to provide access to a field from a trait, you need to define a method in that trait (like, say, get_blah ).
A trait in Rust is a group of methods that are defined for a particular type. Traits are an abstract definition of shared behavior amongst different types. So, in a way, traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is able to access other methods within that trait.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With