Skip to content

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Dec 30, 2025

Emit implementations of HasField, Immutable, TryFromBytes,
FromZeros, and FromBytes for tuples of length up to 26.

Closes #274


Latest Update: v3 — Compare vs v2

📚 Full Patch History

Links show the diff between the row version and the column version.

Version Base v1 v2
v3 vs Base vs v1 vs v2
v2 vs Base vs v1
v1 vs Base

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jswrenn, 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 significantly extends the utility of the zerocopy library by enabling automatic trait implementations for a wide range of tuple types. By generating these implementations for tuples up to 26 elements, it simplifies working with zerocopy traits for composite data structures. Additionally, it refactors the internal logic for TryFromBytes derivation, improving code clarity and maintainability within the library itself.

Highlights

  • Tuple Trait Implementations: Automatically generates implementations for HasField, Immutable, TryFromBytes, FromZeros, and FromBytes traits for Rust tuples. These implementations cover tuples with up to 26 elements.
  • Refactored TryFromBytes Derivation: Introduced a new helper function, is_field_valid, in src/util/macro_util.rs to centralize the logic for checking the bit-validity of individual fields within a type. The zerocopy-derive macro for TryFromBytes now utilizes this helper, leading to more concise and maintainable generated code.
  • Improved Code Generation: The changes result in cleaner and more readable generated code for TryFromBytes implementations, as demonstrated by the updated output tests.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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
Contributor

@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 trait implementations for tuples up to size 26, which is a valuable addition. The implementation uses a complex but well-structured macro. The refactoring of is_bit_valid logic into a helper function is_field_valid is a good improvement for maintainability. I've found one critical issue in the derive macro that will cause compilation failures for users and needs to be addressed.

@jswrenn jswrenn force-pushed the G992a50c1ccf344487e1cd764afe0b17e566bc620 branch from 489515e to 96eb35c Compare December 30, 2025 17:29
@codecov-commenter
Copy link

codecov-commenter commented Dec 30, 2025

Codecov Report

❌ Patch coverage is 47.36842% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.80%. Comparing base (b99b86d) to head (96cb8ae).

Files with missing lines Patch % Lines
src/impls.rs 0.00% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2862      +/-   ##
==========================================
- Coverage   91.94%   91.80%   -0.15%     
==========================================
  Files          20       20              
  Lines        5824     5843      +19     
==========================================
+ Hits         5355     5364       +9     
- Misses        469      479      +10     

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

Emit implementations of `HasField`, `Immutable`, `TryFromBytes`,
`FromZeros`, and `FromBytes` for tuples of length up to 26.

Closes #274

gherrit-pr-id: G992a50c1ccf344487e1cd764afe0b17e566bc620
@jswrenn jswrenn force-pushed the G992a50c1ccf344487e1cd764afe0b17e566bc620 branch from 96eb35c to 96cb8ae Compare December 30, 2025 17:55
@jswrenn jswrenn enabled auto-merge January 2, 2026 15:36
@jswrenn jswrenn requested a review from joshlf January 2, 2026 15:36
// `STRUCT_VARIANT_ID`.
// - `$CurrI` is the field at index `$CurrI`, so `FIELD_ID` is
// `zerocopy::ident_id!($CurrI)`
// - `()` has the same visibility as `$CurrI`
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
// - `()` has the same visibility as `$CurrI`
// - `()` has the same visibility as the `.$CurrI` field (ie, `.0`, `.1`, etc)
// since tuple fields are globally public, and so is the `()` type

Copy link
Member

Choose a reason for hiding this comment

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

I know it's technically fine to just say 0, 1, etc in the same way that it'd be fine for named fields to refer to them without the leading dot (foo, bar, etc), but at least to me, this makes it clearer that we're talking about the visibility of tuple fields, which is not something most people are used to doing.

Comment on lines +1103 to +1128
let slf = slf.as_non_null().as_ptr();
// SAFETY: `PtrInner` promises it references either a zero-sized
// byte range, or else will reference a byte range that is
// entirely contained within an allocated object. In either
// case, this guarantees that `(*slf).$CurrI` is in-bounds of
// `slf`.
let field = unsafe { core::ptr::addr_of_mut!((*slf).$CurrI) };

// SAFETY: `PtrInner` promises it references either a zero-sized
// byte range, or else will reference a byte range that is
// entirely contained within an allocated object. In either
// case, this guarantees that field projection will not wrap
// around the address space, and so `field` will be non-null.
let ptr = unsafe { core::ptr::NonNull::new_unchecked(field) };

// SAFETY:
// 0. `ptr` addresses a subset of the bytes of
// `slf`, so by invariant on `slf: PtrInner`,
// if `ptr`'s referent is not zero sized,
// then `ptr` has valid provenance for its
// referent, which is entirely contained in
// some Rust allocation, `A`.
// 1. By invariant on `slf: PtrInner`, if
// `ptr`'s referent is not zero sized, `A` is
// guaranteed to live for at least `'a`.
unsafe { crate::PtrInner::new(ptr) }
Copy link
Member

Choose a reason for hiding this comment

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

TODO(me): I haven't reviewed this yet because I'm assuming we'll rebase on #2860, which will allow us to significantly simplify this body.

<#field_tys as #zerocopy_crate::TryFromBytes>::is_bit_valid(field_candidate)
})*
true
#(&& #zerocopy_crate::util::macro_util::is_field_valid::<_, _, _, { #zerocopy_crate::ident_id!(#field_names) }>(candidate.reborrow()))*
Copy link
Member

Choose a reason for hiding this comment

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

🥳

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.

Implement traits for tuples

3 participants