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 and deserialize a tagged newtype variant over unit () as if it was a unit variant #2303
Conversation
… it was a unit variant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
serde/src/private/de.rs
Outdated
Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), | ||
Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see the Content::Map
case corresponds to the serialize_map
that has been added to serde/src/private/ser.rs in this PR. But what is Content::Seq
handling?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I first read your comment, I thought that The line with Content::Seq
should not be there and I had made a mistake. I read the code for unit structs at the function below and somehow just thought that I should copy both lines, with Content::Map
and Content::Seq
. However, that would mean that my example enum above could be deserialized from ["success"]
, which may not be what we want. And I made a commit to fix this.
But after a bit more investigation, I see that the behaviour of unit structs seem to be just that. I.E. if we have a unit struct SomeStruct
we could deserialize Response::Success(SomeStruct)
from ["success"]
. So for consistency this should perhaps also be allowed for units so that ()
and unit structs behave in the same way.
What do you think? Should we allow the sequence notation or not? If we decide to allow it, I will just revert my last commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should land it like this and find out whether anyone needs the Seq
case in practice.
Thanks!
Co-authored-by: David Tolnay <dtolnay@gmail.com>
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [serde](https://serde.rs) ([source](https://github.com/serde-rs/serde)) | dependencies | patch | `1.0.145` -> `1.0.147` | --- ### Release Notes <details> <summary>serde-rs/serde</summary> ### [`v1.0.147`](https://github.com/serde-rs/serde/releases/tag/v1.0.147) [Compare Source](serde-rs/serde@v1.0.146...v1.0.147) - Add `serde:🇩🇪 :value::EnumAccessDeserializer` which transforms an `EnumAccess` into a `Deserializer` ([#​2305](serde-rs/serde#2305)) ### [`v1.0.146`](https://github.com/serde-rs/serde/releases/tag/v1.0.146) [Compare Source](serde-rs/serde@v1.0.145...v1.0.146) - Allow internally tagged newtype variant to contain unit ([#​2303](serde-rs/serde#2303), thanks [@​tage64](https://github.com/tage64)) </details> --- ### Configuration📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox. --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4yNDEuNSIsInVwZGF0ZWRJblZlciI6IjMyLjI0MS41In0=--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Co-authored-by: crapStone <crapstone01@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1597 Reviewed-by: crapStone <crapstone@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Let's say we have the following enum:
This PR makes it possible to serialize and deserialize
Response::Success(())
into and from{"result": "success"}
. Previously, this was not possible when the type parameterT
was()
.Note that this is the same behaviour as if
Response::Success
would be a unit variant, but now it can be used in a more general context.See This part of the docs for a background to internally tagged enums.
It's my first time contributing to Serde, so please appologise any mistakes regarding the contribution process.
References #2302