Is bincode "compatible" with appending enum variants?

I understand bincode does not offer any compatibility in general.
However, say I serialized the Message:

#[derive(Serialize, Deserialize, std::cmp::PartialEq, Debug)]
enum Message {
    Request {
        id: String,
        method: String,
        params: String,
    },
}

And latter I added a new variant (append to the end),

#[derive(Serialize, Deserialize, std::cmp::PartialEq, Debug)]
enum Message {
    Request {
        id: String,
        method: String,
        params: String,
    },
    Response {
        id: String,
        result: String,
    },
}

Can I de-serialize the old serialized message correctly?
My test says yes, but is it guarrenteed that

  • there is no edge case it does not work, and
  • this assumption will not break in future releases?

Thanks!

I would definitely not rely on bincode supporting that. It was designed for IPC where processes of the same binary talk to each other, not for persisting data. As for you code block question, I see that you're using four spaces, but usually I recommend using three backticks as that enables syntax highlighting:

```
// your code
```

If you have too few or much indentation, a quick way to fix that is to paste it to the playground and use tab or shift-tab to correct the indentation.

1 Like

I see. Thanks! The code block tip helps as well.

I think bincode serializing enum is implemented with a enum tag + serialized variant. So I assume it would at least work for the current release?
Background: I wanted something simple for serializing Rust data structures, did not want to use the complex libraries such as protobuf which involves compiling files written in another language (the *.proto). Also wants compact-ness, JSON (or its variant which need to encode field names as string) result in too big serialized message.

I mean, it probably does work assuming the number of variants doesn't change enough to change the size of the tag.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.