Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Macro to serialize and deserialize trait object using internal type tag #14

Closed
dtolnay opened this issue Jan 21, 2019 · 2 comments
Closed

Comments

@dtolnay
Copy link
Owner

dtolnay commented Jan 21, 2019

There is currently no simple way to make a boxed trait object deserializable via an internal tag that determines its concrete type. I would like to be able to express trait objects as:

#[serde_trait_object]
trait MyTrait {
    /* whatever trait methods */
}

struct A {
    a: u8,
}

#[serde_trait_object]
impl MyTrait for A {}

// possibly in a different crate
struct B {
    b: u8,
}

#[serde_trait_object]
impl MyTrait for B {}

The attribute macro would insert whatever supertraits and trait methods are necessary to make this work, probably using erased-serde for the object safety and inventory for a registry of all trait implementations compiled into the program.

The serialized form of these types should carry a type tag, similar to what you might see with polymorphic deserialization using Jackson in Java.

{
    "type": "A",
    "a": 0
}

{
    "type": "B",
    "b": 0
}

If more than one deserializable type in the program has the same name, the deserialization should error at runtime and recommend renaming one of the types or using a #[serde(rename = "...")] attribute to make its tag unique.

@dtolnay
Copy link
Owner Author

dtolnay commented Jan 23, 2019

I am going to get started on this one myself.

@dtolnay
Copy link
Owner Author

dtolnay commented Jan 23, 2019

Implemented in https://github.com/dtolnay/typetag!

@dtolnay dtolnay closed this as completed Jan 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant