Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Discriminant size in fixint serialization of Option<T> #700

@mkeeter

Description

@mkeeter

I've run into an interesting corner case in the bincode specification:

  • Option<T> is implemented as an enum
  • The specification says that in fixint encoding, enum discriminants are always encoded as a u32 (source)
  • However, a simple test program shows that an Option<T> is serialized with a 1-byte tag (instead of a u32)
fn main() {
    let i: Option<u32> = Some(100);
    let out = bincode::serialize(&i).unwrap();
    println!("{out:?}");

    let i: Option<u32> = None;
    let out = bincode::serialize(&i).unwrap();
    println!("{out:?}");

    let i: Result<u32, u32> = Ok(101);
    let out = bincode::serialize(&i).unwrap();
    println!("{out:?}");

    let i: Result<u32, u32> = Err(102);
    let out = bincode::serialize(&i).unwrap();
    println!("{out:?}");
}
$ cargo run -q
[1, 100, 0, 0, 0]
[0]
[0, 0, 0, 0, 101, 0, 0, 0]
[1, 0, 0, 0, 102, 0, 0, 0]

Note that the Result<T, E> uses a 4-byte tag, as we'd expect from the specification.

I don't think the behavior should change at this point, but it would be good to reflect this corner case in the specification. If that makes sense, I'd be happy to submit a PR to that effect.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions