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

Serialize/deserialize tagged newtype variant containing unit #2302

Closed
tage64 opened this issue Oct 20, 2022 · 2 comments · Fixed by #2303
Closed

Serialize/deserialize tagged newtype variant containing unit #2302

tage64 opened this issue Oct 20, 2022 · 2 comments · Fixed by #2303

Comments

@tage64
Copy link
Contributor

tage64 commented Oct 20, 2022

To me, it seems that a tagged newtype variant containing () should be serializable/deserializable in the same way as unit variants. Specificly I have the following enum:

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "result", rename_all = "snake_case")]
enum Response<T> {
    Success(T),
    Error {
        ...
    },
}

I'd expect Success(()) to pe deserialized from and serialized to { "result": "success"}, but that doesn't seem to be the case. It doesn't work to deserialize it and when I try to serialize it (with serde_json) I get the following error:

Error("cannot serialize tagged newtype variant Response::Success containing unit", line: 0, column: 0)

Is there any reason why this doesn't work, or should we fix this? I'd would be happy to make a PR.

@dtolnay
Copy link
Member

dtolnay commented Oct 20, 2022

It would be fine to make this work I think.

@tage64
Copy link
Contributor Author

tage64 commented Oct 20, 2022

The PR is: #2303

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

Successfully merging a pull request may close this issue.

2 participants