Skip to content

Conversation

@ananas-block
Copy link
Contributor

@ananas-block ananas-block commented Dec 19, 2025

Summary by CodeRabbit

  • New Features
    • Added support for unit structs (empty structs) to the zero-copy derive macro, enabling zero-copy operations on unit struct types.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 19, 2025

Walkthrough

Extended the zero-copy-derive macro to support unit structs (e.g., struct Foo;) by introducing an InputType::UnitStruct variant and updating code generation paths in four files to handle zero-field structs via type aliases and specialized trait implementations.

Changes

Cohort / File(s) Summary
Input type classification
program-libs/zero-copy-derive/src/shared/utils.rs
Added UnitStruct variant to public InputType<'a> enum; updated process_input() and process_input_generic() signatures to return Option<&FieldsNamed> and InputType<'_> respectively, enabling detection and classification of unit structs vs. named-field structs
Zero-copy reference implementation
program-libs/zero-copy-derive/src/zero_copy.rs
Added code path for unit structs generating: public type alias pub type <ZName<'a>> = &'a <Name>;, and trait impl ZeroCopyAt<'a> that returns a reference to a static unit instance
Equality trait derivation
program-libs/zero-copy-derive/src/zero_copy_eq.rs
Added early-return branch for unit structs (zero fields) that generates PartialEq impls always returning true, bypassing standard field-processing logic
Mutable reference implementation
program-libs/zero-copy-derive/src/zero_copy_mut.rs
Added code path for unit structs generating: mutable type alias {ZStructName}Mut<'a>, config type alias {Name}Config = (), and trait impls ZeroCopyAtMut<'a> and ZeroCopyNew<'a> with zero byte length and leaked Box instances

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring attention:

  • The process_input() and process_input_generic() signature changes propagate to callers across the codebase; verify all call sites correctly handle the new Option and InputType return types
  • Unit-struct handling in zero_copy_eq.rs returns hardcoded true comparisons—confirm this is the intended semantics and doesn't mask actual inequality cases
  • Mutable reference generation via Box::leak() in zero_copy_mut.rs—verify memory safety guarantees and lifetime management for leaked allocations

Possibly related PRs

  • feat: zero copy macro #1851 — Parallel PR extending zero-copy-derive macros with UnitStruct support across the same derive paths and function signatures.
  • feat: zero copy derive enum #1909 — Related input classification extension adding Enum support alongside this PR's UnitStruct, both modifying InputType enum and process functions in shared/utils.rs.

Suggested labels

ai-review

Suggested reviewers

  • sergeytimoshin
  • SwenSchaeferjohann

Poem

🎯 A unit struct stands alone, so bare,
Now zero-copy gives it care,
With static refs and aliases lean,
The tiniest type's now part of the scene! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 70.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: adding unit struct support to the light zero-copy derive macros.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jorrit/feat-zero-copy-unit-struct

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 32f0acf and c810abc.

⛔ Files ignored due to path filters (3)
  • program-tests/zero-copy-derive-test/tests/instruction_data.rs is excluded by none and included by none
  • program-tests/zero-copy-derive-test/tests/ui/fail/01_empty_struct.stderr is excluded by none and included by none
  • program-tests/zero-copy-derive-test/tests/ui/pass/01_empty_struct.rs is excluded by none and included by none
📒 Files selected for processing (4)
  • program-libs/zero-copy-derive/src/shared/utils.rs (3 hunks)
  • program-libs/zero-copy-derive/src/zero_copy.rs (1 hunks)
  • program-libs/zero-copy-derive/src/zero_copy_eq.rs (1 hunks)
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
program-libs/zero-copy-derive/**/*.rs

📄 CodeRabbit inference engine (program-libs/zero-copy-derive/README.md)

