Skip to content

Conversation

@ElouanPetereau
Copy link

This pull request add a feature named serde to enable serializing and deserializing the byteorder numeric wrappers using serde derives.

Why

The reason for adding this is that I need to be able to convert a struct to either:

  • a raw binary blob in the most performance efficient way (using zerocopy on my struct)
  • a serialized format like CSV or json which doesn't need to be as fast (using serde on my struct)

Instead of having to manually write the serde:Serialize and serde:Deserialize traits every time for my structs and calling the get()/from() function to get the inner type, e.g. f64 for an F64, it would have been easier to use the Serialize and Deserialize derives provided by the serde crate.

How

This simply update the define_type!() macro to add the Serialize and Deserialize trait implementations using the get()/from() function to get the inner type when the serde feature is enabled.


Thank you for your work in maintaining this project.

@google-cla
Copy link

google-cla bot commented Nov 24, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link

Summary of Changes

Hello @ElouanPetereau, 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 enhances the crate by integrating an optional serde feature, allowing for seamless serialization and deserialization of byteorder numeric wrapper types. This addition provides greater flexibility for users who need to handle both high-performance binary data (via zerocopy) and structured data formats like JSON or CSV, streamlining the process of converting between these representations without manual trait implementations.

Highlights

  • New serde Optional Feature: An optional serde feature has been introduced to the crate, enabling serialization and deserialization capabilities for byteorder numeric wrapper types.
  • Automatic Serialize and Deserialize Implementations: The define_type! macro now conditionally implements serde::Serialize and serde::Deserialize for numeric wrappers, delegating to their underlying primitive types when the serde feature is active.
  • Dependency Updates: The Cargo.toml file has been updated to include serde as an optional dependency and serde_json as a development dependency for testing purposes.
  • Comprehensive Test Coverage: New unit tests have been added to verify the correct serialization and deserialization behavior of various primitive types and structs containing byteorder wrappers with the serde feature enabled.
  • Documentation Updates: Both the README.md and src/lib.rs documentation have been updated to describe the new serde feature and its functionality.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a serde feature to allow serialization and deserialization of the byteorder numeric wrappers, which is a great addition for interoperability. The implementation is clean, adding the Serialize and Deserialize impls within the define_type! macro behind the new feature flag. The accompanying tests are thorough and cover round-trip serialization for all relevant types, including within a struct.

I have a couple of suggestions regarding dependency versions in Cargo.toml to use more recent, but still MSRV-compatible, versions of serde and serde_json.

@codecov-commenter
Copy link

codecov-commenter commented Nov 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.26%. Comparing base (5226dee) to head (519fc0c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2815      +/-   ##
==========================================
+ Coverage   88.14%   88.26%   +0.11%     
==========================================
  Files          20       20              
  Lines        5450     5505      +55     
==========================================
+ Hits         4804     4859      +55     
  Misses        646      646              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ElouanPetereau ElouanPetereau force-pushed the feature/serde_support branch 2 times, most recently from e15adae to 1982814 Compare November 24, 2025 17:03
Copy link
Member

@joshlf joshlf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, thanks for this! I'm sure others will find this useful too.

I'm broadly on board with the implementation – just a few small changes requested.

src/lib.rs Outdated
//! zerocopy_derive::FromBytes`).
//!
//! - **`serde`**
//! Provides `Serialize` and `Deserialize` impls for the `byteorder` numeric
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Provides `Serialize` and `Deserialize` impls for the `byteorder` numeric
//! Provides [`serde`] [`Serialize`] and [`Deserialize`] impls for the [`byteorder`] numeric

(or something along these lines – just want to mention serde, and want to make things into doc links)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot use the doc links directly as the feature is optional and when it is not used serde is not imported. I guarded this doc part with a config attribute and updated slightly the doc when not using the serde feature to specify that the implemented traits come from serde, wdyt?

Additionally, I fixed the links for the simd feature just below, or rather added the links that where missing:

-//!   When the `simd` feature is enabled, `FromZeros`, `FromBytes`, and
-//!   `IntoBytes` impls are emitted for all stable SIMD types which exist on the
+//!   When the `simd` feature is enabled, [`FromZeros`], [`FromBytes`], and
+//!   [`IntoBytes`] impls are emitted for all stable SIMD types which exist on the

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems using the config attribute breaks the readme option of cargo and I did not realize that the README.md and the doc comment need to be a 1:1 match every time so I cannot use a conditional doc anyway.

I switched to add a link to the serde crate repository but I had to update the generate-readme crate because it was removing the square brackets on proper links with urls ([]()) where it should only do it for Rust doc links ([]).

Wdyt?

@joshlf joshlf enabled auto-merge November 24, 2025 17:43
@joshlf joshlf disabled auto-merge November 24, 2025 17:43
@ElouanPetereau ElouanPetereau force-pushed the feature/serde_support branch 7 times, most recently from ddfcc9a to 2db4c01 Compare November 25, 2025 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants