summaryrefslogtreecommitdiff
path: root/src/idl_gen_rust.cpp
diff options
context:
space:
mode:
authorjean-airoldie <maxence.caron.1@ulaval.ca>2019-01-24 16:25:56 -0500
committerWouter van Oortmerssen <aardappel@gmail.com>2019-01-24 13:25:56 -0800
commit38bf4cfc03baf93a58fd54dcbe63b638b490bdbf (patch)
tree9958f71bd34c81fdfbcea41fdd815a3db6f7f1e7 /src/idl_gen_rust.cpp
parentc2f40c37b2c0cf2c7fbb82b4b760276771abe7a1 (diff)
downloadflatbuffers-38bf4cfc03baf93a58fd54dcbe63b638b490bdbf.tar.gz
flatbuffers-38bf4cfc03baf93a58fd54dcbe63b638b490bdbf.tar.bz2
flatbuffers-38bf4cfc03baf93a58fd54dcbe63b638b490bdbf.zip
[Rust] Added global namespace imports (#5121)
* [Rust] Added global namespace imports * Documented the need for global imports * Added white_space params to GenNamespaceImports * Removed a \n from GenNamespaceImports
Diffstat (limited to 'src/idl_gen_rust.cpp')
-rw-r--r--src/idl_gen_rust.cpp27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index b530137b..77b7bea8 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -298,6 +298,11 @@ class RustGenerator : public BaseGenerator {
assert(!cur_name_space_);
+ // Generate imports for the global scope in case no namespace is used
+ // in the schema file.
+ GenNamespaceImports(0);
+ code_ += "";
+
// Generate all code in their namespaces, once, because Rust does not
// permit re-opening modules.
//
@@ -1740,6 +1745,18 @@ class RustGenerator : public BaseGenerator {
code_ += "";
}
+ void GenNamespaceImports(const int white_spaces) {
+ std::string indent = std::string(white_spaces, ' ');
+ code_ += indent + "#![allow(dead_code)]";
+ code_ += indent + "#![allow(unused_imports)]";
+ code_ += "";
+ code_ += indent + "use std::mem;";
+ code_ += indent + "use std::cmp::Ordering;";
+ code_ += "";
+ code_ += indent + "extern crate flatbuffers;";
+ code_ += indent + "use self::flatbuffers::EndianScalar;";
+ }
+
// Set up the correct namespace. This opens a namespace if the current
// namespace is different from the target namespace. This function
// closes and opens the namespaces only as necessary.
@@ -1774,14 +1791,8 @@ class RustGenerator : public BaseGenerator {
// in the previous example, E, then F, then G are opened
for (auto j = common_prefix_size; j != new_size; ++j) {
code_ += "pub mod " + MakeSnakeCase(ns->components[j]) + " {";
- code_ += " #![allow(dead_code)]";
- code_ += " #![allow(unused_imports)]";
- code_ += "";
- code_ += " use std::mem;";
- code_ += " use std::cmp::Ordering;";
- code_ += "";
- code_ += " extern crate flatbuffers;";
- code_ += " use self::flatbuffers::EndianScalar;";
+ // Generate local namespace imports.
+ GenNamespaceImports(2);
}
if (new_size != common_prefix_size) { code_ += ""; }