Skip to content

Commit

Permalink
FlatBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed Mar 13, 2024
1 parent 46d5fa5 commit 7dad9bb
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"vortex-alp",
"vortex-dict",
"vortex-fastlanes",
"vortex-flatbuffers",
"vortex-ree",
"vortex-roaring",
"vortex-zigzag",
Expand Down
1 change: 1 addition & 0 deletions vortex-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ rayon = "1.8.1"
roaring = "0.10.3"
vortex-alloc = { path = "../vortex-alloc" }
thiserror = "1.0.57"
vortex-flatbuffers = { path = "../vortex-flatbuffers" }
21 changes: 21 additions & 0 deletions vortex-flatbuffers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "vortex-flatbuffers"
version = { workspace = true }
description = "Flatbuffers for Vortex arrays"
homepage = { workspace = true }
repository = { workspace = true }
authors = { workspace = true }
license = { workspace = true }
keywords = { workspace = true }
include = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }

[lints]
workspace = true

[dependencies]
flatbuffers = "23.5.26"

[build-dependencies]
walkdir = "2.4.0"
55 changes: 55 additions & 0 deletions vortex-flatbuffers/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use walkdir::WalkDir;

fn main() {
let buildrs_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.canonicalize()
.expect("Failed to canonicalize CARGO_MANIFEST_DIR");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap())
.canonicalize()
.expect("Failed to canonicalize OUT_DIR");
let _root_dir = buildrs_dir
.join("../")
.canonicalize()
.expect("Failed to canonicalize root dir");
let flatbuffers_dir = buildrs_dir.join("flatbuffers");

rerun_if_changed(&buildrs_dir.join("build.rs"));
WalkDir::new(&flatbuffers_dir)
.into_iter()
.filter_map(|e| e.ok())
.for_each(|e| rerun_if_changed(e.path()));

let exit = Command::new("flatc")
.arg("--rust")
.arg("-o")
.arg(out_dir.join("flatbuffers"))
.args(
WalkDir::new(&flatbuffers_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension() == Some(OsStr::new("fbs")))
.map(|d| d.path().to_path_buf())
.map(|d| {
rerun_if_changed(d.as_path());
d
}),
)
.status()
.expect("Failed to execute flatc")
.success();
assert_eq!(exit, true, "flatc failed");
}

fn rerun_if_changed(path: &Path) {
println!(
"cargo:rerun-if-changed={}",
path.canonicalize()
.unwrap_or_else(|_| panic!("failed to canonicalize {}", path.to_str().unwrap()))
.to_str()
.unwrap()
);
}
34 changes: 34 additions & 0 deletions vortex-flatbuffers/flatbuffers/monsters.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Example IDL file for our monster's schema.
// https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

namespace MyGame.Sample;

enum Color:byte { Red = 0, Green, Blue = 2 }

union Equipment { Weapon } // Optionally add more tables.

struct Vec3 {
x:float;
y:float;
z:float;
}

table Monster {
pos:Vec3; // Struct.
mana:short = 150;
hp:short = 100;
name:string;
friendly:bool = false (deprecated);
inventory:[ubyte]; // Vector of scalars.
color:Color = Blue; // Enum.
weapons:[Weapon]; // Vector of tables.
equipped:Equipment; // Union.
path:[Vec3]; // Vector of structs.
}

table Weapon {
name:string;
damage:short;
}

root_type Monster;
10 changes: 10 additions & 0 deletions vortex-flatbuffers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(unused_imports)]
#![allow(dead_code)]
mod flatbuffers {
include!(concat!(
env!("OUT_DIR"),
"/flatbuffers/monsters_generated.rs"
));
}

pub use flatbuffers::*;

0 comments on commit 7dad9bb

Please sign in to comment.