Merge commit '0f6aab9da6fe982218a01f4a5b896e65fcced437' as 'third_party/flatbuffers'
This commit is contained in:
@@ -0,0 +1 @@
|
||||
src/generated
|
||||
@@ -0,0 +1,11 @@
|
||||
[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" }
|
||||
serde = "1.0"
|
||||
@@ -0,0 +1,57 @@
|
||||
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 = {
|
||||
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("--rust-module-root-file")
|
||||
.arg(&sample_schema)
|
||||
.output()
|
||||
.expect("Failed to generate file");
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// In this example, a build.rs file generates the code and then copies it into generated/
|
||||
extern crate flatbuffers;
|
||||
#[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()
|
||||
},
|
||||
);
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user