-
-
Notifications
You must be signed in to change notification settings - Fork 272
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
#[serde(flatten)]
causes error SequenceMustHaveLength
#245
Comments
This is still happening |
I've confirmed this behavior, with one minor difference. It actually panics when serializing the data, not when deserializing. One additional note, running I'll review the code a bit and see if I can understand why this is happening. |
This is caused by the way serde implements flatten. When you flatten, serde uses the map type rather than the struct type. See this serde issue for details. This explains why your custom serialize impl works, because you still serialize the object as a struct. The remaining question is whether it would be possible for serde to provide length information to bincode when serializing this "map", because in that case I believe bincode would still work. Currently, |
Finally wrapping up this research. The reason serde cannot provide size information to bincode when flattening a struct is related to the implementation of the macro system in Rust. There is no way for the derive(Serialize) on Foo to know anything about Bar. This means the knowledge of how many fields are on Bar, which is needed to determine the overall size of Foo, is not available to the derive macro. |
This seems impossible to fix in serde itself. When I contributed the I'm not sure how to fix this but with bincode being used to transfer data for ipc it's not uncommon now to have objects that are internally using flatten. It would be nice to be able to support such objects in a less efficient way. Right now the workaround is to go through |
Bincode has one more issue that makes it difficult to fix this. On deserialize, structs annotated with |
I'm going to track this as part of #167 |
This works around a bug encountered with the combination of serde and bincode when there is a flatten attribute, see bincode-org/bincode#245
This works around a bug encountered with the combination of serde and bincode when there is a flatten attribute, see bincode-org/bincode#245
This works around a bug encountered with the combination of serde and bincode when there is a flatten attribute, see bincode-org/bincode#245
bincode can't handle that. See bincode-org/bincode#245 (comment)
This example has a runtime error upon deserializing:
Cargo.toml
dependencies:Flatten seems to be incompatible with
bincode
. Is this a known issue? If I were only deserializing frombincode
, then I wouldn't use flatten at all, but I also need to deserialize from JSON thus the requirement.My current workaround is to manually implement
Serialize
andDeserialize
like so:The text was updated successfully, but these errors were encountered: