Rust: remove inner attributes (#6410)

* remove inner attributes

* Added test for outdir in Rust

* add bin/outdir

* Moved outdir test to its own package and only run it if flatc is available

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper 2021-01-26 11:09:29 -05:00 committed by GitHub
parent efcbdc7698
commit 6effe431bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 104 additions and 9 deletions

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem; use std::mem;
use std::cmp::Ordering; use std::cmp::Ordering;

View File

@ -18,6 +18,7 @@
extern crate flatbuffers; extern crate flatbuffers;
// import the generated code // import the generated code
#[allow(dead_code, unused_imports)]
#[path = "./monster_generated.rs"] #[path = "./monster_generated.rs"]
#[allow(clippy::approx_constant)] // We use low precision PI as a default value. #[allow(clippy::approx_constant)] // We use low precision PI as a default value.
mod monster_generated; mod monster_generated;

View File

@ -2497,7 +2497,9 @@ class RustGenerator : public BaseGenerator {
} }
void GenNamespaceImports(const int white_spaces) { 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, ' '); std::string indent = std::string(white_spaces, ' ');
code_ += ""; code_ += "";
if (!parser_.opts.generate_all) { if (!parser_.opts.generate_all) {

View File

@ -48,6 +48,14 @@ check_test_result "No Cargo clippy lints test"
cargo bench $TARGET_FLAG 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. # RUST_NIGHTLY environment variable set in dockerfile.
if [[ $RUST_NIGHTLY == 1 ]]; then if [[ $RUST_NIGHTLY == 1 ]]; then
rustup +nightly component add miri rustup +nightly component add miri

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test2_generated::*; use crate::include_test2_generated::*;
use std::mem; use std::mem;

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*; use crate::include_test1_generated::*;
use std::mem; use std::mem;

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*; use crate::include_test1_generated::*;
use crate::include_test2_generated::*; use crate::include_test2_generated::*;

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem; use std::mem;
use std::cmp::Ordering; use std::cmp::Ordering;

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem; use std::mem;
use std::cmp::Ordering; use std::cmp::Ordering;

View File

@ -1,7 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem; use std::mem;
use std::cmp::Ordering; use std::cmp::Ordering;

View File

@ -36,7 +36,6 @@ path = "../../samples/sample_flexbuffers_serde.rs"
name = "sample_flatbuffers" name = "sample_flatbuffers"
path = "../../samples/sample_binary.rs" path = "../../samples/sample_binary.rs"
[dev-dependencies] [dev-dependencies]
quickcheck = "0.6" quickcheck = "0.6"
# TODO(rw): look into moving to criterion.rs # TODO(rw): look into moving to criterion.rs

View File

@ -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" }

View File

@ -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();
}

View File

@ -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()
}

View File

@ -31,18 +31,23 @@ extern crate quickcheck_derive;
mod flexbuffers_tests; mod flexbuffers_tests;
mod optional_scalars_test; mod optional_scalars_test;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"] #[path = "../../include_test/include_test1_generated.rs"]
pub mod include_test1_generated; pub mod include_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"] #[path = "../../include_test/sub/include_test2_generated.rs"]
pub mod include_test2_generated; pub mod include_test2_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../namespace_test/namespace_test1_generated.rs"] #[path = "../../namespace_test/namespace_test1_generated.rs"]
pub mod namespace_test1_generated; pub mod namespace_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../namespace_test/namespace_test2_generated.rs"] #[path = "../../namespace_test/namespace_test2_generated.rs"]
pub mod namespace_test2_generated; pub mod namespace_test2_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../monster_test_generated.rs"] #[path = "../../monster_test_generated.rs"]
mod monster_test_generated; mod monster_test_generated;
pub use monster_test_generated::my_game; pub use monster_test_generated::my_game;