Rework how Rust generated files are laid out (#6731)

* Refactored Rust Generated code into a module directory.

Each symbol will be generated into one file and then
imported into a module. This breaks the "out_dir"
pattern where some users would generate code in their
target/ directory. Also, these objects are best used
in their own module. It will be hard for users to share
their own module structure with flatbuffers namespaces.

There may be solutions to these drawbacks but that should
be discussed. I don't want to overengineer here.

* shadow error

* try fix .bat file

* fix .bat 2

* Restore accidentally deleted files

* Fixed some DONOTSUBMITs and made Rust outdir pattern use symlinks.

* fixed binary files

* git clang format

* make generated onefiles not public and fix .bat

* reduced diff with master in generate_code.sh

* fix shadowed variable

* add object api flags to .bat

* space

* Removed extern crate and extra &

* use statement

* more clippy lints

* format

* Undo extern crate -> use change, it actually matters to our tests

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2021-07-22 19:02:28 -04:00
committed by GitHub
parent c36672d803
commit 12e341e4f8
83 changed files with 8894 additions and 8034 deletions

View File

@@ -23,15 +23,7 @@ mod flatbuffers_benchmarks;
mod flexbuffers_benchmarks;
#[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 = "../../monster_test_generated.rs"]
#[path = "../../monster_test/mod.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;

View File

@@ -14,18 +14,12 @@
* limitations under the License.
*/
use flatbuffers;
use bencher::Bencher;
#[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 = "../../monster_test_generated.rs"]
#[path = "../../monster_test/mod.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;

View File

@@ -29,15 +29,15 @@ static A: TrackingAllocator = TrackingAllocator;
// import the flatbuffers generated code:
extern crate flatbuffers;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
#[path = "../../include_test1/mod.rs"]
pub mod include_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"]
#[path = "../../include_test2/mod.rs"]
pub mod include_test2_generated;
#[allow(dead_code, unused_imports, clippy::approx_constant)]
#[path = "../../monster_test_generated.rs"]
#[path = "../../monster_test/mod.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;

View File

@@ -1,15 +1,15 @@
extern crate flatbuffers;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
#[path = "../../include_test1/mod.rs"]
pub mod include_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"]
#[path = "../../include_test2/mod.rs"]
pub mod include_test2_generated;
#[allow(dead_code, unused_imports, clippy::approx_constant)]
#[path = "../../monster_test_generated.rs"]
#[path = "../../monster_test/mod.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;

View File

@@ -0,0 +1 @@
src/generated

View File

@@ -2,11 +2,11 @@ fn main() {
use std::process::Command;
let project_root = std::env::current_dir()
.unwrap()
.parent() // flatbuffers/tests/rust_usage test
.parent() // flatbuffers/tests/rust_usage test
.unwrap()
.parent() // flatbuffers/tests
.parent() // flatbuffers/tests
.unwrap()
.parent() // flatbuffers/
.parent() // flatbuffers/
.unwrap()
.to_path_buf();
let sample_schema = {
@@ -22,17 +22,35 @@ fn main() {
f
};
let out_dir = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).to_path_buf();
let out_dir = {
let mut d = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).to_path_buf();
d.push("flatbuffers");
d
};
Command::new(&flatc)
.arg("-o")
.arg(&out_dir)
.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"))
.expect("Could not rename monster_ge.rs to monster_generated.rs");
assert!(out_dir.exists());
let generated = std::path::Path::new("src/generated");
#[cfg(target_os = "windows")]
{
if generated.exists() {
std::fs::remove_dir(generated).unwrap();
}
std::os::windows::fs::symlink_dir(out_dir, generated).unwrap();
}
#[cfg(not(target_os = "windows"))]
{
if generated.exists() {
std::fs::remove_file(generated).unwrap();
}
std::os::unix::fs::symlink(out_dir, generated).unwrap();
}
}

View File

@@ -1,30 +1,21 @@
// In this example, a build.rs file generates the code and then copies it into $OUT_DIR.
// In this example, a build.rs file generates the code and then copies it into generated/
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"));
}
#[allow(unused_imports, dead_code)]
mod generated;
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()
});
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);

View File

@@ -1,6 +1,6 @@
extern crate array_init;
#[allow(dead_code, unused_imports)]
#[path = "../../arrays_test_generated.rs"]
#[path = "../../arrays_test/mod.rs"]
mod arrays_test_generated;
use std::fmt::Debug;

View File

@@ -33,32 +33,28 @@ mod more_defaults_test;
mod optional_scalars_test;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
#[path = "../../include_test1/mod.rs"]
pub mod include_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"]
#[path = "../../include_test2/mod.rs"]
pub mod include_test2_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../namespace_test/namespace_test1_generated.rs"]
pub mod namespace_test1_generated;
#[path = "../../namespace_test/mod.rs"]
pub mod namespace_test_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"]
#[path = "../../monster_test/mod.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;
#[allow(dead_code, unused_imports)]
#[path = "../../optional_scalars_generated.rs"]
#[path = "../../optional_scalars/mod.rs"]
mod optional_scalars_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../arrays_test_generated.rs"]
#[path = "../../arrays_test/mod.rs"]
mod arrays_test_generated;
#[rustfmt::skip] // TODO: Use standard rust formatting and remove dead code.
@@ -2026,23 +2022,17 @@ mod generated_key_comparisons {
#[cfg(test)]
mod included_schema_generated_code {
extern crate flatbuffers;
//extern crate rust_usage_test;
#[test]
#[allow(unused_imports)]
fn namespace_test_mod_is_importable() {
use super::namespace_test_generated::{
namespace_a,
namespace_a::namespace_b,
namespace_c,
};
// TODO(rw): make generated sub-namespace files importable
//#[test]
//fn namespace_test_mod_is_importable() {
// use rust_usage_test::namespace_test;
//}
//#[test]
//fn namespace_test1_mod_is_importable() {
// use rust_usage_test::namespace_test::namespace_test1_generated;
//}
//#[test]
//fn namespace_test2_mod_is_importable() {
// use rust_usage_test::namespace_test::namespace_test2_generated;
//}
}
}
#[cfg(test)]
@@ -2113,7 +2103,7 @@ mod follow_impls {
// Define a test struct to use in a few tests. This replicates the work that the code generator
// would normally do when defining a FlatBuffer struct. For reference, compare the following
// `FooStruct` code with the code generated for the `Vec3` struct in
// `../../monster_test_generated.rs`.
// `../../monster_test/mod.rs`.
use flatbuffers::EndianScalar;
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(C, packed)]

View File

@@ -1,5 +1,5 @@
#[allow(dead_code, unused_imports)]
#[path = "../../more_defaults_generated.rs"]
#[path = "../../more_defaults/mod.rs"]
mod more_defaults_generated;
use self::more_defaults_generated::*;

View File

@@ -1,5 +1,5 @@
#[allow(dead_code, unused_imports)]
#[path = "../../optional_scalars_generated.rs"]
#[path = "../../optional_scalars/mod.rs"]
mod optional_scalars_generated;
use crate::optional_scalars_generated::optional_scalars::*;