summaryrefslogtreecommitdiff
path: root/src/idl_gen_rust.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/idl_gen_rust.cpp')
-rw-r--r--src/idl_gen_rust.cpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index dc26fa32..248e68c5 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -2318,7 +2318,10 @@ class RustGenerator : public BaseGenerator {
code_.SetValue("FIELD_OFFSET", NumToString(offset_to_field));
code_.SetValue("REF", IsStruct(field.value.type) ? "&" : "");
cb(field);
- offset_to_field += SizeOf(field.value.type.base_type) + field.padding;
+ const size_t size = IsStruct(field.value.type)
+ ? field.value.type.struct_def->bytesize
+ : SizeOf(field.value.type.base_type);
+ offset_to_field += size + field.padding;
}
}
// Generate an accessor struct with constructor for a flatbuffers struct.
@@ -2339,8 +2342,13 @@ class RustGenerator : public BaseGenerator {
// hold for PartialOrd/Ord.
code_ += "// struct {{STRUCT_NAME}}, aligned to {{ALIGN}}";
code_ += "#[repr(transparent)]";
- code_ += "#[derive(Clone, Copy, PartialEq, Default)]";
+ code_ += "#[derive(Clone, Copy, PartialEq)]";
code_ += "pub struct {{STRUCT_NAME}}(pub [u8; {{STRUCT_SIZE}}]);";
+ code_ += "impl Default for {{STRUCT_NAME}} { ";
+ code_ += " fn default() -> Self { ";
+ code_ += " Self([0; {{STRUCT_SIZE}}])";
+ code_ += " }";
+ code_ += "}";
// Debug for structs.
code_ += "impl std::fmt::Debug for {{STRUCT_NAME}} {";