codegen: propagate bytes_fields to map<K, bytes> values (#76)#147
Merged
iainmcgin merged 1 commit intoMay 26, 2026
Merged
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
4 tasks
bc97dd1 to
2a25397
Compare
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
iainmcgin
approved these changes
May 26, 2026
… (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>
40644ef to
5e33a10
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
use_bytes_type()(oruse_bytes_type_in(...)),map<K, bytes>values now becomebytes::Bytesinstead ofVec<u8>— the lone hold-out among bytes contexts. This lets map values participate in theto_owned_from_sourcezero-copyslice_refpath that singular / optional / repeated / oneof bytes_fields already use.map<bytes, bytes>(only reachable viastrict_utf8_mapping(true)) keepsVec<u8>values because the existingbytes_key_bytes_val_mapJSON helper is concreteHashMap<Vec<u8>, Vec<u8>>. Documented on both knobs.Arbitraryimpls 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.use_bytes_type()on a proto containingmap<K, bytes>: rewrite value construction fromVec<u8>tobytes::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 inimpl_message::map_merge_arm,impl_text::map_merge_arm, andview::map_to_owned_exprwith parallel "see classify_field" comments.Closes #76 (item a). Item c (
checked_addinbytes_from_source) was already done in #84; item d (string_fieldsshared-buffer strings) remains for a future PR. Net diff ≈ 120 lines excluding tests.Test plan
test_bytes_type_map_value_stays_vec→test_bytes_type_map_value_uses_bytes(positive type assertion plus wire / view→owned / JSON / text agreement).test_bytes_type_map_value_to_owned_from_source_zero_copyassertsslice_refaliasing into the sourceBytesbuf for map values — same property the singular/repeated/oneof tests pin.test_bytes_type_json_all_contexts_roundtrip+test_bytes_type_view_encode_roundtripto construct map values asBytes.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-typesandgen-bootstrap-typesproduce no diff (neither set hasmap<K, bytes>fields).rust-code-reviewer+rust-api-ergonomics-revieweragents per CLAUDE.md — no Critical findings; High (CHANGELOG entry) and Medium (docstring updates) addressed in this PR.🤖 Generated with Claude Code