Skip to content

codegen: propagate bytes_fields to map<K, bytes> values (#76)#147

Merged
iainmcgin merged 1 commit into
anthropics:mainfrom
senthil1216:feat/bytes-fields-map-values
May 26, 2026
Merged

codegen: propagate bytes_fields to map<K, bytes> values (#76)#147
iainmcgin merged 1 commit into
anthropics:mainfrom
senthil1216:feat/bytes-fields-map-values

Conversation

@senthil1216
Copy link
Copy Markdown
Contributor

Summary

  • Under use_bytes_type() (or use_bytes_type_in(...)), map<K, bytes> values now become bytes::Bytes instead of Vec<u8> — the lone hold-out among bytes contexts. This lets map values participate in the to_owned_from_source zero-copy slice_ref path that singular / optional / repeated / oneof bytes_fields already use.
  • One intentional carve-out: map<bytes, bytes> (only reachable via strict_utf8_mapping(true)) keeps Vec<u8> values because the existing bytes_key_bytes_val_map JSON helper is concrete HashMap<Vec<u8>, Vec<u8>>. Documented on both knobs.
  • Generated Arbitrary impls for the affected map fields go through a new __private::arbitrary_bytes_map<K> shim (K: Arbitrary + Eq + Hash — every proto map-key type qualifies). No turbofish needed at the call site.
  • Breaking for code already using use_bytes_type() on a proto containing map<K, bytes>: rewrite value construction from Vec<u8> to bytes::Bytes (b"v".to_vec()bytes::Bytes::from_static(b"v")).

Carve-out logic lives in classify_field::map_value_use_bytes (the source of truth) and is mirrored in impl_message::map_merge_arm, impl_text::map_merge_arm, and view::map_to_owned_expr with parallel "see classify_field" comments.

Closes #76 (item a). Item c (checked_add in bytes_from_source) was already done in #84; item d (string_fields shared-buffer strings) remains for a future PR. Net diff ≈ 120 lines excluding tests.

Test plan

  • Updated test_bytes_type_map_value_stays_vectest_bytes_type_map_value_uses_bytes (positive type assertion plus wire / view→owned / JSON / text agreement).
  • New test_bytes_type_map_value_to_owned_from_source_zero_copy asserts slice_ref aliasing into the source Bytes buf for map values — same property the singular/repeated/oneof tests pin.
  • Updated test_bytes_type_json_all_contexts_roundtrip + test_bytes_type_view_encode_roundtrip to construct map values as Bytes.
  • cargo test --workspace — 28 test suites pass, no failures.
  • cargo clippy --workspace --all-targets -- -D warnings — clean.
  • cargo fmt --all --check — clean.
  • cargo check -p buffa --no-default-features (host no_std) — clean.
  • task gen-wkt-types and gen-bootstrap-types produce no diff (neither set has map<K, bytes> fields).
  • Pre-commit rust-code-reviewer + rust-api-ergonomics-reviewer agents per CLAUDE.md — no Critical findings; High (CHANGELOG entry) and Medium (docstring updates) addressed in this PR.

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 23, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@senthil1216
Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request May 26, 2026
@iainmcgin iainmcgin enabled auto-merge (squash) May 26, 2026 19:12
… (anthropics#147)

## Summary

- Under `use_bytes_type()` (or `use_bytes_type_in(...)`), `map<K, bytes>` values now become `bytes::Bytes` instead of `Vec<u8>` — the lone hold-out among bytes contexts. This lets map values participate in the `to_owned_from_source` zero-copy `slice_ref` path that singular / optional / repeated / oneof bytes_fields already use.
- One intentional carve-out: `map<bytes, bytes>` (only reachable via `strict_utf8_mapping(true)`) keeps `Vec<u8>` values because the existing `bytes_key_bytes_val_map` JSON helper is concrete `HashMap<Vec<u8>, Vec<u8>>`. Documented on both knobs.
- Generated `Arbitrary` impls for the affected map fields go through a new `__private::arbitrary_bytes_map<K>` shim (`K: Arbitrary + Eq + Hash` — every proto map-key type qualifies). No turbofish needed at the call site.
- **Breaking** for code already using `use_bytes_type()` on a proto containing `map<K, bytes>`: rewrite value construction from `Vec<u8>` to `bytes::Bytes` (`b"v".to_vec()` → `bytes::Bytes::from_static(b"v")`).

Carve-out logic lives in `classify_field::map_value_use_bytes` (the source of truth) and is mirrored in `impl_message::map_merge_arm`, `impl_text::map_merge_arm`, and `view::map_to_owned_expr` with parallel "see classify_field" comments.

Closes anthropics#76 (item a). Item c (`checked_add` in `bytes_from_source`) was already done in anthropics#84; item d (`string_fields` shared-buffer strings) remains for a future PR. Net diff ≈ 120 lines excluding tests.

## Test plan

- [x] Updated `test_bytes_type_map_value_stays_vec` → `test_bytes_type_map_value_uses_bytes` (positive type assertion plus wire / view→owned / JSON / text agreement).
- [x] New `test_bytes_type_map_value_to_owned_from_source_zero_copy` asserts `slice_ref` aliasing into the source `Bytes` buf for map values — same property the singular/repeated/oneof tests pin.
- [x] Updated `test_bytes_type_json_all_contexts_roundtrip` + `test_bytes_type_view_encode_roundtrip` to construct map values as `Bytes`.
- [x] `cargo test --workspace` — 28 test suites pass, no failures.
- [x] `cargo clippy --workspace --all-targets -- -D warnings` — clean.
- [x] `cargo fmt --all --check` — clean.
- [x] `cargo check -p buffa --no-default-features` (host no_std) — clean.
- [x] `task gen-wkt-types` and `gen-bootstrap-types` produce no diff (neither set has `map<K, bytes>` fields).
- [x] Pre-commit `rust-code-reviewer` + `rust-api-ergonomics-reviewer` agents per CLAUDE.md — no Critical findings; High (CHANGELOG entry) and Medium (docstring updates) addressed in this PR.

Co-authored-by: senthil sivasubramanian <2329608+senthil1216@users.noreply.github.com>
@iainmcgin iainmcgin force-pushed the feat/bytes-fields-map-values branch from 40644ef to 5e33a10 Compare May 26, 2026 19:31
@iainmcgin iainmcgin merged commit 571b670 into anthropics:main May 26, 2026
7 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow-ups from #74: bytes_fields for map values, checked_add in bytes_from_source

2 participants