dtolnay / typetag

Serde serializable and deserializable trait objects
Apache License 2.0
1.19k stars 38 forks source link

Question: How to use typetag with associated types #2

Closed ISibboI closed 5 years ago

ISibboI commented 5 years ago

I have a trait like so:

#[typetag::serde(tag = "type")]
pub trait Variable {
    type NativeType;
    fn native_value(&self) -> &Self::NativeType;
}

It does not compile as it asks me to specify the associtated type NativeType. What does this mean?

ISibboI commented 5 years ago

Digging further, this seems to be caused by not being able to specify dyn Variable without specifying NativeType.

dtolnay commented 5 years ago

Typetag makes it possible to serialize and deserialize dyn Trait for object-safe traits, but as you noticed, for your trait dyn Variable cannot exist because different impls of the trait would have different signatures for Variable::native_value.