summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2021-03-29 19:56:45 -0400
committerGitHub <noreply@github.com>2021-03-29 19:56:45 -0400
commit4133a39df80546ff5269894a3961c7069fcd45d0 (patch)
treeb09b468c622fcd00adc6b7c4220c8c7f605b1b21 /src
parent1c26d2a1a0a24cf4050bc35a3c707dd862d34bc9 (diff)
downloadflatbuffers-4133a39df80546ff5269894a3961c7069fcd45d0.tar.gz
flatbuffers-4133a39df80546ff5269894a3961c7069fcd45d0.tar.bz2
flatbuffers-4133a39df80546ff5269894a3961c7069fcd45d0.zip
Rust structz (#6539)
* Rust structz * struct of structs test * swift tmp variables Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_rust.cpp12
-rw-r--r--src/idl_gen_swift.cpp4
2 files changed, 12 insertions, 4 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}} {";
diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp
index 984b6aa5..afac8451 100644
--- a/src/idl_gen_swift.cpp
+++ b/src/idl_gen_swift.cpp
@@ -935,8 +935,8 @@ class SwiftGenerator : public BaseGenerator {
auto type = GenType(field.value.type);
code_.SetValue("VALUENAME", name);
if (IsStruct(field.value.type)) {
- code_ += "var _v = _t.{{VALUENAME}}";
- code_ += "_{{VALUENAME}} = _v.unpack()";
+ code_ += "var _v{{VALUENAME}} = _t.{{VALUENAME}}";
+ code_ += "_{{VALUENAME}} = _v{{VALUENAME}}.unpack()";
continue;
}
std::string is_enum = IsEnum(field.value.type) ? ".value" : "";