[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
This commit is contained in:
jean-airoldie 2019-01-24 16:25:56 -05:00 committed by Wouter van Oortmerssen
parent c2f40c37b2
commit 38bf4cfc03
4 changed files with 46 additions and 8 deletions

View File

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

View File

@ -1,6 +1,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(dead_code)]
#![allow(unused_imports)]
use std::mem;
use std::cmp::Ordering;
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
pub mod my_game {
#![allow(dead_code)]
#![allow(unused_imports)]

View File

@ -1,6 +1,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(dead_code)]
#![allow(unused_imports)]
use std::mem;
use std::cmp::Ordering;
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
pub mod namespace_a {
#![allow(dead_code)]
#![allow(unused_imports)]

View File

@ -1,6 +1,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(dead_code)]
#![allow(unused_imports)]
use std::mem;
use std::cmp::Ordering;
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
pub mod namespace_a {
#![allow(dead_code)]
#![allow(unused_imports)]