Open DolajoCZ opened 2 weeks ago
For correction should be sufficient to replace this code:
by this code:
// Struct declaration
code_ += "#[derive(Debug, Copy, 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_ += "}";
Base on some tests everything looks good but I am not C++
developer so it is necessary to review it by some expert.
Issue
Invalid generation of rust code with switch
--gen-object-api
and struct containingarray
of size33
and more.Reason
Generated object api struct implement
Default
trait byderive
macro, butarray
implementDefault
trait only for size up to32
(source).Example of invalid fbs
Fbs file
example.fbs
Command
flatc.exe --rust --gen-object-api example.fbs
Invalid code section
Tool versions
flatc - 24.3.25 rustc - 1.82.0
Potential solution
For
struct
witharray
implement customDefault
trait.Example of solution from code above