Skip to content

feat(codec): add Zeroed type - #4484

Open
ygd58 wants to merge 3 commits into
commonwarexyz:mainfrom
ygd58:feat/codec-zeroed-type
Open

feat(codec): add Zeroed type#4484
ygd58 wants to merge 3 commits into
commonwarexyz:mainfrom
ygd58:feat/codec-zeroed-type

Conversation

@ygd58

@ygd58 ygd58 commented Aug 14, 2026

Copy link
Copy Markdown

What

Adds Zeroed, a wrapper representing exactly n zero bytes — closes #1703.

Useful for padding or reserving space in an encoding, and doubles as an assertion that a reserved region was actually left untouched: decoding rejects any input where one of the n bytes is non-zero.

n is not stored on the wire; the reader supplies it externally via Read::Cfg = usize, the same way a fixed-length array's length is part of its type rather than its encoding.

Implementation

Implements Write/EncodeSize/Read following the same shape as Bytes (codec/src/types/bytes.rs), minus the length prefix (n comes from Cfg here rather than being self-describing). write() uses BufMut::put_bytes rather than allocating a temporary zero buffer.

Testing

Unit tests for round-tripping (n = 0, 1, 8, 300), rejecting a non-zero byte, rejecting a short buffer, and is_empty().

Added the arbitrary-gated conformance test block following the Bytes pattern, and generated the baseline:

just test -p commonware-codec zeroed
# 46 passed, 0 failed

RUSTFLAGS="--cfg generate_conformance_tests" just regenerate-conformance -p commonware-codec
# adds the CodecConformance<Zeroed> entry to codec/conformance.toml

Both commits included — the type itself, and the generated conformance baseline as a separate commit so the hash's provenance is clear.

Closes #1703

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4b3319a. Configure here.

Comment thread codec/src/types/zeroed.rs
Comment thread codec/src/types/zeroed.rs
Comment thread codec/src/types/zeroed.rs
@ygd58

ygd58 commented Aug 14, 2026

Copy link
Copy Markdown
Author

Addressed all 3 Bugbot findings in the follow-up commit:

  1. Decode reallocates for zero checkread_cfg now uses util::ensure_zeros instead of copying into a Vec and scanning it.
  2. Pooled encode double-allocates zeros — overrode encode_inline_size to return 0, since write_bufs pushes the zero region as a separate chunk rather than writing it inline.
  3. Missing write_bufs equivalence test — added test_zeroed_write_bufs_matches_write using the existing TrackingWriteBuf test double, asserting write and write_bufs produce identical bytes.

write() itself is unchanged, so the conformance baseline from the prior commit still applies.

Verified on a real toolchain (rustc >= 1.95, which I don't have access to directly):

just test -p commonware-codec zeroed
# 6 tests run: 6 passed, 128 skipped

ygd58 added 3 commits August 23, 2026 14:35
Adds Zeroed, a wrapper representing exactly n zero bytes. Useful for
padding or reserving space in an encoding, and doubles as an assertion
that a reserved region was actually left untouched: decoding rejects
any input where one of the n bytes is non-zero.

n is not stored on the wire; the reader supplies it externally via
Read::Cfg = usize, the same way a fixed-length array's length is part
of its type rather than its encoding.

Implements Write/EncodeSize/Read following the same shape as Bytes
(codec/src/types/bytes.rs), minus the length prefix (n comes from Cfg
here rather than being self-describing). write() uses BufMut::put_bytes
rather than allocating a temporary zero buffer.

Adds unit tests for round-tripping, rejecting a non-zero byte,
rejecting a short buffer, and is_empty(). Also adds the arbitrary-gated
conformance test block following the same pattern as Bytes's, but I
could not generate the actual conformance.toml baseline entry myself —
that requires running `just regenerate-conformance -p commonware-codec`,
which needs a real build (this workspace requires rustc >= 1.95, which
I don't have in my environment). Whoever picks this up for review will
need to run that before the conformance suite passes.

Closes commonwarexyz#1703
Generated via: RUSTFLAGS="--cfg generate_conformance_tests" just regenerate-conformance -p commonware-codec
Verified: just test -p commonware-codec zeroed -> 46 passed, 0 failed
- read_cfg: use the existing util::ensure_zeros helper (chunk-at-a-time,
  no allocation) instead of copying into a Vec and scanning it —
  duplicated logic Bugbot flagged as already covered by that helper.
- EncodeSize: override encode_inline_size to return 0. write_bufs pushes
  the zero region as a separate Bytes chunk via BufsMut::push rather than
  writing it inline, so the inline buffer shouldn't also reserve n bytes
  for it — previously it did, plus the separate n-byte push allocation,
  double-reserving memory on the pooled encode path.
- Add test_zeroed_write_bufs_matches_write, asserting write_bufs output
  is byte-identical to write via the existing TrackingWriteBuf test
  double (types/mod.rs), following the project's learned rule that
  write_bufs overrides need a wire-format equivalence test.

Addresses all 3 findings from Cursor Bugbot's review of 4b3319a.

write() itself is unchanged, so the conformance.toml baseline from the
prior commit still applies — no regeneration needed.
@ygd58
ygd58 force-pushed the feat/codec-zeroed-type branch from 26f2768 to a33c7d5 Compare August 23, 2026 14:35
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.

[codec] Add Zeroed type

1 participant