OwnedView: remove Deref, add generated FooOwnedView accessor wrappers#154
Merged
Conversation
The Deref<Target = V> impl exposed the inner view as FooView<'static>, so borrowed fields appeared 'static to the borrow checker and could be held past the point where the OwnedView and its Bytes buffer were dropped. Field access now goes through reborrow(), which ties every borrow to the handle. The Serialize impl forwards via the inner view directly, and a compile_fail doctest pins that field access on the handle no longer compiles.
For each message (when views are generated, excluding map entries), emit a FooOwnedView newtype around OwnedView<FooView<'static>> with one &self-tied accessor method per field, plus decode, decode_with_options, from_owned, view(), to_owned_message, bytes, into_bytes, From conversions to and from the raw OwnedView, and a Serialize forwarding impl when JSON generation is enabled. The wrapper is re-exported at the package root next to Foo and FooView. A field or oneof whose name collides with a reserved wrapper method keeps working through view(); its accessor is skipped with the new CodeGenWarning::OwnedViewAccessorSuppressed. The view type helpers now take the borrow lifetime as a parameter so accessor return types can use the anonymous lifetime. Regenerates checked-in WKT and bootstrap descriptor types.
Rewrite the OwnedView sections of the guide, README, DESIGN, and the prost migration doc around reborrow() and the generated FooOwnedView accessor wrappers, and add CHANGELOG entries for the Deref removal and the new wrapper types.
|
All contributors have signed the CLA ✍️ ✅ |
rpb-ant
approved these changes
May 28, 2026
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
OwnedView<V>previously implementedDeref<Target = V>. The stored view isFooView<'static>, where the'staticis synthetic — the borrows actually point into the handle'sBytesbuffer. ThroughDeref, safe code could copy borrowed fields (&'static str,&'static [u8]) out of the handle and keep them after the handle dropped, at which point they dangle. Triggering this requires the calling application to deliberately hold a field reference beyond the handle's lifetime, so the practical exposure is limited — but the API should not permit it at all.This PR:
Derefimpl; access goes through the existingreborrow(), which ties every borrow to the handle, with acompile_faildoctest pinning the removal (buffa/src/view.rs);FooOwnedViewwrapper types: a per-message newtype aroundOwnedView<FooView<'static>>with one&self-tied accessor per field (owned.name(),owned.id(), …), plusdecode/decode_with_options/from_owned/view()/to_owned_message/bytes/into_bytes,Fromconversions to and from the rawOwnedView, and JSONSerializeforwarding. The wrapper is re-exported at the package root next toFooandFooView.reborrow()is a pure lifetime coercion, so the accessors compile to the same code as the old direct field access;Breaking change
owned.fieldno longer compiles onOwnedView. Migrate either withowned.reborrow().field(or once per scope:let view = owned.reborrow();), or by switching to the generatedFooOwnedViewand usingowned.field().serde_json::to_string(&owned_view)on the handle is unchanged.Notes
generate_views; there is no separate knob, so generated-code size grows accordingly.bytes,view,decode, …) keeps working throughview(); its accessor is skipped with a build warning (CodeGenWarning::OwnedViewAccessorSuppressed).DerefonOwnedView).