program-libs/zero-copy-derive/**/*.rs: Apply #[derive(ZeroCopy)] macro to structs to derive ZeroCopyAt trait for zero-copy serialization
Apply #[derive(ZeroCopyMut)] macro to structs to derive ZeroCopyAtMut and ZeroCopyNew traits
Apply #[derive(ZeroCopyEq)] macro to structs to derive PartialEq comparing ZeroCopy representation with original struct
All structs and enums used with ZeroCopy macros must have #[repr(C)] attribute for memory layout safety
Use only supported primitive types: u8, u16, u32, u64, i8, i16, i32, i64, and bool in structs with ZeroCopy macros
Use Vec for collections in zero-copy structs, where T is a supported primitive type or implements ZeroCopyAt trait
Use Arrays [T; N] for fixed-size collections in zero-copy structs where T is a supported type
Use Option for optional values in zero-copy structs; Option, Option, and Option are optimized
Only structs with named fields are supported with ZeroCopy macros; tuple structs are not supported
Structs must have at least one field for zero-copy serialization; empty structs are not supported
ZeroCopyMut and ZeroCopyEq macros only support structs; enums are not supported with these macros
ZeroCopy macro supports enums with unit variants or single unnamed field variants only
Custom types in zero-copy structs must implement the ZeroCopyAt trait
Extract first consecutive fixed-size fields into a meta struct ZMeta; stop extraction at first Vec, Option, or non-Copy type
Primitive integer types are automatically converted to little-endian equivalents (u16→U16, u32→U32, u64→U64) in zero-copy structs
Boolean types are converted to u8 in zero-copy structs for alignment and serialization
Vec uses optimized slice operations in zero-copy structs; other Vec types use ZeroCopySlice
Option, Option, and Option are optimized in zero-copy structs; other Option types delegate to T's ZeroCopyAt implementation
Nested structs in zero-copy serialization must also derive...

Files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
program-libs/**/*.rs

📄 CodeRabbit inference engine (CLAUDE.md)

Unit tests in program-libs must not depend on light-test-utils; tests requiring light-test-utils must be located in program-tests/

Files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
🧠 Learnings (19)
📓 Common learnings
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/batched-merkle-tree/docs/CLAUDE.md:0-0
Timestamp: 2025-11-24T17:56:00.229Z
Learning: Applies to program-libs/batched-merkle-tree/docs/**/Cargo.toml : Depend on light-zero-copy crate for zero-copy serialization for efficient account data access
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopy)] macro to structs to derive ZeroCopyAt trait for zero-copy serialization
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Vec<T> for collections in zero-copy structs, where T is a supported primitive type or implements ZeroCopyAt trait
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyEq)] macro to structs to derive PartialEq comparing ZeroCopy representation with original struct
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Nested structs in zero-copy serialization must also derive #[derive(ZeroCopy)]
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Structs must have at least one field for zero-copy serialization; empty structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Option<T> for optional values in zero-copy structs; Option<u16>, Option<u32>, and Option<u64> are optimized
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyMut)] macro to structs to derive ZeroCopyAtMut and ZeroCopyNew traits
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Primitive integer types are automatically converted to little-endian equivalents (u16→U16, u32→U32, u64→U64) in zero-copy structs
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Option<u64>, Option<u32>, and Option<u16> are optimized in zero-copy structs; other Option<T> types delegate to T's ZeroCopyAt implementation
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopy macro supports enums with unit variants or single unnamed field variants only
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Only structs with named fields are supported with ZeroCopy macros; tuple structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Extract first consecutive fixed-size fields into a meta struct Z<StructName>Meta; stop extraction at first Vec, Option, or non-Copy type
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use only supported primitive types: u8, u16, u32, u64, i8, i16, i32, i64, and bool in structs with ZeroCopy macros
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyEq)] macro to structs to derive PartialEq comparing ZeroCopy representation with original struct

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Extract first consecutive fixed-size fields into a meta struct Z<StructName>Meta; stop extraction at first Vec, Option, or non-Copy type

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Nested structs in zero-copy serialization must also derive #[derive(ZeroCopy)]

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyMut)] macro to structs to derive ZeroCopyAtMut and ZeroCopyNew traits

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopy)] macro to structs to derive ZeroCopyAt trait for zero-copy serialization

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopyMut and ZeroCopyEq macros only support structs; enums are not supported with these macros

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Only structs with named fields are supported with ZeroCopy macros; tuple structs are not supported

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Option<T> for optional values in zero-copy structs; Option<u16>, Option<u32>, and Option<u64> are optimized

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Structs must have at least one field for zero-copy serialization; empty structs are not supported

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Boolean types are converted to u8 in zero-copy structs for alignment and serialization

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : All structs and enums used with ZeroCopy macros must have #[repr(C)] attribute for memory layout safety

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Custom types in zero-copy structs must implement the ZeroCopyAt trait

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_eq.rs
  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopy macro supports enums with unit variants or single unnamed field variants only

Applied to files:

  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Option<u64>, Option<u32>, and Option<u16> are optimized in zero-copy structs; other Option<T> types delegate to T's ZeroCopyAt implementation

Applied to files:

  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Vec<T> for collections in zero-copy structs, where T is a supported primitive type or implements ZeroCopyAt trait

Applied to files:

  • program-libs/zero-copy-derive/src/shared/utils.rs
  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
  • program-libs/zero-copy-derive/src/zero_copy.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use only supported primitive types: u8, u16, u32, u64, i8, i16, i32, i64, and bool in structs with ZeroCopy macros

