Repository navigation
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
Warn on ProtoString::copy_from_str and in the changelog fragment that a type relying on an efficient From<&str> pays a String allocation unless it overrides the method; add the value-equivalence contract bullet. Add a counting string fixture and tests that view-to-owned (each field shape) and JSON deserialization call copy_from_str. Cite anthropics#442/anthropics#441 in the fragment and fix two comments that described the removed bound.
|
[claude code] @rioyu123 the approach looks right for 0.10. Thank you for the design: a provided I pushed two commits to your branch:
|
|
Thanks for the additions, especially the I rechecked I've updated the description and marked this ready for review for 0.10. |
Summary
This implements the explicit copying operation discussed in #441 for 0.10. It lets a string type keep its borrowing
From<&str>conversion while providing owned storage to buffa.ProtoStringcurrently requiresFrom<&str>for every input lifetime. A string library whose conversion borrows from its input cannot satisfy that bound for its owned representation without changing conversion semantics or introducing overlapping implementations.This adds
ProtoString::copy_from_str(&str)with a default throughFrom<String>, removes the higher-rankedFrom<&str>bound, and uses the copying operation for borrowed JSON strings in non-optional singular fields and for generated view-to-owned conversions. The remote derive andbuffa-smolstrexample override it to retain their direct string construction path. Binary decoding still usesfrom_wire; optional/repeated/map/oneof JSON fields keep using the representation's native serde implementation.Custom types with an efficient
From<&str>should override the new method; otherwise these paths now allocate an intermediateString.The latest additions from @iainmcgin document the fallback allocation cost and pin the copying route with a
CountedStrfixture, so tests detect a conversion that produces the right value but bypasses the override.Compatibility
From<&str>fromS: ProtoStringneedS::copy_from_str(value)or an explicit conversion bound.json_helpers::proto_string::deserializenow requiresProtoString; hand-written implementations that only supplied the oldFromconversions need to implement the trait or provide their own deserializer.Stringallocation. Remote-derived types already forward to the inner type's direct conversion.From<&str>on its inner type. Types with a borrowing conversion can implementProtoStringby hand; this PR does not add a new derive option.Stringrepresentation and wire encoding are unchanged.Tests
Cowfixture keeps a borrowingFrom<&str>conversion and implementsProtoStringfor its owned variant. It cannot satisfy the original supertrait bound.CountedStrtests verify that view-to-owned conversion callscopy_from_strfor every string field shape, including map keys and values, and that borrowed-string/null JSON deserialization uses it.From<&str>implementation; derive and inline-storage tests exercise the copying override.Rechecked head
d1029b1on Linux with Rust 1.95.0 and protoc 33.5:task lint,task test(2,931 passed, 117 ignored, including doc tests), andtask docpassed.cargo check --workspace --all-featuresandbuffano-default-features checks, with and withoutjson, passed.main(c28717b); that unrelated difference is not included.The protobuf conformance suite, MSRV check, and cross-target checks were not rerun in this follow-up.
Closes #441.