Skip to content

fix(flatbuffers): use manual impl Default for struct object types#8947

Open
RenzoMXD wants to merge 1 commit intogoogle:masterfrom
RenzoMXD:fix/rust-derive-default-large-arrays
Open

fix(flatbuffers): use manual impl Default for struct object types#8947
RenzoMXD wants to merge 1 commit intogoogle:masterfrom
RenzoMXD:fix/rust-derive-default-large-arrays

Conversation

@RenzoMXD
Copy link

@RenzoMXD RenzoMXD commented Mar 3, 2026

Fix Rust codegen: use manual impl Default for struct object types

Problem

When a FlatBuffers struct contains a fixed-size array with more than 32 elements (e.g. [ubyte:64]), the generated Rust object API type uses #[derive(Default)] which fails to compile on Rust < 1.61, because Default was not implemented for [T; N] where N > 32 until that version.

Fix

Changed the Rust code generator (src/idl_gen_rust.cpp) to emit a manual impl Default for struct object types instead of
#[derive(Default)]. This matches the pattern already used for table object types.

The generated default values are:

  • Scalar/enum fields: use GetDefaultValue() (0, false, etc.)
  • Arrays of builtins: [0; N] (avoids the Default trait bound)
  • Arrays of structs/enums: ::flatbuffers::array_init(|_| Default::default())

Code generation changes

Changes were made to src/idl_gen_rust.cpp.

Test

Added LargeArrayStruct { d:[ubyte:64]; } to tests/arrays_test.fbs .

Closes #8943

@RenzoMXD RenzoMXD requested a review from dbaileychess as a code owner March 3, 2026 16:12
@google-cla
Copy link

google-cla bot commented Mar 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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 rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Rust]: Object API codegen for arrays missing Default trait implementation

1 participant