[Rust] Add gen-name-strings for Rust (#5757)

* Add gen-name-strings for Rust

* Clang format

* Add tests and generate with gen-name-strings

* Clang-format

* Stop doing gen-name-strings with C++ code

* Bring generate_code.bat up with generate_code.sh
This commit is contained in:
Lee Mracek
2020-03-08 22:05:20 -04:00
committed by GitHub
parent f73d205bc7
commit 5a98d65e86
6 changed files with 78 additions and 3 deletions

View File

@@ -1127,6 +1127,16 @@ class RustGenerator : public BaseGenerator {
}
}
// Generates a fully-qualified name getter for use with --gen-name-strings
void GenFullyQualifiedNameGetter(const StructDef &struct_def,
const std::string &name) {
code_ += " pub const fn get_fully_qualified_name() -> &'static str {";
code_ += " \"" +
struct_def.defined_namespace->GetFullyQualifiedName(name) + "\"";
code_ += " }";
code_ += "";
}
// Generate an accessor struct, builder struct, and create function for a
// table.
void GenTable(const StructDef &struct_def) {
@@ -1157,6 +1167,11 @@ class RustGenerator : public BaseGenerator {
code_ += "}";
code_ += "";
code_ += "impl<'a> {{STRUCT_NAME}}<'a> {";
if (parser_.opts.generate_name_strings) {
GenFullyQualifiedNameGetter(struct_def, struct_def.name);
}
code_ += " #[inline]";
code_ +=
" pub fn init_from_table(table: flatbuffers::Table<'a>) -> "
@@ -1728,6 +1743,10 @@ class RustGenerator : public BaseGenerator {
code_ += " }";
code_ += " }";
if (parser_.opts.generate_name_strings) {
GenFullyQualifiedNameGetter(struct_def, struct_def.name);
}
// Generate accessor methods for the struct.
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {