dtolnay / typetag

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

Cannot distinguish deserialisation failure due to unregistered type #79

Closed doug-q closed 2 months ago

doug-q commented 3 months ago

We are using typetag in our application to allow users to extend our system with their own types. Deseralisation of a typetaged trait can fail for two reasons:

In the first case, we are able to degrade gracefully and proceed, keeping the value of unknown type as opaque JSON. In the second case, we must hard fail.

It seems that with typetag as is we are not able to distinguish between these two cases. Would you be willing to accept a PR adding this feature?

One strawman idea would be to add a public(i.e. not-hidden) method to dyn <Trait>:

// returns Ok(None) when the deserialised value is well formed, but the tag is unregistered
fn typetag_deserialise<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Box<dyn Trait>>,D::Error>
dtolnay commented 2 months ago

I would prefer not to build this into this crate. But I would be interested in someone else making a more fully featured library for trait object serialization, which could support this case (and maybe a few of the other feature requests from this repo).