From fa722f19cf4434903998c6e5bfc99b78ba611c36 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 25 Jun 2020 23:22:18 +0800 Subject: [PATCH] add flatbuffers --- flatbuffers/Cargo.lock | 30 ++ flatbuffers/Cargo.toml | 11 + flatbuffers/README.md | 35 ++ flatbuffers/monster.fbs | 34 ++ flatbuffers/src/main.rs | 30 ++ flatbuffers/src/monster_generated.rs | 522 +++++++++++++++++++++++++++ 6 files changed, 662 insertions(+) create mode 100644 flatbuffers/Cargo.lock create mode 100644 flatbuffers/Cargo.toml create mode 100644 flatbuffers/README.md create mode 100644 flatbuffers/monster.fbs create mode 100644 flatbuffers/src/main.rs create mode 100644 flatbuffers/src/monster_generated.rs diff --git a/flatbuffers/Cargo.lock b/flatbuffers/Cargo.lock new file mode 100644 index 0000000..8bad362 --- /dev/null +++ b/flatbuffers/Cargo.lock @@ -0,0 +1,30 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "flatbuffers" +version = "0.1.0" +dependencies = [ + "flatbuffers 0.6.1", + "hex", +] + +[[package]] +name = "flatbuffers" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a788f068dd10687940565bf4b5480ee943176cbd114b12e811074bcf7c04e4b9" +dependencies = [ + "smallvec", +] + +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" + +[[package]] +name = "smallvec" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" diff --git a/flatbuffers/Cargo.toml b/flatbuffers/Cargo.toml new file mode 100644 index 0000000..f6a266a --- /dev/null +++ b/flatbuffers/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "flatbuffers" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +flatbuffers = "0.6.1" +hex = "0.4.2" diff --git a/flatbuffers/README.md b/flatbuffers/README.md new file mode 100644 index 0000000..c4370ee --- /dev/null +++ b/flatbuffers/README.md @@ -0,0 +1,35 @@ + + +# Install `flatc` + +## Install `cmake` + +https://cmake.org/install/ + +## Get `flatbuffers` + +```shell +% git clone https://github.com/google/flatbuffers.git +``` + +## Make + +```shell +% cmake -G "Unix Makefiles" +% make +% make install +``` + +```shell +% flatc --version +``` + +# Compile `fbs` + +```shell +% flatc --rust file.fbs +``` + + + + diff --git a/flatbuffers/monster.fbs b/flatbuffers/monster.fbs new file mode 100644 index 0000000..b2b503b --- /dev/null +++ b/flatbuffers/monster.fbs @@ -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; diff --git a/flatbuffers/src/main.rs b/flatbuffers/src/main.rs new file mode 100644 index 0000000..24c489f --- /dev/null +++ b/flatbuffers/src/main.rs @@ -0,0 +1,30 @@ +#[allow(dead_code, unused_imports)] +mod monster_generated; + +use flatbuffers::FlatBufferBuilder; +use monster_generated::my_game::sample::Monster; +use monster_generated::my_game::sample::MonsterArgs; + +// https://github.com/frol/flatc-rust/blob/master/examples/tutorial/src/main.rs +fn main() { + let mut fbb = FlatBufferBuilder::new(); + + let name = fbb.create_string("Sample Name!"); + + let monster = Monster::create(&mut fbb, &MonsterArgs { + mana: 150, + hp: 80, + name: Some(name), + ..Default::default() + }); + + fbb.finish(monster, None); + + let buff = fbb.finished_data(); + + println!("FlatBuffers: {}", hex::encode(&buff)); + + let monster_2 = flatbuffers::get_root::(&buff); + println!("Monster hp: {}", monster_2.hp()); + println!("Monster name: {:?}", monster_2.name()); +} diff --git a/flatbuffers/src/monster_generated.rs b/flatbuffers/src/monster_generated.rs new file mode 100644 index 0000000..b9f04bf --- /dev/null +++ b/flatbuffers/src/monster_generated.rs @@ -0,0 +1,522 @@ +// automatically generated by the FlatBuffers compiler, do not modify + + +use std::mem; +use std::cmp::Ordering; + +extern crate flatbuffers; +use self::flatbuffers::EndianScalar; + +#[allow(unused_imports, dead_code)] +pub mod my_game { + + use std::mem; + use std::cmp::Ordering; + + extern crate flatbuffers; + use self::flatbuffers::EndianScalar; +#[allow(unused_imports, dead_code)] +pub mod sample { + + use std::mem; + use std::cmp::Ordering; + + extern crate flatbuffers; + use self::flatbuffers::EndianScalar; + +#[allow(non_camel_case_types)] +#[repr(i8)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +pub enum Color { + Red = 0, + Green = 1, + Blue = 2, + +} + +pub const ENUM_MIN_COLOR: i8 = 0; +pub const ENUM_MAX_COLOR: i8 = 2; + +impl<'a> flatbuffers::Follow<'a> for Color { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::read_scalar_at::(buf, loc) + } +} + +impl flatbuffers::EndianScalar for Color { + #[inline] + fn to_little_endian(self) -> Self { + let n = i8::to_le(self as i8); + let p = &n as *const i8 as *const Color; + unsafe { *p } + } + #[inline] + fn from_little_endian(self) -> Self { + let n = i8::from_le(self as i8); + let p = &n as *const i8 as *const Color; + unsafe { *p } + } +} + +impl flatbuffers::Push for Color { + type Output = Color; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, *self); + } +} + +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_COLOR:[Color; 3] = [ + Color::Red, + Color::Green, + Color::Blue +]; + +#[allow(non_camel_case_types)] +pub const ENUM_NAMES_COLOR:[&'static str; 3] = [ + "Red", + "Green", + "Blue" +]; + +pub fn enum_name_color(e: Color) -> &'static str { + let index = e as i8; + ENUM_NAMES_COLOR[index as usize] +} + +#[allow(non_camel_case_types)] +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +pub enum Equipment { + NONE = 0, + Weapon = 1, + +} + +pub const ENUM_MIN_EQUIPMENT: u8 = 0; +pub const ENUM_MAX_EQUIPMENT: u8 = 1; + +impl<'a> flatbuffers::Follow<'a> for Equipment { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::read_scalar_at::(buf, loc) + } +} + +impl flatbuffers::EndianScalar for Equipment { + #[inline] + fn to_little_endian(self) -> Self { + let n = u8::to_le(self as u8); + let p = &n as *const u8 as *const Equipment; + unsafe { *p } + } + #[inline] + fn from_little_endian(self) -> Self { + let n = u8::from_le(self as u8); + let p = &n as *const u8 as *const Equipment; + unsafe { *p } + } +} + +impl flatbuffers::Push for Equipment { + type Output = Equipment; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + flatbuffers::emplace_scalar::(dst, *self); + } +} + +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_EQUIPMENT:[Equipment; 2] = [ + Equipment::NONE, + Equipment::Weapon +]; + +#[allow(non_camel_case_types)] +pub const ENUM_NAMES_EQUIPMENT:[&'static str; 2] = [ + "NONE", + "Weapon" +]; + +pub fn enum_name_equipment(e: Equipment) -> &'static str { + let index = e as u8; + ENUM_NAMES_EQUIPMENT[index as usize] +} + +pub struct EquipmentUnionTableOffset {} +// struct Vec3, aligned to 4 +#[repr(C, align(4))] +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Vec3 { + x_: f32, + y_: f32, + z_: f32, +} // pub struct Vec3 +impl flatbuffers::SafeSliceAccess for Vec3 {} +impl<'a> flatbuffers::Follow<'a> for Vec3 { + type Inner = &'a Vec3; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Vec3>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Vec3 { + type Inner = &'a Vec3; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Vec3 { + type Output = Vec3; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Vec3 { + type Output = Vec3; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Vec3 as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + + +impl Vec3 { + pub fn new<'a>(_x: f32, _y: f32, _z: f32) -> Self { + Vec3 { + x_: _x.to_little_endian(), + y_: _y.to_little_endian(), + z_: _z.to_little_endian(), + + } + } + pub fn x<'a>(&'a self) -> f32 { + self.x_.from_little_endian() + } + pub fn y<'a>(&'a self) -> f32 { + self.y_.from_little_endian() + } + pub fn z<'a>(&'a self) -> f32 { + self.z_.from_little_endian() + } +} + +pub enum MonsterOffset {} +#[derive(Copy, Clone, Debug, PartialEq)] + +pub struct Monster<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for Monster<'a> { + type Inner = Monster<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { + _tab: flatbuffers::Table { buf: buf, loc: loc }, + } + } +} + +impl<'a> Monster<'a> { + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + Monster { + _tab: table, + } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args MonsterArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = MonsterBuilder::new(_fbb); + if let Some(x) = args.path { builder.add_path(x); } + if let Some(x) = args.equipped { builder.add_equipped(x); } + if let Some(x) = args.weapons { builder.add_weapons(x); } + if let Some(x) = args.inventory { builder.add_inventory(x); } + if let Some(x) = args.name { builder.add_name(x); } + if let Some(x) = args.pos { builder.add_pos(x); } + builder.add_hp(args.hp); + builder.add_mana(args.mana); + builder.add_equipped_type(args.equipped_type); + builder.add_color(args.color); + builder.finish() + } + + pub const VT_POS: flatbuffers::VOffsetT = 4; + pub const VT_MANA: flatbuffers::VOffsetT = 6; + pub const VT_HP: flatbuffers::VOffsetT = 8; + pub const VT_NAME: flatbuffers::VOffsetT = 10; + pub const VT_INVENTORY: flatbuffers::VOffsetT = 14; + pub const VT_COLOR: flatbuffers::VOffsetT = 16; + pub const VT_WEAPONS: flatbuffers::VOffsetT = 18; + pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20; + pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22; + pub const VT_PATH: flatbuffers::VOffsetT = 24; + + #[inline] + pub fn pos(&self) -> Option<&'a Vec3> { + self._tab.get::(Monster::VT_POS, None) + } + #[inline] + pub fn mana(&self) -> i16 { + self._tab.get::(Monster::VT_MANA, Some(150)).unwrap() + } + #[inline] + pub fn hp(&self) -> i16 { + self._tab.get::(Monster::VT_HP, Some(100)).unwrap() + } + #[inline] + pub fn name(&self) -> Option<&'a str> { + self._tab.get::>(Monster::VT_NAME, None) + } + #[inline] + pub fn inventory(&self) -> Option<&'a [u8]> { + self._tab.get::>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn color(&self) -> Color { + self._tab.get::(Monster::VT_COLOR, Some(Color::Blue)).unwrap() + } + #[inline] + pub fn weapons(&self) -> Option>>> { + self._tab.get::>>>>(Monster::VT_WEAPONS, None) + } + #[inline] + pub fn equipped_type(&self) -> Equipment { + self._tab.get::(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap() + } + #[inline] + pub fn equipped(&self) -> Option> { + self._tab.get::>>(Monster::VT_EQUIPPED, None) + } + #[inline] + pub fn path(&self) -> Option<&'a [Vec3]> { + self._tab.get::>>(Monster::VT_PATH, None).map(|v| v.safe_slice() ) + } + #[inline] + #[allow(non_snake_case)] + pub fn equipped_as_weapon(&self) -> Option> { + if self.equipped_type() == Equipment::Weapon { + self.equipped().map(|u| Weapon::init_from_table(u)) + } else { + None + } + } + +} + +pub struct MonsterArgs<'a> { + pub pos: Option<&'a Vec3>, + pub mana: i16, + pub hp: i16, + pub name: Option>, + pub inventory: Option>>, + pub color: Color, + pub weapons: Option>>>>, + pub equipped_type: Equipment, + pub equipped: Option>, + pub path: Option>>, +} +impl<'a> Default for MonsterArgs<'a> { + #[inline] + fn default() -> Self { + MonsterArgs { + pos: None, + mana: 150, + hp: 100, + name: None, + inventory: None, + color: Color::Blue, + weapons: None, + equipped_type: Equipment::NONE, + equipped: None, + path: None, + } + } +} +pub struct MonsterBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> { + #[inline] + pub fn add_pos(&mut self, pos: &'b Vec3) { + self.fbb_.push_slot_always::<&Vec3>(Monster::VT_POS, pos); + } + #[inline] + pub fn add_mana(&mut self, mana: i16) { + self.fbb_.push_slot::(Monster::VT_MANA, mana, 150); + } + #[inline] + pub fn add_hp(&mut self, hp: i16) { + self.fbb_.push_slot::(Monster::VT_HP, hp, 100); + } + #[inline] + pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>) { + self.fbb_.push_slot_always::>(Monster::VT_NAME, name); + } + #[inline] + pub fn add_inventory(&mut self, inventory: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_INVENTORY, inventory); + } + #[inline] + pub fn add_color(&mut self, color: Color) { + self.fbb_.push_slot::(Monster::VT_COLOR, color, Color::Blue); + } + #[inline] + pub fn add_weapons(&mut self, weapons: flatbuffers::WIPOffset>>>) { + self.fbb_.push_slot_always::>(Monster::VT_WEAPONS, weapons); + } + #[inline] + pub fn add_equipped_type(&mut self, equipped_type: Equipment) { + self.fbb_.push_slot::(Monster::VT_EQUIPPED_TYPE, equipped_type, Equipment::NONE); + } + #[inline] + pub fn add_equipped(&mut self, equipped: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_EQUIPPED, equipped); + } + #[inline] + pub fn add_path(&mut self, path: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_PATH, path); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> { + let start = _fbb.start_table(); + MonsterBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +pub enum WeaponOffset {} +#[derive(Copy, Clone, Debug, PartialEq)] + +pub struct Weapon<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for Weapon<'a> { + type Inner = Weapon<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { + _tab: flatbuffers::Table { buf: buf, loc: loc }, + } + } +} + +impl<'a> Weapon<'a> { + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + Weapon { + _tab: table, + } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args WeaponArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = WeaponBuilder::new(_fbb); + if let Some(x) = args.name { builder.add_name(x); } + builder.add_damage(args.damage); + builder.finish() + } + + pub const VT_NAME: flatbuffers::VOffsetT = 4; + pub const VT_DAMAGE: flatbuffers::VOffsetT = 6; + + #[inline] + pub fn name(&self) -> Option<&'a str> { + self._tab.get::>(Weapon::VT_NAME, None) + } + #[inline] + pub fn damage(&self) -> i16 { + self._tab.get::(Weapon::VT_DAMAGE, Some(0)).unwrap() + } +} + +pub struct WeaponArgs<'a> { + pub name: Option>, + pub damage: i16, +} +impl<'a> Default for WeaponArgs<'a> { + #[inline] + fn default() -> Self { + WeaponArgs { + name: None, + damage: 0, + } + } +} +pub struct WeaponBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> WeaponBuilder<'a, 'b> { + #[inline] + pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>) { + self.fbb_.push_slot_always::>(Weapon::VT_NAME, name); + } + #[inline] + pub fn add_damage(&mut self, damage: i16) { + self.fbb_.push_slot::(Weapon::VT_DAMAGE, damage, 0); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> WeaponBuilder<'a, 'b> { + let start = _fbb.start_table(); + WeaponBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +#[inline] +pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + flatbuffers::get_root::>(buf) +} + +#[inline] +pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + flatbuffers::get_size_prefixed_root::>(buf) +} + +#[inline] +pub fn finish_monster_buffer<'a, 'b>( + fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, + root: flatbuffers::WIPOffset>) { + fbb.finish(root, None); +} + +#[inline] +pub fn finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { + fbb.finish_size_prefixed(root, None); +} +} // pub mod Sample +} // pub mod MyGame +