Applied to files:

  • program-libs/zero-copy-derive/src/shared/utils.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Arrays [T; N] for fixed-size collections in zero-copy structs where T is a supported type

Applied to files:

  • program-libs/zero-copy-derive/src/shared/utils.rs
📚 Learning: 2025-11-24T17:54:20.982Z
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Vec<u8> uses optimized slice operations in zero-copy structs; other Vec<T> types use ZeroCopySlice

Applied to files:

  • program-libs/zero-copy-derive/src/zero_copy_mut.rs
🧬 Code graph analysis (2)
program-libs/zero-copy-derive/src/zero_copy_eq.rs (1)
program-libs/zero-copy-derive/src/shared/utils.rs (1)
  • process_input (31-66)
program-libs/zero-copy-derive/src/zero_copy_mut.rs (3)
program-libs/zero-copy-derive/src/zero_copy.rs (3)
  • generate_zero_copy_struct_inner (218-238)
  • generate_zero_copy_struct_inner (285-285)
  • generate_zero_copy_struct_inner (308-308)
program-libs/zero-copy-derive/src/shared/z_struct.rs (1)
  • name (28-44)
program-libs/zero-copy/src/slice_mut.rs (1)
  • zero_copy_at_mut (286-290)
⏰ Context from checks skipped due to timeout of 900000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: cli-v2
  • GitHub Check: lint
  • GitHub Check: programs (system-cpi-test-v2-functional-read-only, ["cargo-test-sbf -p system-cpi-v2-test -- func...
  • GitHub Check: programs (system-cpi-test-v2-functional-account-infos, ["cargo-test-sbf -p system-cpi-v2-test -- ...
  • GitHub Check: programs (compressed-token-and-e2e, ["cargo test -p light-compressed-token", "cargo-test-sbf -p c...
  • GitHub Check: programs (compressed-token-batched-tree, ["cargo-test-sbf -p compressed-token-test -- test_transf...
  • GitHub Check: programs (light-system-program-address, ["cargo-test-sbf -p system-test -- test_with_address", "c...
  • GitHub Check: programs (system-cpi-test, ["cargo-test-sbf -p system-cpi-test", "cargo test -p light-system-prog...
  • GitHub Check: programs (account-compression-and-registry, ["cargo-test-sbf -p account-compression-test", "cargo...
  • GitHub Check: programs (light-system-program-compression, ["cargo-test-sbf -p system-test -- test_with_compress...
  • GitHub Check: Test program-libs-fast
  • GitHub Check: Test program-libs-slow
  • GitHub Check: Test batched-merkle-tree-simulate
  • GitHub Check: cli-v1
  • GitHub Check: system-programs (anchor & pinocchio, ["cargo-test-sbf -p sdk-anchor-test", "cargo-test-sbf -p sdk...
  • GitHub Check: system-programs (sdk-libs, light-sdk-macros light-sdk light-program-test light-client light-ctoke...
  • GitHub Check: stateless-js-v1
  • GitHub Check: system-programs (native, ["cargo-test-sbf -p sdk-native-test", "cargo-test-sbf -p sdk-v1-native-t...
  • GitHub Check: system-programs (token test, ["cargo-test-sbf -p sdk-token-test"])
  • GitHub Check: stateless-js-v2
🔇 Additional comments (8)
program-libs/zero-copy-derive/src/zero_copy.rs (1)

303-333: Clean implementation for unit struct zero-copy support.

The approach here is elegant:

  1. Type alias pub type ZFoo<'a> = &'a Foo; avoids creating a separate wrapper struct
  2. Static instance pattern for ZSTs is correct—unit structs are zero-sized, so a single static instance is appropriate
  3. Returning (∓UNIT, bytes) correctly indicates no bytes are consumed

The generate_zero_copy_struct_inner::<false> call on line 307-308 will generate type ZeroCopyInner = ZFoo<'static> which expands to &'static Foo. This is semantically sound for unit structs since all instances are equivalent.

program-libs/zero-copy-derive/src/zero_copy_mut.rs (3)

44-52: Good use of Box::leak for mutable ZST references.

The Box::leak(Box::new(#name)) pattern is the right choice here. Unlike the immutable ZeroCopyAt implementation that uses a static, mutable references cannot safely alias a single static location. For ZSTs, Box::new returns a dangling-but-aligned pointer without heap allocation, so leaking it is both safe and leak-free.

The inline comments explaining this are helpful for future maintainers.


62-83: ZeroCopyNew implementation for unit structs is correct.

The byte_len returning Ok(0) and the () config type are appropriate—unit structs have no configuration needs and consume no bytes. The implementation correctly mirrors the ZeroCopyAtMut pattern.


85-94: Consistent hygiene isolation pattern.

Good to see the unit struct path maintains the same const _: () = { ... } isolation pattern used for non-unit structs (lines 169-190). This keeps the macro output clean and prevents namespace pollution.

program-libs/zero-copy-derive/src/shared/utils.rs (3)

24-28: Clean extension of InputType for unit struct support.

Adding UnitStruct as a distinct variant is the right design choice. It allows callers to explicitly handle the zero-field case rather than checking for empty fields, making the API more expressive.


83-90: Consistent handling in process_input_generic.

The Fields::Unit => InputType::UnitStruct mapping correctly parallels the process_input changes. Error message update is consistent.


46-53: API signature update for unit struct support is well-designed.

Changing from &FieldsNamed to Option<&FieldsNamed> cleanly represents the distinction:

  • Some(fields) → struct with named fields
  • None → unit struct

The error message correctly reflects the expanded support. However, the README.md (line 48) still states "Empty structs: Not supported - structs must have at least one field for zero-copy serialization." Update this constraint to reflect that unit structs are now supported.

⛔ Skipped due to learnings
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Structs must have at least one field for zero-copy serialization; empty structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Only structs with named fields are supported with ZeroCopy macros; tuple structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopy macro supports enums with unit variants or single unnamed field variants only
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Option<T> for optional values in zero-copy structs; Option<u16>, Option<u32>, and Option<u64> are optimized
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Extract first consecutive fixed-size fields into a meta struct Z<StructName>Meta; stop extraction at first Vec, Option, or non-Copy type
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Nested structs in zero-copy serialization must also derive #[derive(ZeroCopy)]
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use only supported primitive types: u8, u16, u32, u64, i8, i16, i32, i64, and bool in structs with ZeroCopy macros
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Arrays [T; N] for fixed-size collections in zero-copy structs where T is a supported type
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopyMut and ZeroCopyEq macros only support structs; enums are not supported with these macros
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Custom types in zero-copy structs must implement the ZeroCopyAt trait
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopy)] macro to structs to derive ZeroCopyAt trait for zero-copy serialization
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Vec<T> for collections in zero-copy structs, where T is a supported primitive type or implements ZeroCopyAt trait
program-libs/zero-copy-derive/src/zero_copy_eq.rs (1)

279-299: Unit struct handling looks correct.

The early return pattern for unit structs is clean and the logic is sound—unit structs with no fields should always compare as equal. The let Some(fields) = fields else { ... } pattern is idiomatic.

One minor style observation: you're using ::core::cmp::PartialEq in the unit struct path (lines 285, 291), but the existing non-unit struct code (lines 236, 245, 253, 260) uses bare PartialEq. Both work since PartialEq is in the prelude, but the fully-qualified form is more defensive for macro hygiene. Consider aligning the style across both paths for consistency.

⛔ Skipped due to learnings
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyEq)] macro to structs to derive PartialEq comparing ZeroCopy representation with original struct
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Nested structs in zero-copy serialization must also derive #[derive(ZeroCopy)]
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopyMut and ZeroCopyEq macros only support structs; enums are not supported with these macros
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopy)] macro to structs to derive ZeroCopyAt trait for zero-copy serialization
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Apply #[derive(ZeroCopyMut)] macro to structs to derive ZeroCopyAtMut and ZeroCopyNew traits
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Boolean types are converted to u8 in zero-copy structs for alignment and serialization
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Custom types in zero-copy structs must implement the ZeroCopyAt trait
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Option<T> for optional values in zero-copy structs; Option<u16>, Option<u32>, and Option<u64> are optimized
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Extract first consecutive fixed-size fields into a meta struct Z<StructName>Meta; stop extraction at first Vec, Option, or non-Copy type
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use Vec<T> for collections in zero-copy structs, where T is a supported primitive type or implements ZeroCopyAt trait
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Only structs with named fields are supported with ZeroCopy macros; tuple structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Structs must have at least one field for zero-copy serialization; empty structs are not supported
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : ZeroCopy macro supports enums with unit variants or single unnamed field variants only
Learnt from: CR
Repo: Lightprotocol/light-protocol PR: 0
File: program-libs/zero-copy-derive/README.md:0-0
Timestamp: 2025-11-24T17:54:20.982Z
Learning: Applies to program-libs/zero-copy-derive/**/*.rs : Use only supported primitive types: u8, u16, u32, u64, i8, i16, i32, i64, and bool in structs with ZeroCopy macros

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sergeytimoshin sergeytimoshin merged commit dd825e5 into main Dec 19, 2025
33 of 39 checks passed
@sergeytimoshin sergeytimoshin deleted the jorrit/feat-zero-copy-unit-struct branch December 19, 2025 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants