Search code examples
rustserdebincode

Does serde skip attribute actually skip an enum variant?


I have an enum like this one:

#[derive(Debug, Deserialize, Serialize)]
enum E {
    A(i32),

    #[serde(skip)]
    B(bool),

    C(char),
    D(Vec<i32>),
}

Then I try to do the following with bincode crate:

fn main() {
    let data = E::C('A');
    let encoded = bincode::serialize(&data).unwrap();
    let decoded = bincode::deserialize::<E>(&encoded).unwrap();
    println!("{:?}", decoded);
}

However this panics with the following message:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Custom { kind: UnexpectedEof, error: "failed to fill whole buffer" })', src/main.rs:16:19

I noticed, that everything works if one of the following :

  1. I remove #[serde(skip)] attribute
  2. I remove tuples from variants

Also I understand that bincode somehow ignores #[serde(skip)] and tries to deserialize encoded as E::D(Vec<i32>). If I change Vec<i32> to char it will work, but decoded will be E::D('A') (instead of E::C('A')).

Do I miss something or is it a bug in bincode crate?


Solution

  • It looks like at the moment, skipping fields with serde doesn't work well on formats that are not self-describing like bincode. There are several open issues about this: