Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incompatible Defaults for functions vs Struct Deserializers #372

Closed
apgoetz opened this issue Feb 22, 2021 · 1 comment
Closed

incompatible Defaults for functions vs Struct Deserializers #372

apgoetz opened this issue Feb 22, 2021 · 1 comment

Comments

@apgoetz
Copy link
Contributor

apgoetz commented Feb 22, 2021

the serialize and deserialize functions use different defaults than the DefaultOptions Struct. Specifically, the functions use a fixedint format, but DefaultOptions uses varint.

Confusingly, both the function documentation and the Struct documentation indicate that they use the default options.

I ran into this issue, because I am writing some code that needs to use serde::de::DeserializeSeed, which requires access to the Deserializer as a struct. However, the data may not be created with the struct, as most bincode usage seems to use the functions.

Steps to reproduce:

extern crate bincode;
extern crate serde;

#[test]
fn test_mix_fn_and_struct() {
    // example data
    let orig = ("abc", 1, 2, 3);

    // serialize using fn 
    let encoded: Vec<u8> = bincode::serialize(&orig).unwrap();

    // deserialize using struct
    let opts = bincode::DefaultOptions::new();
    let mut deserializer = bincode::Deserializer::from_slice(&encoded, opts);
    let output = <_ as serde::Deserialize>::deserialize(&mut deserializer).unwrap();
    // fails
    assert_eq!(orig, output);
    // ---- test_mix_fn_and_struct stdout ----
    // thread 'test_mix_fn_and_struct' panicked at 'assertion failed: `(left == right)`
    // left: `("abc", 1, 2, 3)`,
    // right: `("\u{0}\u{0}\u{0}", 0, 0, 0)`', tests/default.rs:13:5
}
@ZoeyR
Copy link
Collaborator

ZoeyR commented Feb 22, 2021

Yeah, I messed up the documentation on this one. The reason serialize and deserialize use fixedint is to maintain backwards compatibility with older versions of bincode. Closing this as a duplicate of #348

@ZoeyR ZoeyR closed this as completed Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants