1 // In this example, a build.rs file generates the code and then copies it into $OUT_DIR.
2 extern crate flatbuffers;
3
4 #[cfg(target_family = "unix")]
5 #[allow(dead_code, unused_imports)]
6 mod generated {
7 include!(concat!(env!("OUT_DIR"), "/monster_generated.rs"));
8 }
9
10 #[cfg(target_family = "windows")]
11 #[allow(dead_code, unused_imports)]
12 mod generated {
13 include!(concat!(env!("OUT_DIR"), "\\monster_generated.rs"));
14 }
15
16 use generated::my_game::sample::{Monster, MonsterArgs};
17
18
main()19 fn main() {
20 let mut fbb = flatbuffers::FlatBufferBuilder::new();
21 let name = Some(fbb.create_string("bob"));
22 let m = Monster::create(&mut fbb, &MonsterArgs {
23 hp: 1,
24 mana: 2,
25 name,
26 ..Default::default()
27 });
28 fbb.finish(m, None);
29 let mon = flatbuffers::root::<Monster>(fbb.finished_data()).unwrap();
30 assert_eq!(mon.hp(), 1);
31 assert_eq!(mon.mana(), 2);
32 assert_eq!(mon.name().unwrap(), "bob");
33 }
34
35 #[test]
test_main()36 fn test_main() {
37 main()
38 }
39