codegen: propagate bytes_fields to map<K, bytes> values (#76)#147
Open
senthil1216 wants to merge 2 commits into
Open
codegen: propagate bytes_fields to map<K, bytes> values (#76)#147senthil1216 wants to merge 2 commits into
senthil1216 wants to merge 2 commits into
Conversation
Under `use_bytes_type()`, singular/optional/repeated/oneof bytes fields already become `bytes::Bytes`; `map<K, bytes>` values were the lone hold-out at `Vec<u8>`. Closes that asymmetry so map values participate in the `to_owned_from_source` zero-copy `slice_ref` path. One carve-out: `map<bytes, bytes>` (only reachable via `strict_utf8_mapping`) keeps `Vec<u8>` to preserve the existing JSON helper's concrete signature; documented on both knobs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
4 tasks
Addresses review feedback on the map<K, bytes> bytes_fields change: - Extract the "value uses Bytes" decision into a single `map_value_use_bytes` helper, replacing the four copy-pasted predicates in classify_field, the binary and text map_merge_arm decoders, and view::map_to_owned_expr. The helper takes Option<Type> key/value so it mirrors the original predicate and treats a missing entry field as non-bytes. A split decision would only surface as a compile error in the consuming crate, so a single source of truth matters. - Drop the redundant field_uses_bytes re-check on the view map path by emitting bytes_from_source directly; the carve-out already implies it. - Docs: spell out the read-side migration (Bytes has no inherent as_slice) and the precise carve-out trigger (strict_utf8_mapping plus a map key carrying features.utf8_validation = NONE; strict_utf8_mapping alone keeps a plain map<string, bytes> value as Bytes). Add intra-doc links from the no-arg use_bytes_type to use_bytes_type_in. - Tests: carve-out coverage (a NONE-keyed map<string, bytes> keeps Vec<u8> values and round-trips via bytes_key_bytes_val_map), an owned binary-decode assertion for a bytes map value, and a by_key probe in the arbitrary smoke test.
bc97dd1 to
2a25397
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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