Skip to content

Rust: codegen + runtime support for vectors of unions (#5024)#9062

Draft
MukundaKatta wants to merge 1 commit intogoogle:masterfrom
MukundaKatta:feat/rust-vector-of-unions
Draft

Rust: codegen + runtime support for vectors of unions (#5024)#9062
MukundaKatta wants to merge 1 commit intogoogle:masterfrom
MukundaKatta:feat/rust-vector-of-unions

Conversation

@MukundaKatta
Copy link
Copy Markdown

Summary

Refs #5024. Adds Rust codegen and runtime support for vectors of unions, following the paired-vectors layout used by C++/Go/Java codegen.

What's done

  • idl_parser.cpp whitelists Rust for [Union] schemas via SupportsAdvancedUnionFeatures().
  • idl_gen_rust.cpp emits _type (Vector) and value vector accessors plus per-variant <field>_as_<variant>(idx) helpers.
  • flatbuffers::TableVerifier::visit_union_vector runtime helper verifies discriminant and value vectors in lockstep, enforces same-length invariant (matches C++ PR Ensure the two vectors in a vector of unions are the same size in verifier #8853), and dispatches per-variant verifiers.
  • 5 runtime tests covering valid round-trip, length mismatch, orphan type-vector, empty pair, and absent optional.
  • RUST_VECTORS_OF_UNIONS_DESIGN.md walks the wire format, generated Rust shape, verifier contract, forward-compat handling, and aliased-variant behavior.

What's TODO

  • Write-side create_vector_of_unions builder helper plus matching codegen wiring (four sites scaffolded with unimplemented!() and TODO(#5024) markers).
  • Native pack/unpack for owned Vec<UnionT>.
  • Adding union_vector.fbs to scripts/generate_code.py's Rust block. flatc was not available locally to regenerate .rs test files; CI will need to do that.
  • JSON serialization (--rust-serialize) for [Union] fields is currently a skip_field stub.

Test plan

  • cargo test --tests in tests/rust_usage_test: 322 tests pass (276 + 21 + 13 + 5 + 4 + 1 + 1 + 1). The pre-existing with_internal_capacity_preallocates_vecs lib unit test fails on master too, unrelated to this branch.

Marking draft until write-side lands.

Implements the read side of vector-of-union codegen in idl_gen_rust.cpp,
following the paired-vectors layout used by C++/Go/Java codegen. Adds a
Rust runtime accessor and round-trip test. Write-side codegen is
scaffolded with TODOs. Refs google#5024.
@github-actions github-actions Bot added c++ rust codegen Involving generating code from schema documentation Documentation labels Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema documentation Documentation rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant