Skip to content

Commit c5e5837

Browse files
author
JSU
committed
Bugfix default array
1 parent 609c72c commit c5e5837

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/idl_gen_rust.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2884,13 +2884,30 @@ class RustGenerator : public BaseGenerator {
28842884
// Generate Struct Object.
28852885
if (parser_.opts.generate_object_based_api) {
28862886
// Struct declaration
2887-
code_ += "#[derive(Debug, Clone, PartialEq, Default)]";
2887+
code_ += "#[derive(Debug, Copy, Clone, PartialEq)]";
28882888
code_ += "{{ACCESS_TYPE}} struct {{STRUCT_OTY}} {";
28892889
ForAllStructFields(struct_def, [&](const FieldDef &field) {
28902890
(void)field; // unused.
28912891
code_ += "pub {{FIELD}}: {{FIELD_OTY}},";
28922892
});
28932893
code_ += "}";
2894+
2895+
// Struct default trait implementation
2896+
code_ += "impl std::default::Default for {{STRUCT_OTY}} {";
2897+
code_ += " fn default() -> Self {";
2898+
code_ += " Self {";
2899+
ForAllStructFields(struct_def, [&](const FieldDef &field) {
2900+
const Type &type = field.value.type;
2901+
if (IsArray(type)) {
2902+
code_ += " {{FIELD}}: [Default::default(); " + NumToString(type.fixed_length) + "],";
2903+
} else {
2904+
code_ += " {{FIELD}}: Default::default(),";
2905+
}
2906+
});
2907+
code_ += " }";
2908+
code_ += " }";
2909+
code_ += "}";
2910+
28942911
// The `pack` method that turns the native struct into its Flatbuffers
28952912
// counterpart.
28962913
code_ += "impl {{STRUCT_OTY}} {";

0 commit comments

Comments
 (0)