summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--samples/monster_generated.rs1
-rw-r--r--samples/sample_binary.rs1
-rw-r--r--src/idl_gen_rust.cpp4
-rwxr-xr-xtests/RustTest.sh8
-rw-r--r--tests/include_test/include_test1_generated.rs1
-rw-r--r--tests/include_test/sub/include_test2_generated.rs1
-rw-r--r--tests/monster_test_generated.rs1
-rw-r--r--tests/namespace_test/namespace_test1_generated.rs1
-rw-r--r--tests/namespace_test/namespace_test2_generated.rs1
-rw-r--r--tests/optional_scalars_generated.rs1
-rw-r--r--tests/rust_usage_test/Cargo.toml1
-rw-r--r--tests/rust_usage_test/outdir/Cargo.toml10
-rw-r--r--tests/rust_usage_test/outdir/build.rs39
-rw-r--r--tests/rust_usage_test/outdir/src/main.rs38
-rw-r--r--tests/rust_usage_test/tests/integration_test.rs5
15 files changed, 104 insertions, 9 deletions
diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs
index 13b2cf14..cc2051e1 100644
--- a/samples/monster_generated.rs
+++ b/samples/monster_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
diff --git a/samples/sample_binary.rs b/samples/sample_binary.rs
index 1b08f205..67f05e69 100644
--- a/samples/sample_binary.rs
+++ b/samples/sample_binary.rs
@@ -18,6 +18,7 @@
extern crate flatbuffers;
// import the generated code
+#[allow(dead_code, unused_imports)]
#[path = "./monster_generated.rs"]
#[allow(clippy::approx_constant)] // We use low precision PI as a default value.
mod monster_generated;
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp
index 1677ce82..424112c3 100644
--- a/src/idl_gen_rust.cpp
+++ b/src/idl_gen_rust.cpp
@@ -2497,7 +2497,9 @@ class RustGenerator : public BaseGenerator {
}
void GenNamespaceImports(const int white_spaces) {
- if (white_spaces == 0) { code_ += "#![allow(unused_imports, dead_code)]"; }
+ // DO not use global attributes (i.e. #![...]) since it interferes
+ // with users who include! generated files.
+ // See: https://github.com/google/flatbuffers/issues/6261
std::string indent = std::string(white_spaces, ' ');
code_ += "";
if (!parser_.opts.generate_all) {
diff --git a/tests/RustTest.sh b/tests/RustTest.sh
index 7fa64926..71258c92 100755
--- a/tests/RustTest.sh
+++ b/tests/RustTest.sh
@@ -48,6 +48,14 @@ check_test_result "No Cargo clippy lints test"
cargo bench $TARGET_FLAG
+# This test is dependent on flatc.
+if [[ -f ../../flatc ]]; then
+ cd outdir
+ cargo test
+ check_test_result "Rust generated file in \$OUT_DIR"
+ cd ..
+fi
+
# RUST_NIGHTLY environment variable set in dockerfile.
if [[ $RUST_NIGHTLY == 1 ]]; then
rustup +nightly component add miri
diff --git a/tests/include_test/include_test1_generated.rs b/tests/include_test/include_test1_generated.rs
index 4015a3c8..d7511f9e 100644
--- a/tests/include_test/include_test1_generated.rs
+++ b/tests/include_test/include_test1_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use crate::include_test2_generated::*;
use std::mem;
diff --git a/tests/include_test/sub/include_test2_generated.rs b/tests/include_test/sub/include_test2_generated.rs
index 9b3c7ee6..787ec19f 100644
--- a/tests/include_test/sub/include_test2_generated.rs
+++ b/tests/include_test/sub/include_test2_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*;
use std::mem;
diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs
index bae1066c..a514c7a5 100644
--- a/tests/monster_test_generated.rs
+++ b/tests/monster_test_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*;
use crate::include_test2_generated::*;
diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs
index 88c7cb2e..aa4dde6b 100644
--- a/tests/namespace_test/namespace_test1_generated.rs
+++ b/tests/namespace_test/namespace_test1_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
diff --git a/tests/namespace_test/namespace_test2_generated.rs b/tests/namespace_test/namespace_test2_generated.rs
index f86e424e..e0ecd72d 100644
--- a/tests/namespace_test/namespace_test2_generated.rs
+++ b/tests/namespace_test/namespace_test2_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
diff --git a/tests/optional_scalars_generated.rs b/tests/optional_scalars_generated.rs
index 73ebee30..81483b58 100644
--- a/tests/optional_scalars_generated.rs
+++ b/tests/optional_scalars_generated.rs
@@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
-#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
diff --git a/tests/rust_usage_test/Cargo.toml b/tests/rust_usage_test/Cargo.toml
index 664396d0..45ecca66 100644
--- a/tests/rust_usage_test/Cargo.toml
+++ b/tests/rust_usage_test/Cargo.toml
@@ -36,7 +36,6 @@ path = "../../samples/sample_flexbuffers_serde.rs"
name = "sample_flatbuffers"
path = "../../samples/sample_binary.rs"
-
[dev-dependencies]
quickcheck = "0.6"
# TODO(rw): look into moving to criterion.rs
diff --git a/tests/rust_usage_test/outdir/Cargo.toml b/tests/rust_usage_test/outdir/Cargo.toml
new file mode 100644
index 00000000..8387d170
--- /dev/null
+++ b/tests/rust_usage_test/outdir/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "outdir"
+version = "0.1.0"
+authors = ["Casper Neo <cneo@google.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+flatbuffers = { path = "../../../rust/flatbuffers" }
diff --git a/tests/rust_usage_test/outdir/build.rs b/tests/rust_usage_test/outdir/build.rs
new file mode 100644
index 00000000..663ca99f
--- /dev/null
+++ b/tests/rust_usage_test/outdir/build.rs
@@ -0,0 +1,39 @@
+fn main() {
+ use std::process::Command;
+
+ let project_root = std::env::current_dir()
+ .unwrap()
+ .parent() // flatbuffers/tests/rust_usage test
+ .unwrap()
+ .parent() // flatbuffers/tests
+ .unwrap()
+ .parent() // flatbuffers/
+ .unwrap()
+ .to_path_buf();
+
+ let sample_schema = {
+ let mut s = project_root.to_path_buf();
+ s.push("samples");
+ s.push("monster.fbs");
+ s
+ };
+
+ let flatc = {
+ let mut f = project_root.to_path_buf();
+ f.push("flatc");
+ f
+ };
+
+ let out_dir = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).to_path_buf();
+
+ Command::new(&flatc)
+ .arg("--rust")
+ .arg(&sample_schema)
+ .arg("--filename-suffix")
+ .arg("_gen")
+ .output()
+ .expect("Failed to generate file");
+
+ let genfile = "monster_gen.rs";
+ std::fs::rename(&genfile, out_dir.join("monster_generated.rs")).unwrap();
+}
diff --git a/tests/rust_usage_test/outdir/src/main.rs b/tests/rust_usage_test/outdir/src/main.rs
new file mode 100644
index 00000000..b5fcaf8a
--- /dev/null
+++ b/tests/rust_usage_test/outdir/src/main.rs
@@ -0,0 +1,38 @@
+// In this example, a build.rs file generates the code and then copies it into $OUT_DIR.
+extern crate flatbuffers;
+
+#[cfg(target_family = "unix")]
+#[allow(dead_code, unused_imports)]
+mod generated {
+ include!(concat!(env!("OUT_DIR"), "/monster_generated.rs"));
+}
+
+#[cfg(target_family = "windows")]
+#[allow(dead_code, unused_imports)]
+mod generated {
+ include!(concat!(env!("OUT_DIR"), "\\monster_generated.rs"));
+}
+
+use generated::my_game::sample::{Monster, MonsterArgs};
+
+
+fn main() {
+ let mut fbb = flatbuffers::FlatBufferBuilder::new();
+ let name = Some(fbb.create_string("bob"));
+ let m = Monster::create(&mut fbb, &MonsterArgs {
+ hp: 1,
+ mana: 2,
+ name,
+ ..Default::default()
+ });
+ fbb.finish(m, None);
+ let mon = flatbuffers::root::<Monster>(fbb.finished_data()).unwrap();
+ assert_eq!(mon.hp(), 1);
+ assert_eq!(mon.mana(), 2);
+ assert_eq!(mon.name().unwrap(), "bob");
+}
+
+#[test]
+fn test_main() {
+ main()
+}
diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs
index 652a098a..fa686476 100644
--- a/tests/rust_usage_test/tests/integration_test.rs
+++ b/tests/rust_usage_test/tests/integration_test.rs
@@ -31,18 +31,23 @@ extern crate quickcheck_derive;
mod flexbuffers_tests;
mod optional_scalars_test;
+#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
pub mod include_test1_generated;
+#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"]
pub mod include_test2_generated;
+#[allow(dead_code, unused_imports)]
#[path = "../../namespace_test/namespace_test1_generated.rs"]
pub mod namespace_test1_generated;
+#[allow(dead_code, unused_imports)]
#[path = "../../namespace_test/namespace_test2_generated.rs"]
pub mod namespace_test2_generated;
+#[allow(dead_code, unused_imports)]
#[path = "../../monster_test_generated.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;