-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
UPDATED: Integer/boolean tags for internally/adjacently tagged enums #2525
base: master
Are you sure you want to change the base?
Conversation
Is there still any blocking point for this PR after update and pass CI? |
The new adjacently tagged enum code really put a wrench into this. It effectively now serializes tags into a unit enum like: enum EnumTag {
A,
B,
// ...
} This way some (de)serializers can use the variant index instead of the name for smaller sizes. However there is no (de)serialize method that takes a variant index and a generic data type. The closest is A new (de)serialize method could be created for this, something like A default impl would simply redirect to the newtype's serialize impl I think. |
@dtolnay is making a new base serialization method a good solution here? It could even expand enum tags to more than booleans and integers later on and add support for non-string tags for unit enums. |
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 would not accept a new Serializer method.
I have not gotten a chance to look over the code in the PR, but in response to the last 2 comments: I don't immediately see why recent adjacently tagged enum changes (referring to #2505/#2496) would be an obstacle. Enums that use all integer tags need to serialize the tag using the appropriately sized integer type's Serialize impl, and deserialize using that same integer type. Similarly enums that use all boolean tags need to serialize using serialize_bool and deserialize using deserialize_bool. Enums that use a mix of integer and boolean and string tags, if this is supported, need to deserialize using deserialize_any. It's not clear to me that the solution would have been any different prior to #2505.
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.
Thank you for getting this moving.
I have not looked at any deserialization code yet, but a few comments on the rest.
test_suite/tests/ui/rename/non_string_on_externally_tagged.stderr
Outdated
Show resolved
Hide resolved
I've now done most of the simple changes. The rest of the requested changes will be completed on a later date |
@Astavie, that's an amazing piece of work, well done! May we help you somehow? |
@xamgore |
Support for marking specific integer types has been added. Specifically: Rules for unmarked integers:
|
@jenr24-architect |
Is it broken? Cargo.toml [package]
name = "serde_tagged_enum"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { git = "https://github.com/Astavie/serde", branch = "integer-tags-for-enums", features = ["derive"] }
serde_json = "1" main.rs use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct AttachmentUploadEntry {
pub id: u32,
pub upload_filename: String,
pub upload_url: String,
}
fn main() {
let data: String = "{}".to_string();
let mut json: AttachmentUploadEntry = serde_json::from_str(&data).unwrap();
} Error:
|
@IriaSomobu tried this myself, it's because serde_json uses the regular version of serde and not my fork, leading to serde_json not thinking the struct has a Deserialize implementation. you can fix this using [package]
name = "serde_tagged_enum"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
[patch.crates-io]
serde = { git = "https://github.com/Astavie/serde.git", branch = "integer-tags-for-enums" } note, cargo might complain about not finding a version for serde but just unlocking the dependencies by removing cargo.lock fixed that for me |
I see. Sorry for bothering you :) |
please make it as possible, it very helpful since rust enum can contain different type. |
Still waiting on a new review by @dtolnay |
Any progress on this PR? |
@yuto0214w I think @dtolnay still needs to review this |
this is basically just #2056 but synced with the latest serde master
closes #745
for implementation details see #2056
Example