mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
[Rust] idl_gen_rust.cpp: (Option/required-aware codegen for unions) (#5850)
* idl_gen_rust.cpp: Fix google/flatbuffers#5849 (Option/required-aware codegen for unions) The generated code was assuming that an Option is always returned by the union-getter method: however, this is only true if the field is not marked as `(required)`. * idl_gen_rust.cpp: flip conditional * idl_gen_rust.cpp: Add comment, as requested in review The code added is not covered by the integration test
This commit is contained in:
@@ -1350,9 +1350,17 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ +=
|
||||
" if self.{{FIELD_TYPE_FIELD_NAME}}_type() == "
|
||||
"{{U_ELEMENT_ENUM_TYPE}} {";
|
||||
code_ +=
|
||||
|
||||
// The following logic is not tested in the integration test,
|
||||
// as of April 10, 2020
|
||||
if (field.required) {
|
||||
code_ += " let u = self.{{FIELD_NAME}}();";
|
||||
code_ += " Some({{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
||||
} else {
|
||||
code_ +=
|
||||
" self.{{FIELD_NAME}}().map(|u| "
|
||||
"{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
||||
}
|
||||
code_ += " } else {";
|
||||
code_ += " None";
|
||||
code_ += " }";
|
||||
|
||||
Reference in New Issue
Block a user