[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:
jrop
2020-05-04 01:16:57 -06:00
committed by GitHub
parent 712866d57b
commit e3cb07d321

View File

@@ -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_ += " }";