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

Option<T> does not round-trip #5

Closed
apoelstra opened this issue Feb 17, 2016 · 4 comments
Closed

Option<T> does not round-trip #5

apoelstra opened this issue Feb 17, 2016 · 4 comments

Comments

@apoelstra
Copy link

extern crate serde_cbor;

fn main() {
    let obj1 = Some(10u32);

    let mut v = vec![];
    assert!(serde_cbor::ser::to_writer(&mut v, &obj1).is_ok());
    println!("{:?}", v);
    let obj2: Result<Option<u32>, _> = serde_cbor::de::from_reader(&v[..]);

    assert_eq!(obj1, obj2.unwrap());
}

This code fails with
thread '<main>' panicked at 'called Result::unwrap()on anErr value: SyntaxError(invalid syntax: "incorrect type", 0)', ../src/libcore/result.rs:738

I have code in production that fails with "trailing bytes" when I have a structure whose last element is an Option; I haven't produced a small test case because it's such a PITA to write Serialize/Deserialize impls for structures.

@pyfisch
Copy link
Owner

pyfisch commented Feb 21, 2016

This is a serde problem. The deserialization to Option is not implemented for integer values. There is already an issue for this serde-rs/serde#217.

@pyfisch
Copy link
Owner

pyfisch commented Mar 15, 2016

The mentioned is was a bit different. So this is not fixed.

@erickt Is it possible to make options round trip in languages like JSON and CBOR?

@erickt
Copy link

erickt commented Mar 16, 2016

@pyfisch: Yes, you should be able to. Here's how serde_json does it by overriding Deserializer::deserialize_option.

pyfisch added a commit that referenced this issue Mar 30, 2016
@pyfisch
Copy link
Owner

pyfisch commented Mar 30, 2016

Sorry that it took so long. It was a bit more complicated than expected since I had to add one byte lookahead to the parser to correctly parse options.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants