From e3cb07d321e28bdd913eac2a9ae70d131ec2cdb9 Mon Sep 17 00:00:00 2001 From: jrop Date: Mon, 4 May 2020 01:16:57 -0600 Subject: [PATCH] [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 --- src/idl_gen_rust.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 55b8439b6..cece22120 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -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_ += " }";