From 38bf4cfc03baf93a58fd54dcbe63b638b490bdbf Mon Sep 17 00:00:00 2001 From: jean-airoldie Date: Thu, 24 Jan 2019 16:25:56 -0500 Subject: [PATCH] [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 --- src/idl_gen_rust.cpp | 27 +++++++++++++------ tests/monster_test_generated.rs | 9 +++++++ .../namespace_test1_generated.rs | 9 +++++++ .../namespace_test2_generated.rs | 9 +++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index b530137b2..77b7bea87 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_ += ""; } diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index 574339116..82393ea9e 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -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)] diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs index a2abf8f75..7fb6c9784 100644 --- a/tests/namespace_test/namespace_test1_generated.rs +++ b/tests/namespace_test/namespace_test1_generated.rs @@ -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)] diff --git a/tests/namespace_test/namespace_test2_generated.rs b/tests/namespace_test/namespace_test2_generated.rs index ac78936f3..c2e70216a 100644 --- a/tests/namespace_test/namespace_test2_generated.rs +++ b/tests/namespace_test/namespace_test2_generated.rs @@ -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)]