Skip to content

Rust default array fix #8585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion samples/rust_generated/my_game/sample/vec_3_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,21 @@ impl<'a> Vec3 {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct Vec3T {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl std::default::Default for Vec3T {
fn default() -> Self {
Self {
x: Default::default(),
y: Default::default(),
z: Default::default(),
}
}
}
impl Vec3T {
pub fn pack(&self) -> Vec3 {
Vec3::new(
Expand Down
19 changes: 18 additions & 1 deletion src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2884,13 +2884,30 @@ class RustGenerator : public BaseGenerator {
// Generate Struct Object.
if (parser_.opts.generate_object_based_api) {
// Struct declaration
code_ += "#[derive(Debug, Clone, PartialEq, Default)]";
code_ += "#[derive(Debug, Clone, PartialEq)]";
code_ += "{{ACCESS_TYPE}} struct {{STRUCT_OTY}} {";
ForAllStructFields(struct_def, [&](const FieldDef &field) {
(void)field; // unused.
code_ += "pub {{FIELD}}: {{FIELD_OTY}},";
});
code_ += "}";

// Struct default trait implementation
code_ += "impl std::default::Default for {{STRUCT_OTY}} {";
code_ += " fn default() -> Self {";
code_ += " Self {";
ForAllStructFields(struct_def, [&](const FieldDef &field) {
const Type &type = field.value.type;
if (IsArray(type)) {
code_ += " {{FIELD}}: [Default::default(); " + NumToString(type.fixed_length) + "],";
} else {
code_ += " {{FIELD}}: Default::default(),";
}
});
code_ += " }";
code_ += " }";
code_ += "}";

// The `pack` method that turns the native struct into its Flatbuffers
// counterpart.
code_ += "impl {{STRUCT_OTY}} {";
Expand Down
14 changes: 13 additions & 1 deletion tests/arrays_test/my_game/example/array_struct_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'a> ArrayStruct {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct ArrayStructT {
pub a: f32,
pub b: [i32; 15],
Expand All @@ -249,6 +249,18 @@ pub struct ArrayStructT {
pub e: i32,
pub f: [i64; 2],
}
impl std::default::Default for ArrayStructT {
fn default() -> Self {
Self {
a: Default::default(),
b: [Default::default(); 15],
c: Default::default(),
d: [Default::default(); 2],
e: Default::default(),
f: [Default::default(); 2],
}
}
}
impl ArrayStructT {
pub fn pack(&self) -> ArrayStruct {
ArrayStruct::new(
Expand Down
12 changes: 11 additions & 1 deletion tests/arrays_test/my_game/example/nested_struct_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,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 std::default::Default for NestedStructT {
fn default() -> Self {
Self {
a: [Default::default(); 2],
b: Default::default(),
c: [Default::default(); 2],
d: [Default::default(); 2],
}
}
}
impl NestedStructT {
pub fn pack(&self) -> NestedStruct {
NestedStruct::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ impl<'a> Unused {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct UnusedT {
pub a: i32,
}
impl std::default::Default for UnusedT {
fn default() -> Self {
Self {
a: Default::default(),
}
}
}
impl UnusedT {
pub fn pack(&self) -> Unused {
Unused::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ impl<'a> Unused {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct UnusedT {
pub a: i32,
}
impl std::default::Default for UnusedT {
fn default() -> Self {
Self {
a: Default::default(),
}
}
}
impl UnusedT {
pub fn pack(&self) -> Unused {
Unused::new(
Expand Down
10 changes: 9 additions & 1 deletion tests/monster_test/my_game/example/ability_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,19 @@ impl<'a> Ability {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct AbilityT {
pub id: u32,
pub distance: u32,
}
impl std::default::Default for AbilityT {
fn default() -> Self {
Self {
id: Default::default(),
distance: Default::default(),
}
}
}
impl AbilityT {
pub fn pack(&self) -> Ability {
Ability::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,21 @@ impl<'a> StructOfStructs {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct StructOfStructsT {
pub a: AbilityT,
pub b: TestT,
pub c: AbilityT,
}
impl std::default::Default for StructOfStructsT {
fn default() -> Self {
Self {
a: Default::default(),
b: Default::default(),
c: Default::default(),
}
}
}
impl StructOfStructsT {
pub fn pack(&self) -> StructOfStructs {
StructOfStructs::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ impl<'a> StructOfStructsOfStructs {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct StructOfStructsOfStructsT {
pub a: StructOfStructsT,
}
impl std::default::Default for StructOfStructsOfStructsT {
fn default() -> Self {
Self {
a: Default::default(),
}
}
}
impl StructOfStructsOfStructsT {
pub fn pack(&self) -> StructOfStructsOfStructs {
StructOfStructsOfStructs::new(
Expand Down
10 changes: 9 additions & 1 deletion tests/monster_test/my_game/example/test_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ impl<'a> Test {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct TestT {
pub a: i16,
pub b: i8,
}
impl std::default::Default for TestT {
fn default() -> Self {
Self {
a: Default::default(),
b: Default::default(),
}
}
}
impl TestT {
pub fn pack(&self) -> Test {
Test::new(
Expand Down
14 changes: 13 additions & 1 deletion tests/monster_test/my_game/example/vec_3_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'a> Vec3 {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct Vec3T {
pub x: f32,
pub y: f32,
Expand All @@ -271,6 +271,18 @@ pub struct Vec3T {
pub test2: Color,
pub test3: TestT,
}
impl std::default::Default for Vec3T {
fn default() -> Self {
Self {
x: Default::default(),
y: Default::default(),
z: Default::default(),
test1: Default::default(),
test2: Default::default(),
test3: Default::default(),
}
}
}
impl Vec3T {
pub fn pack(&self) -> Vec3 {
Vec3::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@ impl<'a> Unused {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct UnusedT {
pub a: i32,
}
impl std::default::Default for UnusedT {
fn default() -> Self {
Self {
a: Default::default(),
}
}
}
impl UnusedT {
pub fn pack(&self) -> Unused {
Unused::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,19 @@ impl<'a> Ability {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct AbilityT {
pub id: u32,
pub distance: u32,
}
impl std::default::Default for AbilityT {
fn default() -> Self {
Self {
id: Default::default(),
distance: Default::default(),
}
}
}
impl AbilityT {
pub fn pack(&self) -> Ability {
Ability::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,21 @@ impl<'a> StructOfStructs {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct StructOfStructsT {
pub a: AbilityT,
pub b: TestT,
pub c: AbilityT,
}
impl std::default::Default for StructOfStructsT {
fn default() -> Self {
Self {
a: Default::default(),
b: Default::default(),
c: Default::default(),
}
}
}
impl StructOfStructsT {
pub fn pack(&self) -> StructOfStructs {
StructOfStructs::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ impl<'a> StructOfStructsOfStructs {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct StructOfStructsOfStructsT {
pub a: StructOfStructsT,
}
impl std::default::Default for StructOfStructsOfStructsT {
fn default() -> Self {
Self {
a: Default::default(),
}
}
}
impl StructOfStructsOfStructsT {
pub fn pack(&self) -> StructOfStructsOfStructs {
StructOfStructsOfStructs::new(
Expand Down
10 changes: 9 additions & 1 deletion tests/monster_test_serialize/my_game/example/test_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,19 @@ impl<'a> Test {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct TestT {
pub a: i16,
pub b: i8,
}
impl std::default::Default for TestT {
fn default() -> Self {
Self {
a: Default::default(),
b: Default::default(),
}
}
}
impl TestT {
pub fn pack(&self) -> Test {
Test::new(
Expand Down
14 changes: 13 additions & 1 deletion tests/monster_test_serialize/my_game/example/vec_3_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl<'a> Vec3 {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub struct Vec3T {
pub x: f32,
pub y: f32,
Expand All @@ -289,6 +289,18 @@ pub struct Vec3T {
pub test2: Color,
pub test3: TestT,
}
impl std::default::Default for Vec3T {
fn default() -> Self {
Self {
x: Default::default(),
y: Default::default(),
z: Default::default(),
test1: Default::default(),
test2: Default::default(),
test3: Default::default(),
}
}
}
impl Vec3T {
pub fn pack(&self) -> Vec3 {
Vec3::new(
Expand Down
Loading