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

Deserialize error #130

Closed
u2 opened this issue Mar 7, 2017 · 9 comments
Closed

Deserialize error #130

u2 opened this issue Mar 7, 2017 · 9 comments

Comments

@u2
Copy link

@u2 u2 commented Mar 7, 2017

    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
    #[serde(untagged)]
    pub enum TestValue {
        String(String),
        Number(u64),
        Bool(bool),
    }

    #[test]
    fn test_value() {
        let serialized = serialize_little(&TestValue::String(String::from("124")), Infinite).unwrap();
        let _: TestValue = deserialize_little(&mut &serialized[..]).unwrap();
    }

Error message:

---- types::rpc_request::tests::test_value stdout ----
	thread 'types::rpc_request::tests::test_value' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("bincode does not support Deserializer::deserialize")', src/libcore/result.rs:860
note: Run with `RUST_BACKTRACE=1` for a backtrace.

It will pass if I remove #[serde(untagged)].

@dtolnay
Copy link
Collaborator

@dtolnay dtolnay commented Mar 7, 2017

Bincode does not support untagged enums. Staring at some bytes, there is no way to tell whether those bytes came from a string or number or boolean. If you need to use bincode then use tagged enums.

@u2
Copy link
Author

@u2 u2 commented Mar 7, 2017

Thank you.

@TyOverby TyOverby closed this Mar 7, 2017
@zbraniecki
Copy link

@zbraniecki zbraniecki commented Jan 10, 2020

Staring at some bytes, there is no way to tell whether those bytes came from a string or number or boolean.

Would it not be possible to devote one byte to select the right variant for an enum and then read it?

I know it wouldn't work for very long untagged enums, but for common cases like 2 variants, that should be enough, no?

@dtolnay
Copy link
Collaborator

@dtolnay dtolnay commented Jan 10, 2020

That's not an untagged enum anymore...

If you mean a byte for type information, not for the variant tag: that's not Bincode anymore...

@zbraniecki
Copy link

@zbraniecki zbraniecki commented Jan 10, 2020

No, I meant for the variant thing.

I have a use case where the same struct is deserialized from JSON (out of my control) and I want to be able to serialize/deserialize it using bincode for performance.

In result, since in JSON we have to deserialize based on "shape", I need to use untagged, but then I can serialize/deserialize bincode however I want, so adding a byte that says basically "variant 0" or "variant 1" and the proceeding bytes store the value, feel like it should be doable, but I'm not very familiar with intrinsics of bincode.

@boymaas
Copy link

@boymaas boymaas commented Apr 1, 2020

I am running in the same issue here, deserialization from JSON, and serialization into bincode.

@ZoeyR
Copy link
Collaborator

@ZoeyR ZoeyR commented Apr 1, 2020

@zbraniecki if you use a byte to store the variant though, then its not an untagged enum anymore. Serde just tells us to serialize a struct, we get no information that this is an enum at all, so we couldn't put a variant on it anyways.

@zbraniecki
Copy link

@zbraniecki zbraniecki commented Apr 1, 2020

I see. Maybe there's some information we could get out of serde? If you knew you're looking at an untagged enum, you could take the first byte as a variant selection?

@ZoeyR
Copy link
Collaborator

@ZoeyR ZoeyR commented Apr 1, 2020

I looked at serde and there doesn't seem to be a way to get extra info out of it. You should probably open an issue on serde.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.