Fix "... must be defined only once in the type namespace of this module" and NonFixedWidthArray serialisation#11
Conversation
For types where a struct is repeated. Example:
let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);
Fails with:
| let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `StructureFsv3` redefined here
|
= note: `StructureFsv3` must be defined only once in the type namespace of this module
= note: this error originates in the macro `_define_gv` which comes from the expansion of the macro `gv` (in Nightly builds, run with -Z macro-backtrace for more info)
Reported by @alexlarsson in #10.
...We want to align the current element, not add padding after each element for the next one. The offsets that come after the data must not be padded. This lead to incorrect serialisation.
This test would fail if either of the previous commits are missing.
There was a problem hiding this comment.
Summary of Changes
Hello @wmanley, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses two distinct but related issues: a Rust compiler error that occurs when the gv! macro attempts to redefine types for repeated GVariant structures, and an issue with the serialization of NonFixedWidthArray types. The changes ensure that complex GVariant type definitions are correctly generated and serialized, improving the robustness of the gvariant and gvariant-macro crates.
Highlights
- Macro Type Generation Fix: Implemented a mechanism within the
gv!macro's type generation logic to track and prevent redundant type definitions. This resolves the Rust compiler error "must be defined only once in the type namespace of this module" when GVariant types are repeated within a complex structure. - NonFixedWidthArray Serialization Correction: Adjusted the serialization order for
NonFixedWidthArraytypes to ensure padding is applied correctly before the item's data is written, leading to accurate offset calculations and proper serialization. - New Test Case: Added a new test (
test_repeated_types) to validate the correct handling of repeated GVariant types during both serialization and deserialization, specifically for dictionary arrays.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request effectively addresses two separate issues. First, it resolves a compiler error about type redefinition when using GVariant types with repeated structures by tracking defined types within a macro expansion. Second, it corrects a critical serialization bug in NonFixedWidthArray where elements were not being properly aligned. The changes are logical, and the new test case provides good coverage for both fixes. Overall, this is a solid contribution that improves both the usability and correctness of the library.
| for x in self.into_iter() { | ||
| bytes_written += x.serialize(f)?; | ||
| offsets.push(bytes_written); | ||
| let padding = align_offset::<GvT::AlignOf>(bytes_written).to_usize() - bytes_written; | ||
| f.write_all(&b"\0\0\0\0\0\0\0"[..padding])?; | ||
| bytes_written += padding; | ||
| bytes_written += x.serialize(f)?; | ||
| offsets.push(bytes_written); | ||
| } |
There was a problem hiding this comment.
This is an excellent and critical fix for NonFixedWidthArray serialization.
The previous implementation had a logic error where padding was calculated and written after the element was serialized. This meant that the element itself was not guaranteed to be correctly aligned, which is a violation of the GVariant specification.
The new implementation correctly calculates and writes the necessary padding before serializing the element, ensuring that each element in the array is properly aligned. This resolves a significant correctness issue.
Well done for spotting and fixing this!
There was a problem hiding this comment.
Well gee wiz and wowzers! This is an excellent review. Thank you very much @gemini-code-assist. You upbeat regurgitation of my commit message has made my day.
Well done!
|
Yay! |
206e9f0 to
e0bb8d6
Compare
e0bb8d6 to
687dd2d
Compare
|
@alexlarsson: Please review and merge if you're happy. Once it's merged rebase #9 for the CI fix and I'll merge it. |
|
LGTM, and was able to (what it looks like) deserialize an ostree delta superblock correctly. |
For types where a struct is repeated. Example:
Fails with:
Reported by @alexlarsson in #10
Fixes: #10