Skip to content

Add unbox_oneof to store non-recursive message oneof variants inline (#126)#151

Open
sam-shridhar1950f wants to merge 1 commit into
anthropics:mainfrom
sam-shridhar1950f:feat/unbox-oneof-variants
Open

Add unbox_oneof to store non-recursive message oneof variants inline (#126)#151
sam-shridhar1950f wants to merge 1 commit into
anthropics:mainfrom
sam-shridhar1950f:feat/unbox-oneof-variants

Conversation

@sam-shridhar1950f
Copy link
Copy Markdown

Closes #126.

What

Adds an opt-out for the Box<T> wrapping on message- and group-typed oneof
variants. By default every such variant is boxed so recursive types compile;
this lets a config rule store matching non-recursive variants inline instead.

  • Config::unbox_oneof_in(&[".pkg.Msg.oneof.variant"]) opts specific paths out
  • Config::unbox_oneof() opts every non-recursive variant out
  • Paths match with the same proto-segment-aware prefix logic as use_bytes_type_in

How

A single variant_boxed(ctx, ty, variant_fqn) helper is the source of truth,
threaded through each place that boxes a variant: the enum declaration, the
From impls, binary merge, JSON deserialize, text encode and merge, and the
view-to-owned conversion.

Opting a recursive variant out is rejected when the code is generated (it would
otherwise emit an enum that fails to compile as unsized), so the downstream
sites can trust the helper.

Tests

  • Codegen: opt-out drops the Box for the matched variant while siblings stay
    boxed; opting a recursive variant out returns an error
  • Runtime round-trips for an inline variant across binary, JSON, and text, plus
    merge semantics, the From impl, and a still-boxed sibling

Net diff is about 250 lines excluding tests.

…pics#126)

Message and group oneof variants are wrapped in Box<T> unconditionally.
That wrapping is needed for recursive types, but for non-recursive
variants it adds a heap allocation per variant construction.

Add `unbox_oneof_in(paths)` / `unbox_oneof()` to buffa-build, backed by
`unboxed_oneof_fields` in CodeGenConfig, matched with the same
proto-segment-aware prefix logic as bytes_fields. A single `variant_boxed`
helper threads the decision through every site that boxes a variant: the
enum declaration, From impls, binary merge, JSON deserialize, text encode
and merge, and the view-to-owned conversion.

Opting a recursive variant out is rejected when the code is generated,
since storing it inline would produce an unsized type.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 24, 2026

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

@sam-shridhar1950f
Copy link
Copy Markdown
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 24, 2026
@sam-shridhar1950f
Copy link
Copy Markdown
Author

recheck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

buffa-codegen: configurable Box wrapping for message-type oneof variants

1 participant