dtolnay / erased-serde

Type-erased Serialize, Serializer and Deserializer traits
Apache License 2.0
709 stars 36 forks source link

Implementation of erased_serde::Serialize for custom types #17

Closed VladYermakov closed 6 years ago

VladYermakov commented 6 years ago

How to implement erased_serde::Serialize manually for custom type? The types Ok and Error are private and this prevents to implement erased_serde::Serialize for any type or trait outside the library. But if we can't implement serde::Serialize we can't have erased_serde::Serialize for this type. E.g. for some trait TheTrait we need type-erased serialize because if we implement serde::Serialize we'll have The trait TheTrait cannot be made into an object, but we can't implement erased_serde::Serialize manually because Ok and Error of Result are private. Please provide any solution or make Ok and Error types public.

dtolnay commented 6 years ago

Correct, you are not allowed to implement erased_serde::Serialize. There is an impl<T: ?Sized> erased_serde::Serialize for T where T: serde::Serialize so you need to provide a serde::Serialize impl and receive an automatic erased_serde::Serialize impl. The serialize_trait_object! macro implements serde::Serialize for any trait object.