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 :
- I remove
#[serde(skip)]attribute - 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?
It looks like at the moment, skipping fields with
serdedoesn't work well on formats that are not self-describing likebincode. There are several open issues about this:skip_serializing_ifis a footgun onserde's GitHub repository.skip_deserializingvariant attribute has confusing behavior onbincode's GitHub repository.