Skip to content

fix: make Unpacker.readArray usable - #25

Merged
lalinsky merged 2 commits into
mainfrom
fix/unpacker-read-array
Sep 4, 2026
Merged

fix: make Unpacker.readArray usable#25
lalinsky merged 2 commits into
mainfrom
fix/unpacker-read-array

Conversation

@lalinsky

@lalinsky lalinsky commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Fixes #21.

Unpacker.readArray passes its type parameter straight through to unpackArray, but the two disagree about what that parameter means:

pub fn readArray(self: Unpacker, comptime T: type) ![]T {
    return unpackArray(self.reader, self.allocator, T);
}

unpackArray expects the slice type — it derives the element type itself via std.meta.Child and returns T. readArray declares ![]T, i.e. it treats T as the element type. Both readings fail, so the method cannot be instantiated with any argument:

  • readArray(u32)"Expected pointer, optional, array or vector type, found 'u32'"
  • readArray([]u32)"pointer type child 'u32' cannot cast into pointer type child '[]u32'"

The fix passes the slice type down, keeping the element-type convention that Packer.writeArray and Unpacker.readArrayInto already use.

This broke the README

The "Or you can use a completely custom format" example calls unpacker.readArray(u32), so it did not compile as written. It is now a test, verbatim.

Array(T)

The same mismatch hid two more errors in the Array(T) helper in src/array.zig, which nothing in the repo constructs: msgpackWrite called writeArray with one argument where it takes two, and msgpackRead passed the slice type where the element type belongs. Both fixed, with a round-trip test so the generic is actually instantiated. The neighbouring String and Binary helpers were already fine.

Why CI was green

test { _ = std.testing.refAllDecls(@This()); } does not instantiate generic functions, so nothing ever type-checked these. The three tests added here call the affected paths directly. Reverting the one-line fix makes them fail to compile, so they hold the line.

zig build test: 156/156 pass.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed serialization and deserialization for typed arrays, including Array wrappers and raw arrays.
    • Typed arrays now preserve their element types correctly when packed and unpacked.
    • Improved compatibility for custom message-pack formats that use array read/write operations.
  • Tests

    • Added coverage for typed array round trips and custom serialization formats.

readArray passed its type parameter straight to unpackArray, but the two
disagreed about what it means. unpackArray takes the slice type and derives
the element type itself; readArray declares `![]T`, treating T as the element
type. Both readings failed to compile, so the method could not be instantiated
with any argument, and the "completely custom format" example in README.md did
not build.

Pass the slice type down, keeping the element-type convention that
Packer.writeArray and Unpacker.readArrayInto already use.

The same mismatch hid two errors in the Array(T) helper, which nothing
constructs: msgpackWrite called writeArray with one argument instead of two,
and msgpackRead passed the slice type where the element type belongs.

refAllDecls does not instantiate generic functions, so none of this was
covered. Add tests that actually call readArray, round trip Array(T), and
build the README example verbatim.
@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 00bb0e04-a6be-4165-851b-7496148f44a4

📥 Commits

Reviewing files that changed from the base of the PR and between 7d61a07 and 0ce994f.

📒 Files selected for processing (1)
  • src/msgpack.zig

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 34c9640e-24ba-4ea3-9812-9a69961b6fdf

📥 Commits

Reviewing files that changed from the base of the PR and between 893211c and 7d61a07.

📒 Files selected for processing (2)
  • src/array.zig
  • src/msgpack.zig

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change fixes typed array decoding and updates Array(T) to use explicit element types for MessagePack array operations. Tests cover raw u32 arrays, the custom format example, and Array(u32) round-tripping.

Changes

Typed array support

Layer / File(s) Summary
Correct array read typing
src/msgpack.zig
Unpacker.readArray passes []T to unpackArray. Tests cover readArray(u32) and the custom format round-trip.
Array wrapper serialization
src/array.zig
Array(T).msgpackWrite and msgpackRead pass explicit element types. A round-trip test covers Array(u32).

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 7d61a

Typed MessagePack array reads and Array(T) serialization now use consistent type conventions, restoring the documented custom-format and wrapper round trips without an identified remaining merge risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary change: making Unpacker.readArray usable.
Linked Issues check ✅ Passed The changes satisfy issue #21. Unpacker.readArray now passes []T to unpackArray, the Array(T) helper errors are fixed, and tests cover readArray, Array(T) round trips, and the README custom-format exa…
Out of Scope Changes check ✅ Passed All changes support the linked issue objectives. The implementation fixes and related tests are within scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/unpacker-read-array

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.

@lalinsky
lalinsky merged commit e5cb7c0 into main Sep 4, 2026
3 checks passed
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.

Unpacker.readArray is uninstantiable; README custom-format example does not compile

1 participant