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

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

* fix: handle bool and float zero literals in struct object Default impl

* fix: regenerate all test bindings with generate_code.py

* fix: data type check on swift build

* fix: test large array on struct and enum
This commit is contained in:
Renzo
2026-03-06 11:20:32 -08:00
committed by GitHub
parent 57659d9f38
commit 292870612c
37 changed files with 2181 additions and 27 deletions

View File

@@ -245,7 +245,7 @@ impl<'a> ArrayStruct {
}
}
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct ArrayStructT {
pub a: f32,
pub b: [i32; 15],
@@ -254,6 +254,18 @@ pub struct ArrayStructT {
pub e: i32,
pub f: [i64; 2],
}
impl Default for ArrayStructT {
fn default() -> Self {
Self {
a: 0.0,
b: [0; 15],
c: 0,
d: ::flatbuffers::array_init(|_| Default::default()),
e: 0,
f: [0; 2],
}
}
}
impl ArrayStructT {
pub fn pack(&self) -> ArrayStruct {

View File

@@ -0,0 +1,223 @@
// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
use super::*;
// struct LargeArrayStruct, aligned to 8
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq)]
pub struct LargeArrayStruct(pub [u8; 2496]);
impl Default for LargeArrayStruct {
fn default() -> Self {
Self([0; 2496])
}
}
impl ::core::fmt::Debug for LargeArrayStruct {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
f.debug_struct("LargeArrayStruct")
.field("d", &self.d())
.field("e", &self.e())
.field("f", &self.f())
.field("g", &self.g())
.field("h", &self.h())
.finish()
}
}
impl ::flatbuffers::SimpleToVerifyInSlice for LargeArrayStruct {}
impl<'a> ::flatbuffers::Follow<'a> for LargeArrayStruct {
type Inner = &'a LargeArrayStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
unsafe { <&'a LargeArrayStruct>::follow(buf, loc) }
}
}
impl<'a> ::flatbuffers::Follow<'a> for &'a LargeArrayStruct {
type Inner = &'a LargeArrayStruct;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
unsafe { ::flatbuffers::follow_cast_ref::<LargeArrayStruct>(buf, loc) }
}
}
impl<'b> ::flatbuffers::Push for LargeArrayStruct {
type Output = LargeArrayStruct;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
let src = unsafe { ::core::slice::from_raw_parts(self as *const LargeArrayStruct as *const u8, <Self as ::flatbuffers::Push>::size()) };
dst.copy_from_slice(src);
}
#[inline]
fn alignment() -> ::flatbuffers::PushAlignment {
::flatbuffers::PushAlignment::new(8)
}
}
impl<'a> ::flatbuffers::Verifiable for LargeArrayStruct {
#[inline]
fn run_verifier(
v: &mut ::flatbuffers::Verifier, pos: usize
) -> Result<(), ::flatbuffers::InvalidFlatbuffer> {
v.in_buffer::<Self>(pos)
}
}
impl<'a> LargeArrayStruct {
#[allow(clippy::too_many_arguments)]
pub fn new(
d: &[u8; 64],
e: &[f32; 64],
f: &[bool; 64],
g: &[NestedStruct; 64],
h: &[TestEnum; 64],
) -> Self {
let mut s = Self([0; 2496]);
s.set_d(d);
s.set_e(e);
s.set_f(f);
s.set_g(g);
s.set_h(h);
s
}
pub const fn get_fully_qualified_name() -> &'static str {
"MyGame.Example.LargeArrayStruct"
}
pub fn d(&'a self) -> ::flatbuffers::Array<'a, u8, 64> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
use ::flatbuffers::Follow;
unsafe { ::flatbuffers::Array::follow(&self.0, 0) }
}
pub fn set_d(&mut self, items: &[u8; 64]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe { ::flatbuffers::emplace_scalar_array(&mut self.0, 0, items) };
}
pub fn e(&'a self) -> ::flatbuffers::Array<'a, f32, 64> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
use ::flatbuffers::Follow;
unsafe { ::flatbuffers::Array::follow(&self.0, 64) }
}
pub fn set_e(&mut self, items: &[f32; 64]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe { ::flatbuffers::emplace_scalar_array(&mut self.0, 64, items) };
}
pub fn f(&'a self) -> ::flatbuffers::Array<'a, bool, 64> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
use ::flatbuffers::Follow;
unsafe { ::flatbuffers::Array::follow(&self.0, 320) }
}
pub fn set_f(&mut self, items: &[bool; 64]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe { ::flatbuffers::emplace_scalar_array(&mut self.0, 320, items) };
}
pub fn g(&'a self) -> ::flatbuffers::Array<'a, NestedStruct, 64> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
use ::flatbuffers::Follow;
unsafe { ::flatbuffers::Array::follow(&self.0, 384) }
}
pub fn set_g(&mut self, x: &[NestedStruct; 64]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe {
::core::ptr::copy(
x.as_ptr() as *const u8,
self.0.as_mut_ptr().add(384),
2048,
);
}
}
pub fn h(&'a self) -> ::flatbuffers::Array<'a, TestEnum, 64> {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
use ::flatbuffers::Follow;
unsafe { ::flatbuffers::Array::follow(&self.0, 2432) }
}
pub fn set_h(&mut self, x: &[TestEnum; 64]) {
// Safety:
// Created from a valid Table for this object
// Which contains a valid array in this slot
unsafe {
::core::ptr::copy(
x.as_ptr() as *const u8,
self.0.as_mut_ptr().add(2432),
64,
);
}
}
pub fn unpack(&self) -> LargeArrayStructT {
LargeArrayStructT {
d: self.d().into(),
e: self.e().into(),
f: self.f().into(),
g: { let g = self.g(); ::flatbuffers::array_init(|i| g.get(i).unpack()) },
h: self.h().into(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LargeArrayStructT {
pub d: [u8; 64],
pub e: [f32; 64],
pub f: [bool; 64],
pub g: [NestedStructT; 64],
pub h: [TestEnum; 64],
}
impl Default for LargeArrayStructT {
fn default() -> Self {
Self {
d: [0; 64],
e: [0.0; 64],
f: [false; 64],
g: ::flatbuffers::array_init(|_| Default::default()),
h: ::flatbuffers::array_init(|_| Default::default()),
}
}
}
impl LargeArrayStructT {
pub fn pack(&self) -> LargeArrayStruct {
LargeArrayStruct::new(
&self.d,
&self.e,
&self.f,
&::flatbuffers::array_init(|i| self.g[i].pack()),
&self.h,
)
}
}

View File

@@ -179,13 +179,23 @@ impl<'a> NestedStruct {
}
}
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct NestedStructT {
pub a: [i32; 2],
pub b: TestEnum,
pub c: [TestEnum; 2],
pub d: [i64; 2],
}
impl Default for NestedStructT {
fn default() -> Self {
Self {
a: [0; 2],
b: TestEnum::A,
c: ::flatbuffers::array_init(|_| Default::default()),
d: [0; 2],
}
}
}
impl NestedStructT {
pub fn pack(&self) -> NestedStruct {