summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCasper <casperneo@uchicago.edu>2020-10-02 10:30:39 -0700
committerGitHub <noreply@github.com>2020-10-02 10:30:39 -0700
commit8ec8322f091f93a6d5401a3ba9b2a164b28c9aee (patch)
treecacbfcccb81cbe547bedc62a1213f2f29ccba2bb /src
parentbbcc85fd4c77c078a5d9ca1680cc0b4b0b5c4c95 (diff)
downloadflatbuffers-8ec8322f091f93a6d5401a3ba9b2a164b28c9aee.tar.gz
flatbuffers-8ec8322f091f93a6d5401a3ba9b2a164b28c9aee.tar.bz2
flatbuffers-8ec8322f091f93a6d5401a3ba9b2a164b28c9aee.zip
Ruopt enum (#6156)
* Rust support for optional enums * make optional_scalars2 for languages that support optional enums Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_rust.cpp19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index 90cb5718..1e7502ae 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -673,6 +673,9 @@ class RustGenerator : public BaseGenerator {
}
case ftUnionKey:
case ftEnumKey: {
+ if (field.optional) {
+ return "None";
+ }
auto ev = field.value.type.enum_def->FindByValue(field.value.constant);
assert(ev);
return WrapInNameSpace(field.value.type.enum_def->defined_namespace,
@@ -724,7 +727,7 @@ class RustGenerator : public BaseGenerator {
case ftEnumKey:
case ftUnionKey: {
const auto typname = WrapInNameSpace(*type.enum_def);
- return typname;
+ return field.optional ? "Option<" + typname + ">" : typname;
}
case ftUnionValue: {
return "Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>";
@@ -872,7 +875,9 @@ class RustGenerator : public BaseGenerator {
case ftEnumKey:
case ftUnionKey: {
const auto underlying_typname = GetTypeBasic(type);
- return "self.fbb_.push_slot::<" + underlying_typname + ">";
+ return (field.optional ?
+ "self.fbb_.push_slot_always::<" :
+ "self.fbb_.push_slot::<") + underlying_typname + ">";
}
case ftStruct: {
@@ -925,7 +930,7 @@ class RustGenerator : public BaseGenerator {
case ftEnumKey:
case ftUnionKey: {
const auto typname = WrapInNameSpace(*type.enum_def);
- return typname;
+ return field.optional ? "Option<" + typname + ">" : typname;
}
case ftUnionValue: {
@@ -1027,8 +1032,12 @@ class RustGenerator : public BaseGenerator {
const auto underlying_typname = GetTypeBasic(type); //<- never used
const auto typname = WrapInNameSpace(*type.enum_def);
const auto default_value = GetDefaultScalarValue(field);
- return "self._tab.get::<" + typname + ">(" + offset_name + ", Some(" +
- default_value + ")).unwrap()";
+ if (field.optional) {
+ return "self._tab.get::<" + typname + ">(" + offset_name + ", None)";
+ } else {
+ return "self._tab.get::<" + typname + ">(" + offset_name + ", Some(" +
+ default_value + ")).unwrap()";
+ }
}
case ftString: {
return AddUnwrapIfRequired(