diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 71de8254c..e41b8aadd 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -1203,6 +1203,9 @@ extern bool GenerateSwiftGRPC(const Parser &parser, const std::string &path, extern bool GenerateTSGRPC(const Parser &parser, const std::string &path, const std::string &file_name); + +extern bool GenerateRustModuleRootFile(const Parser &parser, + const std::string &path); } // namespace flatbuffers #endif // FLATBUFFERS_IDL_H_ diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs deleted file mode 100644 index b3d826747..000000000 --- a/samples/monster_generated.rs +++ /dev/null @@ -1,1006 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod my_game { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; -#[allow(unused_imports, dead_code)] -pub mod sample { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_COLOR: i8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_COLOR: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_COLOR: [Color; 3] = [ - Color::Red, - Color::Green, - Color::Blue, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct Color(pub i8); -#[allow(non_upper_case_globals)] -impl Color { - pub const Red: Self = Self(0); - pub const Green: Self = Self(1); - pub const Blue: Self = Self(2); - - pub const ENUM_MIN: i8 = 0; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::Red, - Self::Green, - Self::Blue, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::Red => Some("Red"), - Self::Green => Some("Green"), - Self::Blue => Some("Blue"), - _ => None, - } - } -} -impl std::fmt::Debug for Color { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for Color { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for Color { - type Output = Color; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for Color { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for Color { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Color {} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_EQUIPMENT: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_EQUIPMENT: u8 = 1; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [ - Equipment::NONE, - Equipment::Weapon, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct Equipment(pub u8); -#[allow(non_upper_case_globals)] -impl Equipment { - pub const NONE: Self = Self(0); - pub const Weapon: Self = Self(1); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 1; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::Weapon, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::Weapon => Some("Weapon"), - _ => None, - } - } -} -impl std::fmt::Debug for Equipment { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for Equipment { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for Equipment { - type Output = Equipment; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for Equipment { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for Equipment { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Equipment {} -pub struct EquipmentUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum EquipmentT { - NONE, - Weapon(Box), -} -impl Default for EquipmentT { - fn default() -> Self { - Self::NONE - } -} -impl EquipmentT { - pub fn equipment_type(&self) -> Equipment { - match self { - Self::NONE => Equipment::NONE, - Self::Weapon(_) => Equipment::Weapon, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::Weapon(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned WeaponT, setting the union to NONE. - pub fn take_weapon(&mut self) -> Option> { - if let Self::Weapon(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::Weapon(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the WeaponT. - pub fn as_weapon(&self) -> Option<&WeaponT> { - if let Self::Weapon(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the WeaponT. - pub fn as_weapon_mut(&mut self) -> Option<&mut WeaponT> { - if let Self::Weapon(v) = self { Some(v.as_mut()) } else { None } - } -} -// struct Vec3, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct Vec3(pub [u8; 12]); -impl Default for Vec3 { - fn default() -> Self { - Self([0; 12]) - } -} -impl std::fmt::Debug for Vec3 { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Vec3") - .field("x", &self.x()) - .field("y", &self.y()) - .field("z", &self.z()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for 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<'a> flatbuffers::Verifiable for Vec3 { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> Vec3 { - #[allow(clippy::too_many_arguments)] - pub fn new( - x: f32, - y: f32, - z: f32, - ) -> Self { - let mut s = Self([0; 12]); - s.set_x(x); - s.set_y(y); - s.set_z(z); - s - } - - pub fn x(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_x(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn y(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[4..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_y(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[4..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn z(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[8..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_z(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[8..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn unpack(&self) -> Vec3T { - Vec3T { - x: self.x(), - y: self.y(), - z: self.z(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct Vec3T { - pub x: f32, - pub y: f32, - pub z: f32, -} -impl Vec3T { - pub fn pack(&self) -> Vec3 { - Vec3::new( - self.x, - self.y, - self.z, - ) - } -} - -pub enum MonsterOffset {} -#[derive(Copy, Clone, 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, 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 fn unpack(&self) -> MonsterT { - let pos = self.pos().map(|x| { - x.unpack() - }); - let mana = self.mana(); - let hp = self.hp(); - let name = self.name().map(|x| { - x.to_string() - }); - let inventory = self.inventory().map(|x| { - x.to_vec() - }); - let color = self.color(); - let weapons = self.weapons().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let equipped = match self.equipped_type() { - Equipment::NONE => EquipmentT::NONE, - Equipment::Weapon => EquipmentT::Weapon(Box::new( - self.equipped_as_weapon() - .expect("Invalid union table, expected `Equipment::Weapon`.") - .unpack() - )), - _ => EquipmentT::NONE, - }; - let path = self.path().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - MonsterT { - pos, - mana, - hp, - name, - inventory, - color, - weapons, - equipped, - path, - } - } - 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(Weapon::init_from_table) - } else { - None - } - } - -} - -impl flatbuffers::Verifiable for Monster<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"pos", Self::VT_POS, false)? - .visit_field::(&"mana", Self::VT_MANA, false)? - .visit_field::(&"hp", Self::VT_HP, false)? - .visit_field::>(&"name", Self::VT_NAME, false)? - .visit_field::>>(&"inventory", Self::VT_INVENTORY, false)? - .visit_field::(&"color", Self::VT_COLOR, false)? - .visit_field::>>>(&"weapons", Self::VT_WEAPONS, false)? - .visit_union::(&"equipped_type", Self::VT_EQUIPPED_TYPE, &"equipped", Self::VT_EQUIPPED, false, |key, v, pos| { - match key { - Equipment::Weapon => v.verify_union_variant::>("Equipment::Weapon", pos), - _ => Ok(()), - } - })? - .visit_field::>>(&"path", Self::VT_PATH, false)? - .finish(); - Ok(()) - } -} -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: &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()) - } -} - -impl std::fmt::Debug for Monster<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Monster"); - ds.field("pos", &self.pos()); - ds.field("mana", &self.mana()); - ds.field("hp", &self.hp()); - ds.field("name", &self.name()); - ds.field("inventory", &self.inventory()); - ds.field("color", &self.color()); - ds.field("weapons", &self.weapons()); - ds.field("equipped_type", &self.equipped_type()); - match self.equipped_type() { - Equipment::Weapon => { - if let Some(x) = self.equipped_as_weapon() { - ds.field("equipped", &x) - } else { - ds.field("equipped", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - _ => { - let x: Option<()> = None; - ds.field("equipped", &x) - }, - }; - ds.field("path", &self.path()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct MonsterT { - pub pos: Option, - pub mana: i16, - pub hp: i16, - pub name: Option, - pub inventory: Option>, - pub color: Color, - pub weapons: Option>, - pub equipped: EquipmentT, - pub path: Option>, -} -impl Default for MonsterT { - fn default() -> Self { - Self { - pos: None, - mana: 150, - hp: 100, - name: None, - inventory: None, - color: Color::Blue, - weapons: None, - equipped: EquipmentT::NONE, - path: None, - } - } -} -impl MonsterT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let pos_tmp = self.pos.as_ref().map(|x| x.pack()); - let pos = pos_tmp.as_ref(); - let mana = self.mana; - let hp = self.hp; - let name = self.name.as_ref().map(|x|{ - _fbb.create_string(x) - }); - let inventory = self.inventory.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let color = self.color; - let weapons = self.weapons.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) - }); - let equipped_type = self.equipped.equipment_type(); - let equipped = self.equipped.pack(_fbb); - let path = self.path.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) - }); - Monster::create(_fbb, &MonsterArgs{ - pos, - mana, - hp, - name, - inventory, - color, - weapons, - equipped_type, - equipped, - path, - }) - } -} -pub enum WeaponOffset {} -#[derive(Copy, Clone, 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, 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 fn unpack(&self) -> WeaponT { - let name = self.name().map(|x| { - x.to_string() - }); - let damage = self.damage(); - WeaponT { - name, - damage, - } - } - 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() - } -} - -impl flatbuffers::Verifiable for Weapon<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"name", Self::VT_NAME, false)? - .visit_field::(&"damage", Self::VT_DAMAGE, false)? - .finish(); - Ok(()) - } -} -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()) - } -} - -impl std::fmt::Debug for Weapon<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Weapon"); - ds.field("name", &self.name()); - ds.field("damage", &self.damage()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct WeaponT { - pub name: Option, - pub damage: i16, -} -impl Default for WeaponT { - fn default() -> Self { - Self { - name: None, - damage: 0, - } - } -} -impl WeaponT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let name = self.name.as_ref().map(|x|{ - _fbb.create_string(x) - }); - let damage = self.damage; - Weapon::create(_fbb, &WeaponArgs{ - name, - damage, - }) - } -} -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { - unsafe { flatbuffers::root_unchecked::>(buf) } -} - -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { - unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } -} - -#[inline] -/// Verifies that a buffer of bytes contains a `Monster` -/// and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn root_as_monster(buf: &[u8]) -> Result { - flatbuffers::root::(buf) -} -#[inline] -/// Verifies that a buffer of bytes contains a size prefixed -/// `Monster` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `size_prefixed_root_as_monster_unchecked`. -pub fn size_prefixed_root_as_monster(buf: &[u8]) -> Result { - flatbuffers::size_prefixed_root::(buf) -} -#[inline] -/// Verifies, with the given options, that a buffer of bytes -/// contains a `Monster` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn root_as_monster_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::root_with_opts::>(opts, buf) -} -#[inline] -/// Verifies, with the given verifier options, that a buffer of -/// bytes contains a size prefixed `Monster` and returns -/// it. Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::size_prefixed_root_with_opts::>(opts, buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a Monster and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid `Monster`. -pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster { - flatbuffers::root_unchecked::(buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`. -pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster { - flatbuffers::size_prefixed_root_unchecked::(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 - diff --git a/samples/rust_generated/mod.rs b/samples/rust_generated/mod.rs new file mode 100644 index 000000000..fd399934c --- /dev/null +++ b/samples/rust_generated/mod.rs @@ -0,0 +1,17 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod my_game { + use super::*; + pub mod sample { + use super::*; + mod color_generated; + pub use self::color_generated::*; + mod equipment_generated; + pub use self::equipment_generated::*; + mod vec_3_generated; + pub use self::vec_3_generated::*; + mod monster_generated; + pub use self::monster_generated::*; + mod weapon_generated; + pub use self::weapon_generated::*; + } // sample +} // my_game diff --git a/samples/rust_generated/my_game/sample/color_generated.rs b/samples/rust_generated/my_game/sample/color_generated.rs new file mode 100644 index 000000000..1ef1d31b5 --- /dev/null +++ b/samples/rust_generated/my_game/sample/color_generated.rs @@ -0,0 +1,97 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_COLOR: i8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_COLOR: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_COLOR: [Color; 3] = [ + Color::Red, + Color::Green, + Color::Blue, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct Color(pub i8); +#[allow(non_upper_case_globals)] +impl Color { + pub const Red: Self = Self(0); + pub const Green: Self = Self(1); + pub const Blue: Self = Self(2); + + pub const ENUM_MIN: i8 = 0; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::Red, + Self::Green, + Self::Blue, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::Red => Some("Red"), + Self::Green => Some("Green"), + Self::Blue => Some("Blue"), + _ => None, + } + } +} +impl std::fmt::Debug for Color { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for Color { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for Color { + type Output = Color; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for Color { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for Color { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Color {} diff --git a/samples/rust_generated/my_game/sample/equipment_generated.rs b/samples/rust_generated/my_game/sample/equipment_generated.rs new file mode 100644 index 000000000..919958a35 --- /dev/null +++ b/samples/rust_generated/my_game/sample/equipment_generated.rs @@ -0,0 +1,142 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_EQUIPMENT: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_EQUIPMENT: u8 = 1; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [ + Equipment::NONE, + Equipment::Weapon, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct Equipment(pub u8); +#[allow(non_upper_case_globals)] +impl Equipment { + pub const NONE: Self = Self(0); + pub const Weapon: Self = Self(1); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 1; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::Weapon, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::Weapon => Some("Weapon"), + _ => None, + } + } +} +impl std::fmt::Debug for Equipment { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for Equipment { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for Equipment { + type Output = Equipment; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for Equipment { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for Equipment { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Equipment {} +pub struct EquipmentUnionTableOffset {} + +#[allow(clippy::upper_case_acronyms)] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum EquipmentT { + NONE, + Weapon(Box), +} +impl Default for EquipmentT { + fn default() -> Self { + Self::NONE + } +} +impl EquipmentT { + pub fn equipment_type(&self) -> Equipment { + match self { + Self::NONE => Equipment::NONE, + Self::Weapon(_) => Equipment::Weapon, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::Weapon(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned WeaponT, setting the union to NONE. + pub fn take_weapon(&mut self) -> Option> { + if let Self::Weapon(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::Weapon(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the WeaponT. + pub fn as_weapon(&self) -> Option<&WeaponT> { + if let Self::Weapon(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the WeaponT. + pub fn as_weapon_mut(&mut self) -> Option<&mut WeaponT> { + if let Self::Weapon(v) = self { Some(v.as_mut()) } else { None } + } +} diff --git a/samples/rust_generated/my_game/sample/monster_generated.rs b/samples/rust_generated/my_game/sample/monster_generated.rs new file mode 100644 index 000000000..e12260847 --- /dev/null +++ b/samples/rust_generated/my_game/sample/monster_generated.rs @@ -0,0 +1,442 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum MonsterOffset {} +#[derive(Copy, Clone, 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, loc } } + } +} + +impl<'a> Monster<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Sample.Monster" + } + + #[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 fn unpack(&self) -> MonsterT { + let pos = self.pos().map(|x| { + x.unpack() + }); + let mana = self.mana(); + let hp = self.hp(); + let name = self.name().map(|x| { + x.to_string() + }); + let inventory = self.inventory().map(|x| { + x.to_vec() + }); + let color = self.color(); + let weapons = self.weapons().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let equipped = match self.equipped_type() { + Equipment::NONE => EquipmentT::NONE, + Equipment::Weapon => EquipmentT::Weapon(Box::new( + self.equipped_as_weapon() + .expect("Invalid union table, expected `Equipment::Weapon`.") + .unpack() + )), + _ => EquipmentT::NONE, + }; + let path = self.path().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + MonsterT { + pos, + mana, + hp, + name, + inventory, + color, + weapons, + equipped, + path, + } + } + 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(Weapon::init_from_table) + } else { + None + } + } + +} + +impl flatbuffers::Verifiable for Monster<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("pos", Self::VT_POS, false)? + .visit_field::("mana", Self::VT_MANA, false)? + .visit_field::("hp", Self::VT_HP, false)? + .visit_field::>("name", Self::VT_NAME, false)? + .visit_field::>>("inventory", Self::VT_INVENTORY, false)? + .visit_field::("color", Self::VT_COLOR, false)? + .visit_field::>>>("weapons", Self::VT_WEAPONS, false)? + .visit_union::("equipped_type", Self::VT_EQUIPPED_TYPE, "equipped", Self::VT_EQUIPPED, false, |key, v, pos| { + match key { + Equipment::Weapon => v.verify_union_variant::>("Equipment::Weapon", pos), + _ => Ok(()), + } + })? + .visit_field::>>("path", Self::VT_PATH, false)? + .finish(); + Ok(()) + } +} +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: &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()) + } +} + +impl std::fmt::Debug for Monster<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Monster"); + ds.field("pos", &self.pos()); + ds.field("mana", &self.mana()); + ds.field("hp", &self.hp()); + ds.field("name", &self.name()); + ds.field("inventory", &self.inventory()); + ds.field("color", &self.color()); + ds.field("weapons", &self.weapons()); + ds.field("equipped_type", &self.equipped_type()); + match self.equipped_type() { + Equipment::Weapon => { + if let Some(x) = self.equipped_as_weapon() { + ds.field("equipped", &x) + } else { + ds.field("equipped", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("equipped", &x) + }, + }; + ds.field("path", &self.path()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct MonsterT { + pub pos: Option, + pub mana: i16, + pub hp: i16, + pub name: Option, + pub inventory: Option>, + pub color: Color, + pub weapons: Option>, + pub equipped: EquipmentT, + pub path: Option>, +} +impl Default for MonsterT { + fn default() -> Self { + Self { + pos: None, + mana: 150, + hp: 100, + name: None, + inventory: None, + color: Color::Blue, + weapons: None, + equipped: EquipmentT::NONE, + path: None, + } + } +} +impl MonsterT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let pos_tmp = self.pos.as_ref().map(|x| x.pack()); + let pos = pos_tmp.as_ref(); + let mana = self.mana; + let hp = self.hp; + let name = self.name.as_ref().map(|x|{ + _fbb.create_string(x) + }); + let inventory = self.inventory.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let color = self.color; + let weapons = self.weapons.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) + }); + let equipped_type = self.equipped.equipment_type(); + let equipped = self.equipped.pack(_fbb); + let path = self.path.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) + }); + Monster::create(_fbb, &MonsterArgs{ + pos, + mana, + hp, + name, + inventory, + color, + weapons, + equipped_type, + equipped, + path, + }) + } +} +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + unsafe { flatbuffers::root_unchecked::>(buf) } +} + +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } +} + +#[inline] +/// Verifies that a buffer of bytes contains a `Monster` +/// and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn root_as_monster(buf: &[u8]) -> Result { + flatbuffers::root::(buf) +} +#[inline] +/// Verifies that a buffer of bytes contains a size prefixed +/// `Monster` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `size_prefixed_root_as_monster_unchecked`. +pub fn size_prefixed_root_as_monster(buf: &[u8]) -> Result { + flatbuffers::size_prefixed_root::(buf) +} +#[inline] +/// Verifies, with the given options, that a buffer of bytes +/// contains a `Monster` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn root_as_monster_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::root_with_opts::>(opts, buf) +} +#[inline] +/// Verifies, with the given verifier options, that a buffer of +/// bytes contains a size prefixed `Monster` and returns +/// it. Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::size_prefixed_root_with_opts::>(opts, buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a Monster and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid `Monster`. +pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster { + flatbuffers::root_unchecked::(buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`. +pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster { + flatbuffers::size_prefixed_root_unchecked::(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); +} diff --git a/samples/rust_generated/my_game/sample/vec_3_generated.rs b/samples/rust_generated/my_game/sample/vec_3_generated.rs new file mode 100644 index 000000000..d7d8d26a0 --- /dev/null +++ b/samples/rust_generated/my_game/sample/vec_3_generated.rs @@ -0,0 +1,184 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Vec3, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Vec3(pub [u8; 12]); +impl Default for Vec3 { + fn default() -> Self { + Self([0; 12]) + } +} +impl std::fmt::Debug for Vec3 { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Vec3") + .field("x", &self.x()) + .field("y", &self.y()) + .field("z", &self.z()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for 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<'a> flatbuffers::Verifiable for Vec3 { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Vec3 { + #[allow(clippy::too_many_arguments)] + pub fn new( + x: f32, + y: f32, + z: f32, + ) -> Self { + let mut s = Self([0; 12]); + s.set_x(x); + s.set_y(y); + s.set_z(z); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Sample.Vec3" + } + + pub fn x(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_x(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn y(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_y(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn z(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[8..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_z(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[8..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> Vec3T { + Vec3T { + x: self.x(), + y: self.y(), + z: self.z(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct Vec3T { + pub x: f32, + pub y: f32, + pub z: f32, +} +impl Vec3T { + pub fn pack(&self) -> Vec3 { + Vec3::new( + self.x, + self.y, + self.z, + ) + } +} + diff --git a/samples/rust_generated/my_game/sample/weapon_generated.rs b/samples/rust_generated/my_game/sample/weapon_generated.rs new file mode 100644 index 000000000..4c5792fe8 --- /dev/null +++ b/samples/rust_generated/my_game/sample/weapon_generated.rs @@ -0,0 +1,154 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum WeaponOffset {} +#[derive(Copy, Clone, 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, loc } } + } +} + +impl<'a> Weapon<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Sample.Weapon" + } + + #[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 fn unpack(&self) -> WeaponT { + let name = self.name().map(|x| { + x.to_string() + }); + let damage = self.damage(); + WeaponT { + name, + damage, + } + } + 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() + } +} + +impl flatbuffers::Verifiable for Weapon<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("name", Self::VT_NAME, false)? + .visit_field::("damage", Self::VT_DAMAGE, false)? + .finish(); + Ok(()) + } +} +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()) + } +} + +impl std::fmt::Debug for Weapon<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Weapon"); + ds.field("name", &self.name()); + ds.field("damage", &self.damage()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct WeaponT { + pub name: Option, + pub damage: i16, +} +impl Default for WeaponT { + fn default() -> Self { + Self { + name: None, + damage: 0, + } + } +} +impl WeaponT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let name = self.name.as_ref().map(|x|{ + _fbb.create_string(x) + }); + let damage = self.damage; + Weapon::create(_fbb, &WeaponArgs{ + name, + damage, + }) + } +} diff --git a/samples/sample_binary.rs b/samples/sample_binary.rs index b4abf40c7..2f010b8d3 100644 --- a/samples/sample_binary.rs +++ b/samples/sample_binary.rs @@ -19,10 +19,9 @@ extern crate flatbuffers; // import the generated code #[allow(dead_code, unused_imports)] -#[path = "./monster_generated.rs"] #[allow(clippy::approx_constant)] // We use low precision PI as a default value. -mod monster_generated; -pub use monster_generated::my_game::sample::{Color, Equipment, +mod rust_generated; +pub use rust_generated::my_game::sample::{Color, Equipment, Monster, MonsterArgs, Vec3, Weapon, WeaponArgs}; diff --git a/samples/sample_flexbuffers.rs b/samples/sample_flexbuffers.rs index 237dbf0b0..074a20017 100644 --- a/samples/sample_flexbuffers.rs +++ b/samples/sample_flexbuffers.rs @@ -16,7 +16,6 @@ extern crate flexbuffers; use flexbuffers::{BitWidth, Builder, Reader, ReaderError}; - // In this Example we're creating a monster that corresponds to the following JSON: // { // "coins": [5, 10, 25, 25, 25, 100], diff --git a/src/flatc.cpp b/src/flatc.cpp index 9afca6881..569d41c2e 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -577,6 +577,9 @@ int FlatCompiler::Compile(int argc, const char **argv) { // in any files coming up next. parser->MarkGenerated(); } + if (opts.lang_to_generate & IDLOptions::kRust && !parser->opts.one_file) { + GenerateRustModuleRootFile(*parser, output_path); + } return 0; } diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 09c3e447c..83973cf4f 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -210,6 +210,63 @@ bool IsOptionalToBuilder(const FieldDef &field) { return field.IsOptional() || !IsScalar(field.value.type.base_type); } +bool GenerateRustModuleRootFile(const Parser &parser, + const std::string &output_dir) { + // We gather the symbols into a tree of namespaces (which are rust mods) and + // generate a file that gathers them all. + struct Module { + std::map sub_modules; + std::vector generated_files; + // Add a symbol into the tree. + void Insert(const Definition *s, const std::string suffix) { + const Definition &symbol = *s; + Module *current_module = this; + for (auto it = symbol.defined_namespace->components.begin(); + it != symbol.defined_namespace->components.end(); it++) { + std::string ns_component = MakeSnakeCase(*it); + current_module = ¤t_module->sub_modules[ns_component]; + } + current_module->generated_files.push_back(MakeSnakeCase(symbol.name) + + suffix); + } + // Recursively create the importer file. + void GenerateImports(CodeWriter &code) { + for (auto it = sub_modules.begin(); it != sub_modules.end(); it++) { + code += "pub mod " + it->first + " {"; + code.IncrementIdentLevel(); + code += "use super::*;"; + it->second.GenerateImports(code); + code.DecrementIdentLevel(); + code += "} // " + it->first; + } + for (auto it = generated_files.begin(); it != generated_files.end(); + it++) { + code += "mod " + *it + ";"; + code += "pub use self::" + *it + "::*;"; + } + } + }; + Module root_module; + for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); + it++) { + root_module.Insert(*it, parser.opts.filename_suffix); + } + for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); + it++) { + root_module.Insert(*it, parser.opts.filename_suffix); + } + CodeWriter code(" "); + // TODO(caspern): Move generated warning out of BaseGenerator. + code += + "// Automatically generated by the Flatbuffers compiler. " + "Do not modify."; + root_module.GenerateImports(code); + const bool success = + SaveFile((output_dir + "mod.rs").c_str(), code.ToString(), false); + code.Clear(); + return success; +} + namespace rust { class RustGenerator : public BaseGenerator { @@ -320,13 +377,79 @@ class RustGenerator : public BaseGenerator { "ENUM_MAX", "ENUM_MIN", "ENUM_VALUES", + // clang-format on }; for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw); } + bool generate() { + if (parser_.opts.one_file) { + return GenerateOneFile(); + } else { + return GenerateIndividualFiles(); + } + } + + template + bool GenerateSymbols(const SymbolTable &symbols, + std::function gen_symbol) { + for (auto it = symbols.vec.begin(); it != symbols.vec.end(); it++) { + const T &symbol = **it; + if (symbol.generated) continue; + code_.Clear(); + code_ += "// " + std::string(FlatBuffersGeneratedWarning()); + code_ += "extern crate flatbuffers;"; + code_ += "use std::mem;"; + code_ += "use std::cmp::Ordering;"; + code_ += "use self::flatbuffers::{EndianScalar, Follow};"; + code_ += "use super::*;"; + cur_name_space_ = symbol.defined_namespace; + gen_symbol(symbol); + std::stringstream file_path; + file_path << path_; + // DO NOT SUBMIT: CASPER: Refactor out common path name generation. + if (symbol.defined_namespace) + for (auto i = symbol.defined_namespace->components.begin(); + i != symbol.defined_namespace->components.end(); i++) { + file_path << MakeSnakeCase(*i) << kPathSeparator; + EnsureDirExists(file_path.str()); + } + file_path << MakeSnakeCase(symbol.name) << parser_.opts.filename_suffix + << ".rs"; + const bool save_success = + SaveFile(file_path.str().c_str(), code_.ToString(), + /*binary=*/false); + if (!save_success) return false; + } + return true; + } + + bool GenerateIndividualFiles() { + code_.Clear(); + // Don't bother with imports. Use absolute paths everywhere. + return GenerateSymbols( + parser_.enums_, [&](const EnumDef &e) { this->GenEnum(e); }) && + GenerateSymbols( + parser_.structs_, [&](const StructDef &s) { + if (s.fixed) { + this->GenStruct(s); + } else { + this->GenTable(s); + if (this->parser_.opts.generate_object_based_api) { + this->GenTableObject(s); + } + } + if (this->parser_.root_struct_def_ == &s) { + this->GenRootTableFuncs(s); + } + }); + } + + // Generates code organized by .fbs files. This is broken legacy behavior + // that does not work with multiple fbs files with shared namespaces. // Iterate through all definitions we haven't generated code for (enums, // structs, and tables) and output them to a single file. - bool generate() { + bool GenerateOneFile() { code_.Clear(); code_ += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n"; @@ -496,24 +619,17 @@ class RustGenerator : public BaseGenerator { // example: f(A, D::E) -> super::D::E // does not include leaf object (typically a struct type). - size_t i = 0; std::stringstream stream; - - auto s = src->components.begin(); - auto d = dst->components.begin(); - for (;;) { - if (s == src->components.end()) { break; } - if (d == dst->components.end()) { break; } - if (*s != *d) { break; } - ++s; - ++d; - ++i; - } - - for (; s != src->components.end(); ++s) { stream << "super::"; } - for (; d != dst->components.end(); ++d) { - stream << MakeSnakeCase(*d) + "::"; - } + size_t common = 0; + std::vector s, d; + if (src) s = src->components; + if (dst) d = dst->components; + while (common < s.size() && common < d.size() && s[common] == d[common]) + common++; + // If src namespace is empty, this must be an absolute path. + for (size_t i = common; i < s.size(); i++) stream << "super::"; + for (size_t i = common; i < d.size(); i++) + stream << MakeSnakeCase(d[i]) + "::"; return stream.str(); } @@ -822,6 +938,8 @@ class RustGenerator : public BaseGenerator { code_.SetValue("NATIVE_NAME", NativeName(enum_def)); // Generate native union. + code_ += "#[allow(clippy::upper_case_acronyms)]"; // NONE's spelling is + // intended. code_ += "#[non_exhaustive]"; code_ += "#[derive(Debug, Clone, PartialEq)]"; code_ += "pub enum {{NATIVE_NAME}} {"; @@ -1850,7 +1968,7 @@ class RustGenerator : public BaseGenerator { // All types besides unions. code_.SetValue("TY", FollowType(field.value.type, "'_")); code_ += - "\n .visit_field::<{{TY}}>(&\"{{FIELD_NAME}}\", " + "\n .visit_field::<{{TY}}>(\"{{FIELD_NAME}}\", " "Self::{{OFFSET_NAME}}, {{IS_REQ}})?\\"; return; } @@ -1859,8 +1977,8 @@ class RustGenerator : public BaseGenerator { code_.SetValue("UNION_TYPE", WrapInNameSpace(union_def)); code_ += "\n .visit_union::<{{UNION_TYPE}}, _>(" - "&\"{{FIELD_NAME}}_type\", Self::{{OFFSET_NAME}}_TYPE, " - "&\"{{FIELD_NAME}}\", Self::{{OFFSET_NAME}}, {{IS_REQ}}, " + "\"{{FIELD_NAME}}_type\", Self::{{OFFSET_NAME}}_TYPE, " + "\"{{FIELD_NAME}}\", Self::{{OFFSET_NAME}}, {{IS_REQ}}, " "|key, v, pos| {"; code_ += " match key {"; ForAllUnionVariantsBesidesNone(union_def, [&](const EnumVal &unused) { @@ -2187,6 +2305,7 @@ class RustGenerator : public BaseGenerator { FLATBUFFERS_ASSERT(field.key); code_.SetValue("KEY_TYPE", GenTableAccessorFuncReturnType(field, "")); + code_.SetValue("REF", IsString(field.value.type) ? "" : "&"); code_ += " #[inline]"; code_ += @@ -2200,7 +2319,7 @@ class RustGenerator : public BaseGenerator { " pub fn key_compare_with_value(&self, val: {{KEY_TYPE}}) -> " " ::std::cmp::Ordering {"; code_ += " let key = self.{{FIELD_NAME}}();"; - code_ += " key.cmp(&val)"; + code_ += " key.cmp({{REF}}val)"; code_ += " }"; } @@ -2543,7 +2662,7 @@ class RustGenerator : public BaseGenerator { code_ += " let mut s = Self([0; {{STRUCT_SIZE}}]);"; ForAllStructFields(struct_def, [&](const FieldDef &unused) { (void)unused; - code_ += " s.set_{{FIELD_NAME}}({{REF}}{{FIELD_NAME}});"; + code_ += " s.set_{{FIELD_NAME}}({{FIELD_NAME}});"; }); code_ += " s"; code_ += " }"; diff --git a/tests/arrays_test/mod.rs b/tests/arrays_test/mod.rs new file mode 100644 index 000000000..581ba7b96 --- /dev/null +++ b/tests/arrays_test/mod.rs @@ -0,0 +1,15 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod my_game { + use super::*; + pub mod example { + use super::*; + mod test_enum_generated; + pub use self::test_enum_generated::*; + mod nested_struct_generated; + pub use self::nested_struct_generated::*; + mod array_struct_generated; + pub use self::array_struct_generated::*; + mod array_table_generated; + pub use self::array_table_generated::*; + } // example +} // my_game diff --git a/tests/arrays_test/my_game/example/array_struct_generated.rs b/tests/arrays_test/my_game/example/array_struct_generated.rs new file mode 100644 index 000000000..be9cbef32 --- /dev/null +++ b/tests/arrays_test/my_game/example/array_struct_generated.rs @@ -0,0 +1,232 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct ArrayStruct, aligned to 8 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct ArrayStruct(pub [u8; 160]); +impl Default for ArrayStruct { + fn default() -> Self { + Self([0; 160]) + } +} +impl std::fmt::Debug for ArrayStruct { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("ArrayStruct") + .field("a", &self.a()) + .field("b", &self.b()) + .field("c", &self.c()) + .field("d", &self.d()) + .field("e", &self.e()) + .field("f", &self.f()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for ArrayStruct {} +impl flatbuffers::SafeSliceAccess for ArrayStruct {} +impl<'a> flatbuffers::Follow<'a> for ArrayStruct { + type Inner = &'a ArrayStruct; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a ArrayStruct>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a ArrayStruct { + type Inner = &'a ArrayStruct; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for ArrayStruct { + type Output = ArrayStruct; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const ArrayStruct as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b ArrayStruct { + type Output = ArrayStruct; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const ArrayStruct as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for ArrayStruct { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> ArrayStruct { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: f32, + b: &[i32; 15], + c: i8, + d: &[NestedStruct; 2], + e: i32, + f: &[i64; 2], + ) -> Self { + let mut s = Self([0; 160]); + s.set_a(a); + s.set_b(b); + s.set_c(c); + s.set_d(d); + s.set_e(e); + s.set_f(f); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.ArrayStruct" + } + + pub fn a(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn b(&'a self) -> flatbuffers::Array<'a, i32, 15> { + flatbuffers::Array::follow(&self.0, 4) + } + + pub fn set_b(&mut self, items: &[i32; 15]) { + flatbuffers::emplace_scalar_array(&mut self.0, 4, items); + } + + pub fn c(&self) -> i8 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[64..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_c(&mut self, x: i8) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i8 as *const u8, + self.0[64..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn d(&'a self) -> flatbuffers::Array<'a, NestedStruct, 2> { + flatbuffers::Array::follow(&self.0, 72) + } + + pub fn set_d(&mut self, x: &[NestedStruct; 2]) { + unsafe { + std::ptr::copy( + x.as_ptr() as *const u8, + self.0.as_mut_ptr().add(72), + 64, + ); + } + } + + pub fn e(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[136..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_e(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[136..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn f(&'a self) -> flatbuffers::Array<'a, i64, 2> { + flatbuffers::Array::follow(&self.0, 144) + } + + pub fn set_f(&mut self, items: &[i64; 2]) { + flatbuffers::emplace_scalar_array(&mut self.0, 144, items); + } + + pub fn unpack(&self) -> ArrayStructT { + ArrayStructT { + a: self.a(), + b: self.b().into(), + c: self.c(), + d: { let d = self.d(); flatbuffers::array_init(|i| d.get(i).unpack()) }, + e: self.e(), + f: self.f().into(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct ArrayStructT { + pub a: f32, + pub b: [i32; 15], + pub c: i8, + pub d: [NestedStructT; 2], + pub e: i32, + pub f: [i64; 2], +} +impl ArrayStructT { + pub fn pack(&self) -> ArrayStruct { + ArrayStruct::new( + self.a, + &self.b, + self.c, + &flatbuffers::array_init(|i| self.d[i].pack()), + self.e, + &self.f, + ) + } +} + diff --git a/tests/arrays_test/my_game/example/array_table_generated.rs b/tests/arrays_test/my_game/example/array_table_generated.rs new file mode 100644 index 000000000..a5fd1987f --- /dev/null +++ b/tests/arrays_test/my_game/example/array_table_generated.rs @@ -0,0 +1,230 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum ArrayTableOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct ArrayTable<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for ArrayTable<'a> { + type Inner = ArrayTable<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> ArrayTable<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.ArrayTable" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + ArrayTable { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args ArrayTableArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = ArrayTableBuilder::new(_fbb); + if let Some(x) = args.a { builder.add_a(x); } + builder.finish() + } + + pub fn unpack(&self) -> ArrayTableT { + let a = self.a().map(|x| { + x.unpack() + }); + ArrayTableT { + a, + } + } + pub const VT_A: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn a(&self) -> Option<&'a ArrayStruct> { + self._tab.get::(ArrayTable::VT_A, None) + } +} + +impl flatbuffers::Verifiable for ArrayTable<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("a", Self::VT_A, false)? + .finish(); + Ok(()) + } +} +pub struct ArrayTableArgs<'a> { + pub a: Option<&'a ArrayStruct>, +} +impl<'a> Default for ArrayTableArgs<'a> { + #[inline] + fn default() -> Self { + ArrayTableArgs { + a: None, + } + } +} +pub struct ArrayTableBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> ArrayTableBuilder<'a, 'b> { + #[inline] + pub fn add_a(&mut self, a: &ArrayStruct) { + self.fbb_.push_slot_always::<&ArrayStruct>(ArrayTable::VT_A, a); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ArrayTableBuilder<'a, 'b> { + let start = _fbb.start_table(); + ArrayTableBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for ArrayTable<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("ArrayTable"); + ds.field("a", &self.a()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct ArrayTableT { + pub a: Option, +} +impl Default for ArrayTableT { + fn default() -> Self { + Self { + a: None, + } + } +} +impl ArrayTableT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let a_tmp = self.a.as_ref().map(|x| x.pack()); + let a = a_tmp.as_ref(); + ArrayTable::create(_fbb, &ArrayTableArgs{ + a, + }) + } +} +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_root_as_array_table<'a>(buf: &'a [u8]) -> ArrayTable<'a> { + unsafe { flatbuffers::root_unchecked::>(buf) } +} + +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_size_prefixed_root_as_array_table<'a>(buf: &'a [u8]) -> ArrayTable<'a> { + unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } +} + +#[inline] +/// Verifies that a buffer of bytes contains a `ArrayTable` +/// and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_array_table_unchecked`. +pub fn root_as_array_table(buf: &[u8]) -> Result { + flatbuffers::root::(buf) +} +#[inline] +/// Verifies that a buffer of bytes contains a size prefixed +/// `ArrayTable` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `size_prefixed_root_as_array_table_unchecked`. +pub fn size_prefixed_root_as_array_table(buf: &[u8]) -> Result { + flatbuffers::size_prefixed_root::(buf) +} +#[inline] +/// Verifies, with the given options, that a buffer of bytes +/// contains a `ArrayTable` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_array_table_unchecked`. +pub fn root_as_array_table_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::root_with_opts::>(opts, buf) +} +#[inline] +/// Verifies, with the given verifier options, that a buffer of +/// bytes contains a size prefixed `ArrayTable` and returns +/// it. Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_array_table_unchecked`. +pub fn size_prefixed_root_as_array_table_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::size_prefixed_root_with_opts::>(opts, buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a ArrayTable and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid `ArrayTable`. +pub unsafe fn root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable { + flatbuffers::root_unchecked::(buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a size prefixed ArrayTable and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid size prefixed `ArrayTable`. +pub unsafe fn size_prefixed_root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable { + flatbuffers::size_prefixed_root_unchecked::(buf) +} +pub const ARRAY_TABLE_IDENTIFIER: &str = "ARRT"; + +#[inline] +pub fn array_table_buffer_has_identifier(buf: &[u8]) -> bool { + flatbuffers::buffer_has_identifier(buf, ARRAY_TABLE_IDENTIFIER, false) +} + +#[inline] +pub fn array_table_size_prefixed_buffer_has_identifier(buf: &[u8]) -> bool { + flatbuffers::buffer_has_identifier(buf, ARRAY_TABLE_IDENTIFIER, true) +} + +pub const ARRAY_TABLE_EXTENSION: &str = "mon"; + +#[inline] +pub fn finish_array_table_buffer<'a, 'b>( + fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, + root: flatbuffers::WIPOffset>) { + fbb.finish(root, Some(ARRAY_TABLE_IDENTIFIER)); +} + +#[inline] +pub fn finish_size_prefixed_array_table_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { + fbb.finish_size_prefixed(root, Some(ARRAY_TABLE_IDENTIFIER)); +} diff --git a/tests/arrays_test/my_game/example/nested_struct_generated.rs b/tests/arrays_test/my_game/example/nested_struct_generated.rs new file mode 100644 index 000000000..382477841 --- /dev/null +++ b/tests/arrays_test/my_game/example/nested_struct_generated.rs @@ -0,0 +1,174 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct NestedStruct, aligned to 8 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct NestedStruct(pub [u8; 32]); +impl Default for NestedStruct { + fn default() -> Self { + Self([0; 32]) + } +} +impl std::fmt::Debug for NestedStruct { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("NestedStruct") + .field("a", &self.a()) + .field("b", &self.b()) + .field("c", &self.c()) + .field("d", &self.d()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for NestedStruct {} +impl flatbuffers::SafeSliceAccess for NestedStruct {} +impl<'a> flatbuffers::Follow<'a> for NestedStruct { + type Inner = &'a NestedStruct; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a NestedStruct>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a NestedStruct { + type Inner = &'a NestedStruct; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for NestedStruct { + type Output = NestedStruct; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const NestedStruct as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b NestedStruct { + type Output = NestedStruct; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const NestedStruct as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for NestedStruct { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> NestedStruct { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: &[i32; 2], + b: TestEnum, + c: &[TestEnum; 2], + d: &[i64; 2], + ) -> Self { + let mut s = Self([0; 32]); + s.set_a(a); + s.set_b(b); + s.set_c(c); + s.set_d(d); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.NestedStruct" + } + + pub fn a(&'a self) -> flatbuffers::Array<'a, i32, 2> { + flatbuffers::Array::follow(&self.0, 0) + } + + pub fn set_a(&mut self, items: &[i32; 2]) { + flatbuffers::emplace_scalar_array(&mut self.0, 0, items); + } + + pub fn b(&self) -> TestEnum { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[8..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_b(&mut self, x: TestEnum) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const TestEnum as *const u8, + self.0[8..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn c(&'a self) -> flatbuffers::Array<'a, TestEnum, 2> { + flatbuffers::Array::follow(&self.0, 9) + } + + pub fn set_c(&mut self, x: &[TestEnum; 2]) { + unsafe { + std::ptr::copy( + x.as_ptr() as *const u8, + self.0.as_mut_ptr().add(9), + 2, + ); + } + } + + pub fn d(&'a self) -> flatbuffers::Array<'a, i64, 2> { + flatbuffers::Array::follow(&self.0, 16) + } + + pub fn set_d(&mut self, items: &[i64; 2]) { + flatbuffers::emplace_scalar_array(&mut self.0, 16, items); + } + + pub fn unpack(&self) -> NestedStructT { + NestedStructT { + a: self.a().into(), + b: self.b(), + c: self.c().into(), + d: self.d().into(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct NestedStructT { + pub a: [i32; 2], + pub b: TestEnum, + pub c: [TestEnum; 2], + pub d: [i64; 2], +} +impl NestedStructT { + pub fn pack(&self) -> NestedStruct { + NestedStruct::new( + &self.a, + self.b, + &self.c, + &self.d, + ) + } +} + diff --git a/tests/arrays_test/my_game/example/test_enum_generated.rs b/tests/arrays_test/my_game/example/test_enum_generated.rs new file mode 100644 index 000000000..6dcc1cb97 --- /dev/null +++ b/tests/arrays_test/my_game/example/test_enum_generated.rs @@ -0,0 +1,97 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_TEST_ENUM: i8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_TEST_ENUM: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_TEST_ENUM: [TestEnum; 3] = [ + TestEnum::A, + TestEnum::B, + TestEnum::C, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct TestEnum(pub i8); +#[allow(non_upper_case_globals)] +impl TestEnum { + pub const A: Self = Self(0); + pub const B: Self = Self(1); + pub const C: Self = Self(2); + + pub const ENUM_MIN: i8 = 0; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::A, + Self::B, + Self::C, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::A => Some("A"), + Self::B => Some("B"), + Self::C => Some("C"), + _ => None, + } + } +} +impl std::fmt::Debug for TestEnum { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for TestEnum { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for TestEnum { + type Output = TestEnum; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for TestEnum { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for TestEnum { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for TestEnum {} diff --git a/tests/arrays_test_generated.rs b/tests/arrays_test_generated.rs deleted file mode 100644 index e8c61615c..000000000 --- a/tests/arrays_test_generated.rs +++ /dev/null @@ -1,727 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod my_game { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; -#[allow(unused_imports, dead_code)] -pub mod example { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_TEST_ENUM: i8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_TEST_ENUM: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_TEST_ENUM: [TestEnum; 3] = [ - TestEnum::A, - TestEnum::B, - TestEnum::C, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct TestEnum(pub i8); -#[allow(non_upper_case_globals)] -impl TestEnum { - pub const A: Self = Self(0); - pub const B: Self = Self(1); - pub const C: Self = Self(2); - - pub const ENUM_MIN: i8 = 0; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::A, - Self::B, - Self::C, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::A => Some("A"), - Self::B => Some("B"), - Self::C => Some("C"), - _ => None, - } - } -} -impl std::fmt::Debug for TestEnum { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for TestEnum { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for TestEnum { - type Output = TestEnum; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for TestEnum { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for TestEnum { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for TestEnum {} -// struct NestedStruct, aligned to 8 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct NestedStruct(pub [u8; 32]); -impl Default for NestedStruct { - fn default() -> Self { - Self([0; 32]) - } -} -impl std::fmt::Debug for NestedStruct { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("NestedStruct") - .field("a", &self.a()) - .field("b", &self.b()) - .field("c", &self.c()) - .field("d", &self.d()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for NestedStruct {} -impl flatbuffers::SafeSliceAccess for NestedStruct {} -impl<'a> flatbuffers::Follow<'a> for NestedStruct { - type Inner = &'a NestedStruct; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a NestedStruct>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a NestedStruct { - type Inner = &'a NestedStruct; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for NestedStruct { - type Output = NestedStruct; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const NestedStruct as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b NestedStruct { - type Output = NestedStruct; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const NestedStruct as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for NestedStruct { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> NestedStruct { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: &[i32; 2], - b: TestEnum, - c: &[TestEnum; 2], - d: &[i64; 2], - ) -> Self { - let mut s = Self([0; 32]); - s.set_a(&a); - s.set_b(b); - s.set_c(&c); - s.set_d(&d); - s - } - - pub fn a(&'a self) -> flatbuffers::Array<'a, i32, 2> { - flatbuffers::Array::follow(&self.0, 0) - } - - pub fn set_a(&mut self, items: &[i32; 2]) { - flatbuffers::emplace_scalar_array(&mut self.0, 0, items); - } - - pub fn b(&self) -> TestEnum { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[8..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_b(&mut self, x: TestEnum) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const TestEnum as *const u8, - self.0[8..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn c(&'a self) -> flatbuffers::Array<'a, TestEnum, 2> { - flatbuffers::Array::follow(&self.0, 9) - } - - pub fn set_c(&mut self, x: &[TestEnum; 2]) { - unsafe { - std::ptr::copy( - x.as_ptr() as *const u8, - self.0.as_mut_ptr().add(9), - 2, - ); - } - } - - pub fn d(&'a self) -> flatbuffers::Array<'a, i64, 2> { - flatbuffers::Array::follow(&self.0, 16) - } - - pub fn set_d(&mut self, items: &[i64; 2]) { - flatbuffers::emplace_scalar_array(&mut self.0, 16, items); - } - - pub fn unpack(&self) -> NestedStructT { - NestedStructT { - a: self.a().into(), - b: self.b(), - c: self.c().into(), - d: self.d().into(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct NestedStructT { - pub a: [i32; 2], - pub b: TestEnum, - pub c: [TestEnum; 2], - pub d: [i64; 2], -} -impl NestedStructT { - pub fn pack(&self) -> NestedStruct { - NestedStruct::new( - &self.a, - self.b, - &self.c, - &self.d, - ) - } -} - -// struct ArrayStruct, aligned to 8 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct ArrayStruct(pub [u8; 160]); -impl Default for ArrayStruct { - fn default() -> Self { - Self([0; 160]) - } -} -impl std::fmt::Debug for ArrayStruct { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("ArrayStruct") - .field("a", &self.a()) - .field("b", &self.b()) - .field("c", &self.c()) - .field("d", &self.d()) - .field("e", &self.e()) - .field("f", &self.f()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for ArrayStruct {} -impl flatbuffers::SafeSliceAccess for ArrayStruct {} -impl<'a> flatbuffers::Follow<'a> for ArrayStruct { - type Inner = &'a ArrayStruct; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a ArrayStruct>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a ArrayStruct { - type Inner = &'a ArrayStruct; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for ArrayStruct { - type Output = ArrayStruct; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const ArrayStruct as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b ArrayStruct { - type Output = ArrayStruct; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const ArrayStruct as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for ArrayStruct { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> ArrayStruct { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: f32, - b: &[i32; 15], - c: i8, - d: &[NestedStruct; 2], - e: i32, - f: &[i64; 2], - ) -> Self { - let mut s = Self([0; 160]); - s.set_a(a); - s.set_b(&b); - s.set_c(c); - s.set_d(&d); - s.set_e(e); - s.set_f(&f); - s - } - - pub fn a(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_a(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn b(&'a self) -> flatbuffers::Array<'a, i32, 15> { - flatbuffers::Array::follow(&self.0, 4) - } - - pub fn set_b(&mut self, items: &[i32; 15]) { - flatbuffers::emplace_scalar_array(&mut self.0, 4, items); - } - - pub fn c(&self) -> i8 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[64..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_c(&mut self, x: i8) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i8 as *const u8, - self.0[64..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn d(&'a self) -> flatbuffers::Array<'a, NestedStruct, 2> { - flatbuffers::Array::follow(&self.0, 72) - } - - pub fn set_d(&mut self, x: &[NestedStruct; 2]) { - unsafe { - std::ptr::copy( - x.as_ptr() as *const u8, - self.0.as_mut_ptr().add(72), - 64, - ); - } - } - - pub fn e(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[136..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_e(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[136..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn f(&'a self) -> flatbuffers::Array<'a, i64, 2> { - flatbuffers::Array::follow(&self.0, 144) - } - - pub fn set_f(&mut self, items: &[i64; 2]) { - flatbuffers::emplace_scalar_array(&mut self.0, 144, items); - } - - pub fn unpack(&self) -> ArrayStructT { - ArrayStructT { - a: self.a(), - b: self.b().into(), - c: self.c(), - d: { let d = self.d(); flatbuffers::array_init(|i| d.get(i).unpack()) }, - e: self.e(), - f: self.f().into(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct ArrayStructT { - pub a: f32, - pub b: [i32; 15], - pub c: i8, - pub d: [NestedStructT; 2], - pub e: i32, - pub f: [i64; 2], -} -impl ArrayStructT { - pub fn pack(&self) -> ArrayStruct { - ArrayStruct::new( - self.a, - &self.b, - self.c, - &flatbuffers::array_init(|i| self.d[i].pack()), - self.e, - &self.f, - ) - } -} - -pub enum ArrayTableOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct ArrayTable<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for ArrayTable<'a> { - type Inner = ArrayTable<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> ArrayTable<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - ArrayTable { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args ArrayTableArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = ArrayTableBuilder::new(_fbb); - if let Some(x) = args.a { builder.add_a(x); } - builder.finish() - } - - pub fn unpack(&self) -> ArrayTableT { - let a = self.a().map(|x| { - x.unpack() - }); - ArrayTableT { - a, - } - } - pub const VT_A: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn a(&self) -> Option<&'a ArrayStruct> { - self._tab.get::(ArrayTable::VT_A, None) - } -} - -impl flatbuffers::Verifiable for ArrayTable<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"a", Self::VT_A, false)? - .finish(); - Ok(()) - } -} -pub struct ArrayTableArgs<'a> { - pub a: Option<&'a ArrayStruct>, -} -impl<'a> Default for ArrayTableArgs<'a> { - #[inline] - fn default() -> Self { - ArrayTableArgs { - a: None, - } - } -} -pub struct ArrayTableBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> ArrayTableBuilder<'a, 'b> { - #[inline] - pub fn add_a(&mut self, a: &ArrayStruct) { - self.fbb_.push_slot_always::<&ArrayStruct>(ArrayTable::VT_A, a); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ArrayTableBuilder<'a, 'b> { - let start = _fbb.start_table(); - ArrayTableBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for ArrayTable<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("ArrayTable"); - ds.field("a", &self.a()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct ArrayTableT { - pub a: Option, -} -impl Default for ArrayTableT { - fn default() -> Self { - Self { - a: None, - } - } -} -impl ArrayTableT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let a_tmp = self.a.as_ref().map(|x| x.pack()); - let a = a_tmp.as_ref(); - ArrayTable::create(_fbb, &ArrayTableArgs{ - a, - }) - } -} -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_root_as_array_table<'a>(buf: &'a [u8]) -> ArrayTable<'a> { - unsafe { flatbuffers::root_unchecked::>(buf) } -} - -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_size_prefixed_root_as_array_table<'a>(buf: &'a [u8]) -> ArrayTable<'a> { - unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } -} - -#[inline] -/// Verifies that a buffer of bytes contains a `ArrayTable` -/// and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_array_table_unchecked`. -pub fn root_as_array_table(buf: &[u8]) -> Result { - flatbuffers::root::(buf) -} -#[inline] -/// Verifies that a buffer of bytes contains a size prefixed -/// `ArrayTable` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `size_prefixed_root_as_array_table_unchecked`. -pub fn size_prefixed_root_as_array_table(buf: &[u8]) -> Result { - flatbuffers::size_prefixed_root::(buf) -} -#[inline] -/// Verifies, with the given options, that a buffer of bytes -/// contains a `ArrayTable` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_array_table_unchecked`. -pub fn root_as_array_table_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::root_with_opts::>(opts, buf) -} -#[inline] -/// Verifies, with the given verifier options, that a buffer of -/// bytes contains a size prefixed `ArrayTable` and returns -/// it. Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_array_table_unchecked`. -pub fn size_prefixed_root_as_array_table_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::size_prefixed_root_with_opts::>(opts, buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a ArrayTable and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid `ArrayTable`. -pub unsafe fn root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable { - flatbuffers::root_unchecked::(buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a size prefixed ArrayTable and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid size prefixed `ArrayTable`. -pub unsafe fn size_prefixed_root_as_array_table_unchecked(buf: &[u8]) -> ArrayTable { - flatbuffers::size_prefixed_root_unchecked::(buf) -} -pub const ARRAY_TABLE_IDENTIFIER: &str = "ARRT"; - -#[inline] -pub fn array_table_buffer_has_identifier(buf: &[u8]) -> bool { - flatbuffers::buffer_has_identifier(buf, ARRAY_TABLE_IDENTIFIER, false) -} - -#[inline] -pub fn array_table_size_prefixed_buffer_has_identifier(buf: &[u8]) -> bool { - flatbuffers::buffer_has_identifier(buf, ARRAY_TABLE_IDENTIFIER, true) -} - -pub const ARRAY_TABLE_EXTENSION: &str = "mon"; - -#[inline] -pub fn finish_array_table_buffer<'a, 'b>( - fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, - root: flatbuffers::WIPOffset>) { - fbb.finish(root, Some(ARRAY_TABLE_IDENTIFIER)); -} - -#[inline] -pub fn finish_size_prefixed_array_table_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { - fbb.finish_size_prefixed(root, Some(ARRAY_TABLE_IDENTIFIER)); -} -} // pub mod Example -} // pub mod MyGame - diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 8458b8c5c..49301bf8d 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -31,12 +31,12 @@ set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CP set TEST_CS_FLAGS=--cs-gen-json-serializer set TEST_TS_FLAGS=--gen-name-strings set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api -set TEST_RUST_FLAGS=%TEST_BASE_FLAGS% --gen-name-strings +set TEST_RUST_FLAGS=%TEST_BASE_FLAGS% --gen-name-strings --gen-all set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes ..\%buildtype%\flatc.exe --binary --cpp --java --kotlin --csharp --dart --go --lobster --lua --ts --php --grpc ^ %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL -..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -I include_test -o monster_test monster_test.fbs monsterdata_test.json || goto FAIL ..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL @@ -45,25 +45,27 @@ set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes @rem For Rust we currently generate two independent schemas, with namespace_test2 @rem duplicating the types in namespace_test1 -..\%buildtype%\flatc.exe --rust --gen-all %TEST_RUST_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL ..\%buildtype%\flatc.exe --cpp --java --csharp --ts --php %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% %TEST_TS_FLAGS% -o union_vector ./union_vector/union_vector.fbs || goto FAIL ..\%buildtype%\flatc.exe --ts --gen-name-strings --gen-mutable %TEST_BASE_FLAGS% %TEST_TS_FLAGS% -I include_test monster_test.fbs ..\%buildtype%\flatc.exe %TEST_BASE_FLAGS% %TEST_TS_FLAGS% -b -I include_test monster_test.fbs unicode_test.json ..\%buildtype%\flatc.exe --ts --gen-name-strings %TEST_BASE_FLAGS% %TEST_TS_FLAGS% -o union_vector union_vector/union_vector.fbs -..\%buildtype%\flatc.exe --rust -I include_test -o include_test include_test/include_test1.fbs || goto FAIL -..\%buildtype%\flatc.exe --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -I include_test -o include_test1 include_test/include_test1.fbs || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -I include_test -o include_test2 include_test/sub/include_test2.fbs || goto FAIL ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-filenames . --bfbs-builtins -I include_test monster_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --cpp --bfbs-comments --bfbs-builtins --bfbs-gen-embed %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% -I include_test monster_test.fbs || goto FAIL ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs || goto FAIL -..\%buildtype%\flatc.exe --cpp --java --csharp --jsonschema --rust %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% --scoped-enums arrays_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --cpp --java --csharp --jsonschema %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% --scoped-enums arrays_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -o arrays_test arrays_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% arrays_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --cpp %TEST_BASE_FLAGS% --cpp-ptr-type flatbuffers::unique_ptr native_type_test.fbs || goto FAIL @rem Generate the optional scalar code for tests. ..\%buildtype%\flatc.exe --java --kotlin --lobster --ts optional_scalars.fbs || goto FAIL -..\%buildtype%\flatc.exe --csharp --rust --gen-object-api optional_scalars.fbs || goto FAIL +..\%buildtype%\flatc.exe --csharp --gen-object-api optional_scalars.fbs || goto FAIL +..\%buildtype%\flatc.exe --rust %TEST_RUST_FLAGS% -o optional_scalars optional_scalars.fbs || goto FAIL ..\%buildtype%\flatc.exe %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% --cpp optional_scalars.fbs || goto FAIL @rem Generate the schema evolution tests diff --git a/tests/generate_code.sh b/tests/generate_code.sh index c8b62bd68..c7d68d3a1 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -28,12 +28,13 @@ TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_F TEST_CS_FLAGS="--cs-gen-json-serializer" TEST_TS_FLAGS="--gen-name-strings" TEST_BASE_FLAGS="--reflect-names --gen-mutable --gen-object-api" -TEST_RUST_FLAGS="$TEST_BASE_FLAGS --gen-name-strings" +TEST_RUST_FLAGS="$TEST_BASE_FLAGS --gen-all --gen-name-strings" TEST_NOINCL_FLAGS="$TEST_BASE_FLAGS --no-includes" + ../flatc --binary --cpp --java --kotlin --csharp --dart --go --lobster --lua --ts --php --grpc \ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -I include_test monster_test.fbs monsterdata_test.json -../flatc --rust $TEST_RUST_FLAGS -I include_test monster_test.fbs monsterdata_test.json +../flatc --rust $TEST_RUST_FLAGS -o monster_test -I include_test monster_test.fbs monsterdata_test.json ../flatc --python $TEST_BASE_FLAGS -I include_test monster_test.fbs monsterdata_test.json @@ -48,24 +49,26 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_TS_FLAGS -o namespace_te ../flatc --ts --gen-name-strings --gen-mutable $TEST_BASE_FLAGS $TEST_TS_FLAGS -I include_test monster_test.fbs ../flatc $TEST_BASE_FLAGS $TEST_TS_FLAGS -b -I include_test monster_test.fbs unicode_test.json ../flatc --ts --gen-name-strings $TEST_BASE_FLAGS $TEST_TS_FLAGS -o union_vector union_vector/union_vector.fbs -../flatc --rust -I include_test -o include_test include_test/include_test1.fbs -../flatc --rust -I include_test -o include_test/sub include_test/sub/include_test2.fbs +../flatc --rust $TEST_RUST_FLAGS -o include_test1 -I include_test include_test/include_test1.fbs +../flatc --rust $TEST_RUST_FLAGS -o include_test2 -I include_test include_test/sub/include_test2.fbs ../flatc -b --schema --bfbs-comments --bfbs-filenames . --bfbs-builtins -I include_test monster_test.fbs ../flatc --cpp --bfbs-comments --bfbs-builtins --bfbs-gen-embed $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS -I include_test monster_test.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs ../flatc --jsonschema --schema -I include_test monster_test.fbs ../flatc --cpp --java --kotlin --csharp --python $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS monster_extra.fbs monsterdata_extra.json -../flatc --cpp --java --csharp --jsonschema --rust $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS --scoped-enums arrays_test.fbs +../flatc --cpp --java --csharp --jsonschema $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS --scoped-enums arrays_test.fbs +../flatc --rust $TEST_RUST_FLAGS -o arrays_test arrays_test.fbs ../flatc --python $TEST_BASE_FLAGS arrays_test.fbs ../flatc --dart --gen-object-api monster_extra.fbs # Generate optional scalar code for tests. ../flatc --java --kotlin --lobster --ts optional_scalars.fbs -../flatc --csharp --rust --gen-object-api optional_scalars.fbs +../flatc --csharp --gen-object-api optional_scalars.fbs +../flatc --rust $TEST_RUST_FLAGS -o optional_scalars optional_scalars.fbs ../flatc $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS --cpp optional_scalars.fbs # Generate string/vector default code for tests -../flatc --rust --gen-object-api more_defaults.fbs +../flatc --rust $TEST_RUST_FLAGS -o more_defaults more_defaults.fbs # Generate the schema evolution tests ../flatc --cpp --scoped-enums $TEST_CPP_FLAGS -o evolution_test ./evolution_test/evolution_v*.fbs @@ -96,7 +99,8 @@ TEST_CPP17_FLAGS="--cpp --cpp-std c++17 --cpp-static-reflection -o ./cpp17/gener ../flatc $TEST_CPP17_FLAGS optional_scalars.fbs cd ../samples -../flatc --cpp --rust --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs +../flatc --cpp --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs +../flatc --rust $TEST_RUST_FLAGS -o rust_generated monster.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs cd ../reflection ./generate_code.sh --cpp-std c++0x diff --git a/tests/include_test/sub/include_test2_generated.rs b/tests/include_test/sub/include_test2_generated.rs deleted file mode 100644 index 92e09bc80..000000000 --- a/tests/include_test/sub/include_test2_generated.rs +++ /dev/null @@ -1,306 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use crate::include_test1_generated::*; -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod my_game { - - use crate::include_test1_generated::*; - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; -#[allow(unused_imports, dead_code)] -pub mod other_name_space { - - use crate::include_test1_generated::*; - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_FROM_INCLUDE: i64 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_FROM_INCLUDE: i64 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [ - FromInclude::IncludeVal, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct FromInclude(pub i64); -#[allow(non_upper_case_globals)] -impl FromInclude { - pub const IncludeVal: Self = Self(0); - - pub const ENUM_MIN: i64 = 0; - pub const ENUM_MAX: i64 = 0; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::IncludeVal, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::IncludeVal => Some("IncludeVal"), - _ => None, - } - } -} -impl std::fmt::Debug for FromInclude { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for FromInclude { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for FromInclude { - type Output = FromInclude; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for FromInclude { - #[inline] - fn to_little_endian(self) -> Self { - let b = i64::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i64::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for FromInclude { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i64::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for FromInclude {} -// struct Unused, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct Unused(pub [u8; 4]); -impl Default for Unused { - fn default() -> Self { - Self([0; 4]) - } -} -impl std::fmt::Debug for Unused { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Unused") - .field("a", &self.a()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Unused {} -impl flatbuffers::SafeSliceAccess for Unused {} -impl<'a> flatbuffers::Follow<'a> for Unused { - type Inner = &'a Unused; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a Unused>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a Unused { - type Inner = &'a Unused; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for Unused { - type Output = Unused; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b Unused { - type Output = Unused; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const Unused as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for Unused { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> Unused { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: i32, - ) -> Self { - let mut s = Self([0; 4]); - s.set_a(a); - s - } - - pub fn a(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_a(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - -} - -pub enum TableBOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TableB<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TableB<'a> { - type Inner = TableB<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TableB<'a> { - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TableB { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TableBArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = TableBBuilder::new(_fbb); - if let Some(x) = args.a { builder.add_a(x); } - builder.finish() - } - - pub const VT_A: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn a(&self) -> Option> { - self._tab.get::>(TableB::VT_A, None) - } -} - -impl flatbuffers::Verifiable for TableB<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"a", Self::VT_A, false)? - .finish(); - Ok(()) - } -} -pub struct TableBArgs<'a> { - pub a: Option>>, -} -impl<'a> Default for TableBArgs<'a> { - #[inline] - fn default() -> Self { - TableBArgs { - a: None, - } - } -} -pub struct TableBBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TableBBuilder<'a, 'b> { - #[inline] - pub fn add_a(&mut self, a: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TableB::VT_A, a); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableBBuilder<'a, 'b> { - let start = _fbb.start_table(); - TableBBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TableB<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TableB"); - ds.field("a", &self.a()); - ds.finish() - } -} -} // pub mod OtherNameSpace -} // pub mod MyGame - diff --git a/tests/include_test1/mod.rs b/tests/include_test1/mod.rs new file mode 100644 index 000000000..839ec17c5 --- /dev/null +++ b/tests/include_test1/mod.rs @@ -0,0 +1,15 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod my_game { + use super::*; + pub mod other_name_space { + use super::*; + mod from_include_generated; + pub use self::from_include_generated::*; + mod unused_generated; + pub use self::unused_generated::*; + mod table_b_generated; + pub use self::table_b_generated::*; + } // other_name_space +} // my_game +mod table_a_generated; +pub use self::table_a_generated::*; diff --git a/tests/include_test1/my_game/other_name_space/from_include_generated.rs b/tests/include_test1/my_game/other_name_space/from_include_generated.rs new file mode 100644 index 000000000..048bafdb8 --- /dev/null +++ b/tests/include_test1/my_game/other_name_space/from_include_generated.rs @@ -0,0 +1,89 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [ + FromInclude::IncludeVal, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct FromInclude(pub i64); +#[allow(non_upper_case_globals)] +impl FromInclude { + pub const IncludeVal: Self = Self(0); + + pub const ENUM_MIN: i64 = 0; + pub const ENUM_MAX: i64 = 0; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::IncludeVal, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::IncludeVal => Some("IncludeVal"), + _ => None, + } + } +} +impl std::fmt::Debug for FromInclude { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for FromInclude { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for FromInclude { + type Output = FromInclude; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for FromInclude { + #[inline] + fn to_little_endian(self) -> Self { + let b = i64::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i64::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for FromInclude { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i64::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for FromInclude {} diff --git a/tests/include_test1/my_game/other_name_space/table_b_generated.rs b/tests/include_test1/my_game/other_name_space/table_b_generated.rs new file mode 100644 index 000000000..6d863e34f --- /dev/null +++ b/tests/include_test1/my_game/other_name_space/table_b_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableBOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableB<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableB<'a> { + type Inner = TableB<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableB<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.TableB" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableB { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableBArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableBBuilder::new(_fbb); + if let Some(x) = args.a { builder.add_a(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableBT { + let a = self.a().map(|x| { + Box::new(x.unpack()) + }); + TableBT { + a, + } + } + pub const VT_A: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn a(&self) -> Option> { + self._tab.get::>(TableB::VT_A, None) + } +} + +impl flatbuffers::Verifiable for TableB<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("a", Self::VT_A, false)? + .finish(); + Ok(()) + } +} +pub struct TableBArgs<'a> { + pub a: Option>>, +} +impl<'a> Default for TableBArgs<'a> { + #[inline] + fn default() -> Self { + TableBArgs { + a: None, + } + } +} +pub struct TableBBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableBBuilder<'a, 'b> { + #[inline] + pub fn add_a(&mut self, a: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableB::VT_A, a); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableBBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableBBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableB<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableB"); + ds.field("a", &self.a()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableBT { + pub a: Option>, +} +impl Default for TableBT { + fn default() -> Self { + Self { + a: None, + } + } +} +impl TableBT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let a = self.a.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableB::create(_fbb, &TableBArgs{ + a, + }) + } +} diff --git a/tests/include_test1/my_game/other_name_space/unused_generated.rs b/tests/include_test1/my_game/other_name_space/unused_generated.rs new file mode 100644 index 000000000..41725f3fa --- /dev/null +++ b/tests/include_test1/my_game/other_name_space/unused_generated.rs @@ -0,0 +1,126 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Unused, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Unused(pub [u8; 4]); +impl Default for Unused { + fn default() -> Self { + Self([0; 4]) + } +} +impl std::fmt::Debug for Unused { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Unused") + .field("a", &self.a()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Unused {} +impl flatbuffers::SafeSliceAccess for Unused {} +impl<'a> flatbuffers::Follow<'a> for Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Unused>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Unused { + type Output = Unused; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Unused { + type Output = Unused; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for Unused { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Unused { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i32, + ) -> Self { + let mut s = Self([0; 4]); + s.set_a(a); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.Unused" + } + + pub fn a(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> UnusedT { + UnusedT { + a: self.a(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct UnusedT { + pub a: i32, +} +impl UnusedT { + pub fn pack(&self) -> Unused { + Unused::new( + self.a, + ) + } +} + diff --git a/tests/include_test/include_test1_generated.rs b/tests/include_test1/table_a_generated.rs similarity index 78% rename from tests/include_test/include_test1_generated.rs rename to tests/include_test1/table_a_generated.rs index 44cfaa762..97f5c267c 100644 --- a/tests/include_test/include_test1_generated.rs +++ b/tests/include_test1/table_a_generated.rs @@ -1,14 +1,9 @@ // automatically generated by the FlatBuffers compiler, do not modify - - - -use crate::include_test2_generated::*; +extern crate flatbuffers; use std::mem; use std::cmp::Ordering; - -extern crate flatbuffers; use self::flatbuffers::{EndianScalar, Follow}; - +use super::*; pub enum TableAOffset {} #[derive(Copy, Clone, PartialEq)] @@ -25,6 +20,10 @@ impl<'a> flatbuffers::Follow<'a> for TableA<'a> { } impl<'a> TableA<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "TableA" + } + #[inline] pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { TableA { _tab: table } @@ -38,6 +37,14 @@ impl<'a> TableA<'a> { builder.finish() } + pub fn unpack(&self) -> TableAT { + let b = self.b().map(|x| { + Box::new(x.unpack()) + }); + TableAT { + b, + } + } pub const VT_B: flatbuffers::VOffsetT = 4; #[inline] @@ -53,7 +60,7 @@ impl flatbuffers::Verifiable for TableA<'_> { ) -> Result<(), flatbuffers::InvalidFlatbuffer> { use self::flatbuffers::Verifiable; v.visit_table(pos)? - .visit_field::>(&"b", Self::VT_B, false)? + .visit_field::>("b", Self::VT_B, false)? .finish(); Ok(()) } @@ -100,3 +107,28 @@ impl std::fmt::Debug for TableA<'_> { ds.finish() } } +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableAT { + pub b: Option>, +} +impl Default for TableAT { + fn default() -> Self { + Self { + b: None, + } + } +} +impl TableAT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let b = self.b.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableA::create(_fbb, &TableAArgs{ + b, + }) + } +} diff --git a/tests/include_test2/mod.rs b/tests/include_test2/mod.rs new file mode 100644 index 000000000..839ec17c5 --- /dev/null +++ b/tests/include_test2/mod.rs @@ -0,0 +1,15 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod my_game { + use super::*; + pub mod other_name_space { + use super::*; + mod from_include_generated; + pub use self::from_include_generated::*; + mod unused_generated; + pub use self::unused_generated::*; + mod table_b_generated; + pub use self::table_b_generated::*; + } // other_name_space +} // my_game +mod table_a_generated; +pub use self::table_a_generated::*; diff --git a/tests/include_test2/my_game/other_name_space/from_include_generated.rs b/tests/include_test2/my_game/other_name_space/from_include_generated.rs new file mode 100644 index 000000000..048bafdb8 --- /dev/null +++ b/tests/include_test2/my_game/other_name_space/from_include_generated.rs @@ -0,0 +1,89 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [ + FromInclude::IncludeVal, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct FromInclude(pub i64); +#[allow(non_upper_case_globals)] +impl FromInclude { + pub const IncludeVal: Self = Self(0); + + pub const ENUM_MIN: i64 = 0; + pub const ENUM_MAX: i64 = 0; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::IncludeVal, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::IncludeVal => Some("IncludeVal"), + _ => None, + } + } +} +impl std::fmt::Debug for FromInclude { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for FromInclude { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for FromInclude { + type Output = FromInclude; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for FromInclude { + #[inline] + fn to_little_endian(self) -> Self { + let b = i64::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i64::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for FromInclude { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i64::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for FromInclude {} diff --git a/tests/include_test2/my_game/other_name_space/table_b_generated.rs b/tests/include_test2/my_game/other_name_space/table_b_generated.rs new file mode 100644 index 000000000..6d863e34f --- /dev/null +++ b/tests/include_test2/my_game/other_name_space/table_b_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableBOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableB<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableB<'a> { + type Inner = TableB<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableB<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.TableB" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableB { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableBArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableBBuilder::new(_fbb); + if let Some(x) = args.a { builder.add_a(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableBT { + let a = self.a().map(|x| { + Box::new(x.unpack()) + }); + TableBT { + a, + } + } + pub const VT_A: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn a(&self) -> Option> { + self._tab.get::>(TableB::VT_A, None) + } +} + +impl flatbuffers::Verifiable for TableB<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("a", Self::VT_A, false)? + .finish(); + Ok(()) + } +} +pub struct TableBArgs<'a> { + pub a: Option>>, +} +impl<'a> Default for TableBArgs<'a> { + #[inline] + fn default() -> Self { + TableBArgs { + a: None, + } + } +} +pub struct TableBBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableBBuilder<'a, 'b> { + #[inline] + pub fn add_a(&mut self, a: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableB::VT_A, a); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableBBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableBBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableB<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableB"); + ds.field("a", &self.a()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableBT { + pub a: Option>, +} +impl Default for TableBT { + fn default() -> Self { + Self { + a: None, + } + } +} +impl TableBT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let a = self.a.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableB::create(_fbb, &TableBArgs{ + a, + }) + } +} diff --git a/tests/include_test2/my_game/other_name_space/unused_generated.rs b/tests/include_test2/my_game/other_name_space/unused_generated.rs new file mode 100644 index 000000000..41725f3fa --- /dev/null +++ b/tests/include_test2/my_game/other_name_space/unused_generated.rs @@ -0,0 +1,126 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Unused, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Unused(pub [u8; 4]); +impl Default for Unused { + fn default() -> Self { + Self([0; 4]) + } +} +impl std::fmt::Debug for Unused { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Unused") + .field("a", &self.a()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Unused {} +impl flatbuffers::SafeSliceAccess for Unused {} +impl<'a> flatbuffers::Follow<'a> for Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Unused>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Unused { + type Output = Unused; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Unused { + type Output = Unused; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for Unused { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Unused { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i32, + ) -> Self { + let mut s = Self([0; 4]); + s.set_a(a); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.Unused" + } + + pub fn a(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> UnusedT { + UnusedT { + a: self.a(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct UnusedT { + pub a: i32, +} +impl UnusedT { + pub fn pack(&self) -> Unused { + Unused::new( + self.a, + ) + } +} + diff --git a/tests/include_test2/table_a_generated.rs b/tests/include_test2/table_a_generated.rs new file mode 100644 index 000000000..97f5c267c --- /dev/null +++ b/tests/include_test2/table_a_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableAOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableA<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableA<'a> { + type Inner = TableA<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableA<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "TableA" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableA { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableAArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableABuilder::new(_fbb); + if let Some(x) = args.b { builder.add_b(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableAT { + let b = self.b().map(|x| { + Box::new(x.unpack()) + }); + TableAT { + b, + } + } + pub const VT_B: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn b(&self) -> Option> { + self._tab.get::>(TableA::VT_B, None) + } +} + +impl flatbuffers::Verifiable for TableA<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("b", Self::VT_B, false)? + .finish(); + Ok(()) + } +} +pub struct TableAArgs<'a> { + pub b: Option>>, +} +impl<'a> Default for TableAArgs<'a> { + #[inline] + fn default() -> Self { + TableAArgs { + b: None, + } + } +} +pub struct TableABuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableABuilder<'a, 'b> { + #[inline] + pub fn add_b(&mut self, b: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableA::VT_B, b); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableABuilder<'a, 'b> { + let start = _fbb.start_table(); + TableABuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableA<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableA"); + ds.field("b", &self.b()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableAT { + pub b: Option>, +} +impl Default for TableAT { + fn default() -> Self { + Self { + b: None, + } + } +} +impl TableAT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let b = self.b.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableA::create(_fbb, &TableAArgs{ + b, + }) + } +} diff --git a/tests/monster_test/mod.rs b/tests/monster_test/mod.rs new file mode 100644 index 000000000..69c599bc4 --- /dev/null +++ b/tests/monster_test/mod.rs @@ -0,0 +1,53 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod my_game { + use super::*; + pub mod example { + use super::*; + mod color_generated; + pub use self::color_generated::*; + mod race_generated; + pub use self::race_generated::*; + mod any_generated; + pub use self::any_generated::*; + mod any_unique_aliases_generated; + pub use self::any_unique_aliases_generated::*; + mod any_ambiguous_aliases_generated; + pub use self::any_ambiguous_aliases_generated::*; + mod test_generated; + pub use self::test_generated::*; + mod test_simple_table_with_enum_generated; + pub use self::test_simple_table_with_enum_generated::*; + mod vec_3_generated; + pub use self::vec_3_generated::*; + mod ability_generated; + pub use self::ability_generated::*; + mod struct_of_structs_generated; + pub use self::struct_of_structs_generated::*; + mod stat_generated; + pub use self::stat_generated::*; + mod referrable_generated; + pub use self::referrable_generated::*; + mod monster_generated; + pub use self::monster_generated::*; + mod type_aliases_generated; + pub use self::type_aliases_generated::*; + } // example + pub mod example_2 { + use super::*; + mod monster_generated; + pub use self::monster_generated::*; + } // example_2 + pub mod other_name_space { + use super::*; + mod from_include_generated; + pub use self::from_include_generated::*; + mod unused_generated; + pub use self::unused_generated::*; + mod table_b_generated; + pub use self::table_b_generated::*; + } // other_name_space + mod in_parent_namespace_generated; + pub use self::in_parent_namespace_generated::*; +} // my_game +mod table_a_generated; +pub use self::table_a_generated::*; diff --git a/tests/monster_test/my_game/example/ability_generated.rs b/tests/monster_test/my_game/example/ability_generated.rs new file mode 100644 index 000000000..7ff5d88e0 --- /dev/null +++ b/tests/monster_test/my_game/example/ability_generated.rs @@ -0,0 +1,165 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Ability, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Ability(pub [u8; 8]); +impl Default for Ability { + fn default() -> Self { + Self([0; 8]) + } +} +impl std::fmt::Debug for Ability { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Ability") + .field("id", &self.id()) + .field("distance", &self.distance()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Ability {} +impl flatbuffers::SafeSliceAccess for Ability {} +impl<'a> flatbuffers::Follow<'a> for Ability { + type Inner = &'a Ability; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Ability>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Ability { + type Inner = &'a Ability; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Ability { + type Output = Ability; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Ability as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Ability { + type Output = Ability; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Ability as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for Ability { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Ability { + #[allow(clippy::too_many_arguments)] + pub fn new( + id: u32, + distance: u32, + ) -> Self { + let mut s = Self([0; 8]); + s.set_id(id); + s.set_distance(distance); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Ability" + } + + pub fn id(&self) -> u32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_id(&mut self, x: u32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const u32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + #[inline] + pub fn key_compare_less_than(&self, o: &Ability) -> bool { + self.id() < o.id() + } + + #[inline] + pub fn key_compare_with_value(&self, val: u32) -> ::std::cmp::Ordering { + let key = self.id(); + key.cmp(&val) + } + pub fn distance(&self) -> u32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_distance(&mut self, x: u32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const u32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> AbilityT { + AbilityT { + id: self.id(), + distance: self.distance(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct AbilityT { + pub id: u32, + pub distance: u32, +} +impl AbilityT { + pub fn pack(&self) -> Ability { + Ability::new( + self.id, + self.distance, + ) + } +} + diff --git a/tests/monster_test/my_game/example/any_ambiguous_aliases_generated.rs b/tests/monster_test/my_game/example/any_ambiguous_aliases_generated.rs new file mode 100644 index 000000000..321007abe --- /dev/null +++ b/tests/monster_test/my_game/example/any_ambiguous_aliases_generated.rs @@ -0,0 +1,198 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ANY_AMBIGUOUS_ALIASES: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ANY_AMBIGUOUS_ALIASES: u8 = 3; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES: [AnyAmbiguousAliases; 4] = [ + AnyAmbiguousAliases::NONE, + AnyAmbiguousAliases::M1, + AnyAmbiguousAliases::M2, + AnyAmbiguousAliases::M3, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct AnyAmbiguousAliases(pub u8); +#[allow(non_upper_case_globals)] +impl AnyAmbiguousAliases { + pub const NONE: Self = Self(0); + pub const M1: Self = Self(1); + pub const M2: Self = Self(2); + pub const M3: Self = Self(3); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 3; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::M1, + Self::M2, + Self::M3, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::M1 => Some("M1"), + Self::M2 => Some("M2"), + Self::M3 => Some("M3"), + _ => None, + } + } +} +impl std::fmt::Debug for AnyAmbiguousAliases { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for AnyAmbiguousAliases { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for AnyAmbiguousAliases { + type Output = AnyAmbiguousAliases; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for AnyAmbiguousAliases { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for AnyAmbiguousAliases { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for AnyAmbiguousAliases {} +pub struct AnyAmbiguousAliasesUnionTableOffset {} + +#[allow(clippy::upper_case_acronyms)] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum AnyAmbiguousAliasesT { + NONE, + M1(Box), + M2(Box), + M3(Box), +} +impl Default for AnyAmbiguousAliasesT { + fn default() -> Self { + Self::NONE + } +} +impl AnyAmbiguousAliasesT { + pub fn any_ambiguous_aliases_type(&self) -> AnyAmbiguousAliases { + match self { + Self::NONE => AnyAmbiguousAliases::NONE, + Self::M1(_) => AnyAmbiguousAliases::M1, + Self::M2(_) => AnyAmbiguousAliases::M2, + Self::M3(_) => AnyAmbiguousAliases::M3, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::M1(v) => Some(v.pack(fbb).as_union_value()), + Self::M2(v) => Some(v.pack(fbb).as_union_value()), + Self::M3(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned MonsterT, setting the union to NONE. + pub fn take_m1(&mut self) -> Option> { + if let Self::M1(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::M1(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the MonsterT. + pub fn as_m1(&self) -> Option<&MonsterT> { + if let Self::M1(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the MonsterT. + pub fn as_m1_mut(&mut self) -> Option<&mut MonsterT> { + if let Self::M1(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned MonsterT, setting the union to NONE. + pub fn take_m2(&mut self) -> Option> { + if let Self::M2(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::M2(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the MonsterT. + pub fn as_m2(&self) -> Option<&MonsterT> { + if let Self::M2(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the MonsterT. + pub fn as_m2_mut(&mut self) -> Option<&mut MonsterT> { + if let Self::M2(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned MonsterT, setting the union to NONE. + pub fn take_m3(&mut self) -> Option> { + if let Self::M3(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::M3(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the MonsterT. + pub fn as_m3(&self) -> Option<&MonsterT> { + if let Self::M3(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the MonsterT. + pub fn as_m3_mut(&mut self) -> Option<&mut MonsterT> { + if let Self::M3(v) = self { Some(v.as_mut()) } else { None } + } +} diff --git a/tests/monster_test/my_game/example/any_generated.rs b/tests/monster_test/my_game/example/any_generated.rs new file mode 100644 index 000000000..6f7f6fdac --- /dev/null +++ b/tests/monster_test/my_game/example/any_generated.rs @@ -0,0 +1,198 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ANY: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ANY: u8 = 3; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ANY: [Any; 4] = [ + Any::NONE, + Any::Monster, + Any::TestSimpleTableWithEnum, + Any::MyGame_Example2_Monster, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct Any(pub u8); +#[allow(non_upper_case_globals)] +impl Any { + pub const NONE: Self = Self(0); + pub const Monster: Self = Self(1); + pub const TestSimpleTableWithEnum: Self = Self(2); + pub const MyGame_Example2_Monster: Self = Self(3); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 3; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::Monster, + Self::TestSimpleTableWithEnum, + Self::MyGame_Example2_Monster, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::Monster => Some("Monster"), + Self::TestSimpleTableWithEnum => Some("TestSimpleTableWithEnum"), + Self::MyGame_Example2_Monster => Some("MyGame_Example2_Monster"), + _ => None, + } + } +} +impl std::fmt::Debug for Any { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for Any { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for Any { + type Output = Any; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for Any { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for Any { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Any {} +pub struct AnyUnionTableOffset {} + +#[allow(clippy::upper_case_acronyms)] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum AnyT { + NONE, + Monster(Box), + TestSimpleTableWithEnum(Box), + MyGameExample2Monster(Box), +} +impl Default for AnyT { + fn default() -> Self { + Self::NONE + } +} +impl AnyT { + pub fn any_type(&self) -> Any { + match self { + Self::NONE => Any::NONE, + Self::Monster(_) => Any::Monster, + Self::TestSimpleTableWithEnum(_) => Any::TestSimpleTableWithEnum, + Self::MyGameExample2Monster(_) => Any::MyGame_Example2_Monster, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::Monster(v) => Some(v.pack(fbb).as_union_value()), + Self::TestSimpleTableWithEnum(v) => Some(v.pack(fbb).as_union_value()), + Self::MyGameExample2Monster(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned MonsterT, setting the union to NONE. + pub fn take_monster(&mut self) -> Option> { + if let Self::Monster(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::Monster(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the MonsterT. + pub fn as_monster(&self) -> Option<&MonsterT> { + if let Self::Monster(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the MonsterT. + pub fn as_monster_mut(&mut self) -> Option<&mut MonsterT> { + if let Self::Monster(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned TestSimpleTableWithEnumT, setting the union to NONE. + pub fn take_test_simple_table_with_enum(&mut self) -> Option> { + if let Self::TestSimpleTableWithEnum(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::TestSimpleTableWithEnum(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the TestSimpleTableWithEnumT. + pub fn as_test_simple_table_with_enum(&self) -> Option<&TestSimpleTableWithEnumT> { + if let Self::TestSimpleTableWithEnum(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the TestSimpleTableWithEnumT. + pub fn as_test_simple_table_with_enum_mut(&mut self) -> Option<&mut TestSimpleTableWithEnumT> { + if let Self::TestSimpleTableWithEnum(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned super::example_2::MonsterT, setting the union to NONE. + pub fn take_my_game_example_2_monster(&mut self) -> Option> { + if let Self::MyGameExample2Monster(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::MyGameExample2Monster(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the super::example_2::MonsterT. + pub fn as_my_game_example_2_monster(&self) -> Option<&super::example_2::MonsterT> { + if let Self::MyGameExample2Monster(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the super::example_2::MonsterT. + pub fn as_my_game_example_2_monster_mut(&mut self) -> Option<&mut super::example_2::MonsterT> { + if let Self::MyGameExample2Monster(v) = self { Some(v.as_mut()) } else { None } + } +} diff --git a/tests/monster_test/my_game/example/any_unique_aliases_generated.rs b/tests/monster_test/my_game/example/any_unique_aliases_generated.rs new file mode 100644 index 000000000..1749d5d7a --- /dev/null +++ b/tests/monster_test/my_game/example/any_unique_aliases_generated.rs @@ -0,0 +1,198 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ANY_UNIQUE_ALIASES: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ANY_UNIQUE_ALIASES: u8 = 3; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ANY_UNIQUE_ALIASES: [AnyUniqueAliases; 4] = [ + AnyUniqueAliases::NONE, + AnyUniqueAliases::M, + AnyUniqueAliases::TS, + AnyUniqueAliases::M2, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct AnyUniqueAliases(pub u8); +#[allow(non_upper_case_globals)] +impl AnyUniqueAliases { + pub const NONE: Self = Self(0); + pub const M: Self = Self(1); + pub const TS: Self = Self(2); + pub const M2: Self = Self(3); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 3; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::M, + Self::TS, + Self::M2, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::M => Some("M"), + Self::TS => Some("TS"), + Self::M2 => Some("M2"), + _ => None, + } + } +} +impl std::fmt::Debug for AnyUniqueAliases { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for AnyUniqueAliases { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for AnyUniqueAliases { + type Output = AnyUniqueAliases; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for AnyUniqueAliases { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for AnyUniqueAliases { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for AnyUniqueAliases {} +pub struct AnyUniqueAliasesUnionTableOffset {} + +#[allow(clippy::upper_case_acronyms)] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum AnyUniqueAliasesT { + NONE, + M(Box), + TS(Box), + M2(Box), +} +impl Default for AnyUniqueAliasesT { + fn default() -> Self { + Self::NONE + } +} +impl AnyUniqueAliasesT { + pub fn any_unique_aliases_type(&self) -> AnyUniqueAliases { + match self { + Self::NONE => AnyUniqueAliases::NONE, + Self::M(_) => AnyUniqueAliases::M, + Self::TS(_) => AnyUniqueAliases::TS, + Self::M2(_) => AnyUniqueAliases::M2, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::M(v) => Some(v.pack(fbb).as_union_value()), + Self::TS(v) => Some(v.pack(fbb).as_union_value()), + Self::M2(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned MonsterT, setting the union to NONE. + pub fn take_m(&mut self) -> Option> { + if let Self::M(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::M(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the MonsterT. + pub fn as_m(&self) -> Option<&MonsterT> { + if let Self::M(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the MonsterT. + pub fn as_m_mut(&mut self) -> Option<&mut MonsterT> { + if let Self::M(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned TestSimpleTableWithEnumT, setting the union to NONE. + pub fn take_ts(&mut self) -> Option> { + if let Self::TS(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::TS(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the TestSimpleTableWithEnumT. + pub fn as_ts(&self) -> Option<&TestSimpleTableWithEnumT> { + if let Self::TS(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the TestSimpleTableWithEnumT. + pub fn as_ts_mut(&mut self) -> Option<&mut TestSimpleTableWithEnumT> { + if let Self::TS(v) = self { Some(v.as_mut()) } else { None } + } + /// If the union variant matches, return the owned super::example_2::MonsterT, setting the union to NONE. + pub fn take_m2(&mut self) -> Option> { + if let Self::M2(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::M2(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the super::example_2::MonsterT. + pub fn as_m2(&self) -> Option<&super::example_2::MonsterT> { + if let Self::M2(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the super::example_2::MonsterT. + pub fn as_m2_mut(&mut self) -> Option<&mut super::example_2::MonsterT> { + if let Self::M2(v) = self { Some(v.as_mut()) } else { None } + } +} diff --git a/tests/monster_test/my_game/example/color_generated.rs b/tests/monster_test/my_game/example/color_generated.rs new file mode 100644 index 000000000..4a3282b46 --- /dev/null +++ b/tests/monster_test/my_game/example/color_generated.rs @@ -0,0 +1,67 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[allow(non_upper_case_globals)] +mod bitflags_color { + flatbuffers::bitflags::bitflags! { + /// Composite components of Monster color. + #[derive(Default)] + pub struct Color: u8 { + const Red = 1; + /// \brief color Green + /// Green is bit_flag with value (1u << 1) + const Green = 2; + /// \brief color Blue (1u << 3) + const Blue = 8; + } + } +} +pub use self::bitflags_color::Color; + +impl<'a> flatbuffers::Follow<'a> for Color { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + unsafe { Self::from_bits_unchecked(b) } + } +} + +impl flatbuffers::Push for Color { + type Output = Color; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.bits()); } + } +} + +impl flatbuffers::EndianScalar for Color { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.bits()); + unsafe { Self::from_bits_unchecked(b) } + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.bits()); + unsafe { Self::from_bits_unchecked(b) } + } +} + +impl<'a> flatbuffers::Verifiable for Color { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Color {} diff --git a/tests/monster_test/my_game/example/monster_generated.rs b/tests/monster_test/my_game/example/monster_generated.rs new file mode 100644 index 000000000..d2ad1ee41 --- /dev/null +++ b/tests/monster_test/my_game/example/monster_generated.rs @@ -0,0 +1,1562 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum MonsterOffset {} +#[derive(Copy, Clone, PartialEq)] + +/// an example documentation comment: "monster object" +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, loc } } + } +} + +impl<'a> Monster<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Monster" + } + + #[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); + builder.add_non_owning_reference(args.non_owning_reference); + builder.add_co_owning_reference(args.co_owning_reference); + builder.add_single_weak_reference(args.single_weak_reference); + builder.add_testhashu64_fnv1a(args.testhashu64_fnv1a); + builder.add_testhashs64_fnv1a(args.testhashs64_fnv1a); + builder.add_testhashu64_fnv1(args.testhashu64_fnv1); + builder.add_testhashs64_fnv1(args.testhashs64_fnv1); + if let Some(x) = args.scalar_key_sorted_tables { builder.add_scalar_key_sorted_tables(x); } + if let Some(x) = args.testrequirednestedflatbuffer { builder.add_testrequirednestedflatbuffer(x); } + if let Some(x) = args.vector_of_enums { builder.add_vector_of_enums(x); } + if let Some(x) = args.any_ambiguous { builder.add_any_ambiguous(x); } + if let Some(x) = args.any_unique { builder.add_any_unique(x); } + if let Some(x) = args.vector_of_non_owning_references { builder.add_vector_of_non_owning_references(x); } + if let Some(x) = args.vector_of_co_owning_references { builder.add_vector_of_co_owning_references(x); } + if let Some(x) = args.vector_of_strong_referrables { builder.add_vector_of_strong_referrables(x); } + if let Some(x) = args.vector_of_weak_references { builder.add_vector_of_weak_references(x); } + if let Some(x) = args.vector_of_referrables { builder.add_vector_of_referrables(x); } + if let Some(x) = args.parent_namespace_test { builder.add_parent_namespace_test(x); } + if let Some(x) = args.vector_of_doubles { builder.add_vector_of_doubles(x); } + if let Some(x) = args.vector_of_longs { builder.add_vector_of_longs(x); } + if let Some(x) = args.test5 { builder.add_test5(x); } + if let Some(x) = args.flex { builder.add_flex(x); } + if let Some(x) = args.testarrayofsortedstruct { builder.add_testarrayofsortedstruct(x); } + if let Some(x) = args.testarrayofstring2 { builder.add_testarrayofstring2(x); } + builder.add_testf3(args.testf3); + builder.add_testf2(args.testf2); + builder.add_testf(args.testf); + if let Some(x) = args.testarrayofbools { builder.add_testarrayofbools(x); } + builder.add_testhashu32_fnv1a(args.testhashu32_fnv1a); + builder.add_testhashs32_fnv1a(args.testhashs32_fnv1a); + builder.add_testhashu32_fnv1(args.testhashu32_fnv1); + builder.add_testhashs32_fnv1(args.testhashs32_fnv1); + if let Some(x) = args.testempty { builder.add_testempty(x); } + if let Some(x) = args.testnestedflatbuffer { builder.add_testnestedflatbuffer(x); } + if let Some(x) = args.enemy { builder.add_enemy(x); } + if let Some(x) = args.testarrayoftables { builder.add_testarrayoftables(x); } + if let Some(x) = args.testarrayofstring { builder.add_testarrayofstring(x); } + if let Some(x) = args.test4 { builder.add_test4(x); } + if let Some(x) = args.test { builder.add_test(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_signed_enum(args.signed_enum); + builder.add_any_ambiguous_type(args.any_ambiguous_type); + builder.add_any_unique_type(args.any_unique_type); + builder.add_testbool(args.testbool); + builder.add_test_type(args.test_type); + builder.add_color(args.color); + builder.finish() + } + + pub fn unpack(&self) -> MonsterT { + let pos = self.pos().map(|x| { + x.unpack() + }); + let mana = self.mana(); + let hp = self.hp(); + let name = { + let x = self.name(); + x.to_string() + }; + let inventory = self.inventory().map(|x| { + x.to_vec() + }); + let color = self.color(); + let test = match self.test_type() { + Any::NONE => AnyT::NONE, + Any::Monster => AnyT::Monster(Box::new( + self.test_as_monster() + .expect("Invalid union table, expected `Any::Monster`.") + .unpack() + )), + Any::TestSimpleTableWithEnum => AnyT::TestSimpleTableWithEnum(Box::new( + self.test_as_test_simple_table_with_enum() + .expect("Invalid union table, expected `Any::TestSimpleTableWithEnum`.") + .unpack() + )), + Any::MyGame_Example2_Monster => AnyT::MyGameExample2Monster(Box::new( + self.test_as_my_game_example_2_monster() + .expect("Invalid union table, expected `Any::MyGame_Example2_Monster`.") + .unpack() + )), + _ => AnyT::NONE, + }; + let test4 = self.test4().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let testarrayofstring = self.testarrayofstring().map(|x| { + x.iter().map(|s| s.to_string()).collect() + }); + let testarrayoftables = self.testarrayoftables().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let enemy = self.enemy().map(|x| { + Box::new(x.unpack()) + }); + let testnestedflatbuffer = self.testnestedflatbuffer().map(|x| { + x.to_vec() + }); + let testempty = self.testempty().map(|x| { + Box::new(x.unpack()) + }); + let testbool = self.testbool(); + let testhashs32_fnv1 = self.testhashs32_fnv1(); + let testhashu32_fnv1 = self.testhashu32_fnv1(); + let testhashs64_fnv1 = self.testhashs64_fnv1(); + let testhashu64_fnv1 = self.testhashu64_fnv1(); + let testhashs32_fnv1a = self.testhashs32_fnv1a(); + let testhashu32_fnv1a = self.testhashu32_fnv1a(); + let testhashs64_fnv1a = self.testhashs64_fnv1a(); + let testhashu64_fnv1a = self.testhashu64_fnv1a(); + let testarrayofbools = self.testarrayofbools().map(|x| { + x.to_vec() + }); + let testf = self.testf(); + let testf2 = self.testf2(); + let testf3 = self.testf3(); + let testarrayofstring2 = self.testarrayofstring2().map(|x| { + x.iter().map(|s| s.to_string()).collect() + }); + let testarrayofsortedstruct = self.testarrayofsortedstruct().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let flex = self.flex().map(|x| { + x.to_vec() + }); + let test5 = self.test5().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let vector_of_longs = self.vector_of_longs().map(|x| { + x.into_iter().collect() + }); + let vector_of_doubles = self.vector_of_doubles().map(|x| { + x.into_iter().collect() + }); + let parent_namespace_test = self.parent_namespace_test().map(|x| { + Box::new(x.unpack()) + }); + let vector_of_referrables = self.vector_of_referrables().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let single_weak_reference = self.single_weak_reference(); + let vector_of_weak_references = self.vector_of_weak_references().map(|x| { + x.into_iter().collect() + }); + let vector_of_strong_referrables = self.vector_of_strong_referrables().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + let co_owning_reference = self.co_owning_reference(); + let vector_of_co_owning_references = self.vector_of_co_owning_references().map(|x| { + x.into_iter().collect() + }); + let non_owning_reference = self.non_owning_reference(); + let vector_of_non_owning_references = self.vector_of_non_owning_references().map(|x| { + x.into_iter().collect() + }); + let any_unique = match self.any_unique_type() { + AnyUniqueAliases::NONE => AnyUniqueAliasesT::NONE, + AnyUniqueAliases::M => AnyUniqueAliasesT::M(Box::new( + self.any_unique_as_m() + .expect("Invalid union table, expected `AnyUniqueAliases::M`.") + .unpack() + )), + AnyUniqueAliases::TS => AnyUniqueAliasesT::TS(Box::new( + self.any_unique_as_ts() + .expect("Invalid union table, expected `AnyUniqueAliases::TS`.") + .unpack() + )), + AnyUniqueAliases::M2 => AnyUniqueAliasesT::M2(Box::new( + self.any_unique_as_m2() + .expect("Invalid union table, expected `AnyUniqueAliases::M2`.") + .unpack() + )), + _ => AnyUniqueAliasesT::NONE, + }; + let any_ambiguous = match self.any_ambiguous_type() { + AnyAmbiguousAliases::NONE => AnyAmbiguousAliasesT::NONE, + AnyAmbiguousAliases::M1 => AnyAmbiguousAliasesT::M1(Box::new( + self.any_ambiguous_as_m1() + .expect("Invalid union table, expected `AnyAmbiguousAliases::M1`.") + .unpack() + )), + AnyAmbiguousAliases::M2 => AnyAmbiguousAliasesT::M2(Box::new( + self.any_ambiguous_as_m2() + .expect("Invalid union table, expected `AnyAmbiguousAliases::M2`.") + .unpack() + )), + AnyAmbiguousAliases::M3 => AnyAmbiguousAliasesT::M3(Box::new( + self.any_ambiguous_as_m3() + .expect("Invalid union table, expected `AnyAmbiguousAliases::M3`.") + .unpack() + )), + _ => AnyAmbiguousAliasesT::NONE, + }; + let vector_of_enums = self.vector_of_enums().map(|x| { + x.into_iter().collect() + }); + let signed_enum = self.signed_enum(); + let testrequirednestedflatbuffer = self.testrequirednestedflatbuffer().map(|x| { + x.to_vec() + }); + let scalar_key_sorted_tables = self.scalar_key_sorted_tables().map(|x| { + x.iter().map(|t| t.unpack()).collect() + }); + MonsterT { + pos, + mana, + hp, + name, + inventory, + color, + test, + test4, + testarrayofstring, + testarrayoftables, + enemy, + testnestedflatbuffer, + testempty, + testbool, + testhashs32_fnv1, + testhashu32_fnv1, + testhashs64_fnv1, + testhashu64_fnv1, + testhashs32_fnv1a, + testhashu32_fnv1a, + testhashs64_fnv1a, + testhashu64_fnv1a, + testarrayofbools, + testf, + testf2, + testf3, + testarrayofstring2, + testarrayofsortedstruct, + flex, + test5, + vector_of_longs, + vector_of_doubles, + parent_namespace_test, + vector_of_referrables, + single_weak_reference, + vector_of_weak_references, + vector_of_strong_referrables, + co_owning_reference, + vector_of_co_owning_references, + non_owning_reference, + vector_of_non_owning_references, + any_unique, + any_ambiguous, + vector_of_enums, + signed_enum, + testrequirednestedflatbuffer, + scalar_key_sorted_tables, + } + } + 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_TEST_TYPE: flatbuffers::VOffsetT = 18; + pub const VT_TEST: flatbuffers::VOffsetT = 20; + pub const VT_TEST4: flatbuffers::VOffsetT = 22; + pub const VT_TESTARRAYOFSTRING: flatbuffers::VOffsetT = 24; + pub const VT_TESTARRAYOFTABLES: flatbuffers::VOffsetT = 26; + pub const VT_ENEMY: flatbuffers::VOffsetT = 28; + pub const VT_TESTNESTEDFLATBUFFER: flatbuffers::VOffsetT = 30; + pub const VT_TESTEMPTY: flatbuffers::VOffsetT = 32; + pub const VT_TESTBOOL: flatbuffers::VOffsetT = 34; + pub const VT_TESTHASHS32_FNV1: flatbuffers::VOffsetT = 36; + pub const VT_TESTHASHU32_FNV1: flatbuffers::VOffsetT = 38; + pub const VT_TESTHASHS64_FNV1: flatbuffers::VOffsetT = 40; + pub const VT_TESTHASHU64_FNV1: flatbuffers::VOffsetT = 42; + pub const VT_TESTHASHS32_FNV1A: flatbuffers::VOffsetT = 44; + pub const VT_TESTHASHU32_FNV1A: flatbuffers::VOffsetT = 46; + pub const VT_TESTHASHS64_FNV1A: flatbuffers::VOffsetT = 48; + pub const VT_TESTHASHU64_FNV1A: flatbuffers::VOffsetT = 50; + pub const VT_TESTARRAYOFBOOLS: flatbuffers::VOffsetT = 52; + pub const VT_TESTF: flatbuffers::VOffsetT = 54; + pub const VT_TESTF2: flatbuffers::VOffsetT = 56; + pub const VT_TESTF3: flatbuffers::VOffsetT = 58; + pub const VT_TESTARRAYOFSTRING2: flatbuffers::VOffsetT = 60; + pub const VT_TESTARRAYOFSORTEDSTRUCT: flatbuffers::VOffsetT = 62; + pub const VT_FLEX: flatbuffers::VOffsetT = 64; + pub const VT_TEST5: flatbuffers::VOffsetT = 66; + pub const VT_VECTOR_OF_LONGS: flatbuffers::VOffsetT = 68; + pub const VT_VECTOR_OF_DOUBLES: flatbuffers::VOffsetT = 70; + pub const VT_PARENT_NAMESPACE_TEST: flatbuffers::VOffsetT = 72; + pub const VT_VECTOR_OF_REFERRABLES: flatbuffers::VOffsetT = 74; + pub const VT_SINGLE_WEAK_REFERENCE: flatbuffers::VOffsetT = 76; + pub const VT_VECTOR_OF_WEAK_REFERENCES: flatbuffers::VOffsetT = 78; + pub const VT_VECTOR_OF_STRONG_REFERRABLES: flatbuffers::VOffsetT = 80; + pub const VT_CO_OWNING_REFERENCE: flatbuffers::VOffsetT = 82; + pub const VT_VECTOR_OF_CO_OWNING_REFERENCES: flatbuffers::VOffsetT = 84; + pub const VT_NON_OWNING_REFERENCE: flatbuffers::VOffsetT = 86; + pub const VT_VECTOR_OF_NON_OWNING_REFERENCES: flatbuffers::VOffsetT = 88; + pub const VT_ANY_UNIQUE_TYPE: flatbuffers::VOffsetT = 90; + pub const VT_ANY_UNIQUE: flatbuffers::VOffsetT = 92; + pub const VT_ANY_AMBIGUOUS_TYPE: flatbuffers::VOffsetT = 94; + pub const VT_ANY_AMBIGUOUS: flatbuffers::VOffsetT = 96; + pub const VT_VECTOR_OF_ENUMS: flatbuffers::VOffsetT = 98; + pub const VT_SIGNED_ENUM: flatbuffers::VOffsetT = 100; + pub const VT_TESTREQUIREDNESTEDFLATBUFFER: flatbuffers::VOffsetT = 102; + pub const VT_SCALAR_KEY_SORTED_TABLES: flatbuffers::VOffsetT = 104; + + #[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) -> &'a str { + self._tab.get::>(Monster::VT_NAME, None).unwrap() + } + #[inline] + pub fn key_compare_less_than(&self, o: &Monster) -> bool { + self.name() < o.name() + } + + #[inline] + pub fn key_compare_with_value(&self, val: & str) -> ::std::cmp::Ordering { + let key = self.name(); + key.cmp(val) + } + #[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 test_type(&self) -> Any { + self._tab.get::(Monster::VT_TEST_TYPE, Some(Any::NONE)).unwrap() + } + #[inline] + pub fn test(&self) -> Option> { + self._tab.get::>>(Monster::VT_TEST, None) + } + #[inline] + pub fn test4(&self) -> Option<&'a [Test]> { + self._tab.get::>>(Monster::VT_TEST4, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn testarrayofstring(&self) -> Option>> { + self._tab.get::>>>(Monster::VT_TESTARRAYOFSTRING, None) + } + /// an example documentation comment: this will end up in the generated code + /// multiline too + #[inline] + pub fn testarrayoftables(&self) -> Option>>> { + self._tab.get::>>>(Monster::VT_TESTARRAYOFTABLES, None) + } + #[inline] + pub fn enemy(&self) -> Option> { + self._tab.get::>(Monster::VT_ENEMY, None) + } + #[inline] + pub fn testnestedflatbuffer(&self) -> Option<&'a [u8]> { + self._tab.get::>>(Monster::VT_TESTNESTEDFLATBUFFER, None).map(|v| v.safe_slice()) + } + pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option> { + self.testnestedflatbuffer().map(|data| { + use flatbuffers::Follow; + >>::follow(data, 0) + }) + } + #[inline] + pub fn testempty(&self) -> Option> { + self._tab.get::>(Monster::VT_TESTEMPTY, None) + } + #[inline] + pub fn testbool(&self) -> bool { + self._tab.get::(Monster::VT_TESTBOOL, Some(false)).unwrap() + } + #[inline] + pub fn testhashs32_fnv1(&self) -> i32 { + self._tab.get::(Monster::VT_TESTHASHS32_FNV1, Some(0)).unwrap() + } + #[inline] + pub fn testhashu32_fnv1(&self) -> u32 { + self._tab.get::(Monster::VT_TESTHASHU32_FNV1, Some(0)).unwrap() + } + #[inline] + pub fn testhashs64_fnv1(&self) -> i64 { + self._tab.get::(Monster::VT_TESTHASHS64_FNV1, Some(0)).unwrap() + } + #[inline] + pub fn testhashu64_fnv1(&self) -> u64 { + self._tab.get::(Monster::VT_TESTHASHU64_FNV1, Some(0)).unwrap() + } + #[inline] + pub fn testhashs32_fnv1a(&self) -> i32 { + self._tab.get::(Monster::VT_TESTHASHS32_FNV1A, Some(0)).unwrap() + } + #[inline] + pub fn testhashu32_fnv1a(&self) -> u32 { + self._tab.get::(Monster::VT_TESTHASHU32_FNV1A, Some(0)).unwrap() + } + #[inline] + pub fn testhashs64_fnv1a(&self) -> i64 { + self._tab.get::(Monster::VT_TESTHASHS64_FNV1A, Some(0)).unwrap() + } + #[inline] + pub fn testhashu64_fnv1a(&self) -> u64 { + self._tab.get::(Monster::VT_TESTHASHU64_FNV1A, Some(0)).unwrap() + } + #[inline] + pub fn testarrayofbools(&self) -> Option<&'a [bool]> { + self._tab.get::>>(Monster::VT_TESTARRAYOFBOOLS, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn testf(&self) -> f32 { + self._tab.get::(Monster::VT_TESTF, Some(3.14159)).unwrap() + } + #[inline] + pub fn testf2(&self) -> f32 { + self._tab.get::(Monster::VT_TESTF2, Some(3.0)).unwrap() + } + #[inline] + pub fn testf3(&self) -> f32 { + self._tab.get::(Monster::VT_TESTF3, Some(0.0)).unwrap() + } + #[inline] + pub fn testarrayofstring2(&self) -> Option>> { + self._tab.get::>>>(Monster::VT_TESTARRAYOFSTRING2, None) + } + #[inline] + pub fn testarrayofsortedstruct(&self) -> Option<&'a [Ability]> { + self._tab.get::>>(Monster::VT_TESTARRAYOFSORTEDSTRUCT, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn flex(&self) -> Option<&'a [u8]> { + self._tab.get::>>(Monster::VT_FLEX, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn test5(&self) -> Option<&'a [Test]> { + self._tab.get::>>(Monster::VT_TEST5, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn vector_of_longs(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_LONGS, None) + } + #[inline] + pub fn vector_of_doubles(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_DOUBLES, None) + } + #[inline] + pub fn parent_namespace_test(&self) -> Option> { + self._tab.get::>(Monster::VT_PARENT_NAMESPACE_TEST, None) + } + #[inline] + pub fn vector_of_referrables(&self) -> Option>>> { + self._tab.get::>>>(Monster::VT_VECTOR_OF_REFERRABLES, None) + } + #[inline] + pub fn single_weak_reference(&self) -> u64 { + self._tab.get::(Monster::VT_SINGLE_WEAK_REFERENCE, Some(0)).unwrap() + } + #[inline] + pub fn vector_of_weak_references(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_WEAK_REFERENCES, None) + } + #[inline] + pub fn vector_of_strong_referrables(&self) -> Option>>> { + self._tab.get::>>>(Monster::VT_VECTOR_OF_STRONG_REFERRABLES, None) + } + #[inline] + pub fn co_owning_reference(&self) -> u64 { + self._tab.get::(Monster::VT_CO_OWNING_REFERENCE, Some(0)).unwrap() + } + #[inline] + pub fn vector_of_co_owning_references(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_CO_OWNING_REFERENCES, None) + } + #[inline] + pub fn non_owning_reference(&self) -> u64 { + self._tab.get::(Monster::VT_NON_OWNING_REFERENCE, Some(0)).unwrap() + } + #[inline] + pub fn vector_of_non_owning_references(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, None) + } + #[inline] + pub fn any_unique_type(&self) -> AnyUniqueAliases { + self._tab.get::(Monster::VT_ANY_UNIQUE_TYPE, Some(AnyUniqueAliases::NONE)).unwrap() + } + #[inline] + pub fn any_unique(&self) -> Option> { + self._tab.get::>>(Monster::VT_ANY_UNIQUE, None) + } + #[inline] + pub fn any_ambiguous_type(&self) -> AnyAmbiguousAliases { + self._tab.get::(Monster::VT_ANY_AMBIGUOUS_TYPE, Some(AnyAmbiguousAliases::NONE)).unwrap() + } + #[inline] + pub fn any_ambiguous(&self) -> Option> { + self._tab.get::>>(Monster::VT_ANY_AMBIGUOUS, None) + } + #[inline] + pub fn vector_of_enums(&self) -> Option> { + self._tab.get::>>(Monster::VT_VECTOR_OF_ENUMS, None) + } + #[inline] + pub fn signed_enum(&self) -> Race { + self._tab.get::(Monster::VT_SIGNED_ENUM, Some(Race::None)).unwrap() + } + #[inline] + pub fn testrequirednestedflatbuffer(&self) -> Option<&'a [u8]> { + self._tab.get::>>(Monster::VT_TESTREQUIREDNESTEDFLATBUFFER, None).map(|v| v.safe_slice()) + } + pub fn testrequirednestedflatbuffer_nested_flatbuffer(&'a self) -> Option> { + self.testrequirednestedflatbuffer().map(|data| { + use flatbuffers::Follow; + >>::follow(data, 0) + }) + } + #[inline] + pub fn scalar_key_sorted_tables(&self) -> Option>>> { + self._tab.get::>>>(Monster::VT_SCALAR_KEY_SORTED_TABLES, None) + } + #[inline] + #[allow(non_snake_case)] + pub fn test_as_monster(&self) -> Option> { + if self.test_type() == Any::Monster { + self.test().map(Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn test_as_test_simple_table_with_enum(&self) -> Option> { + if self.test_type() == Any::TestSimpleTableWithEnum { + self.test().map(TestSimpleTableWithEnum::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn test_as_my_game_example_2_monster(&self) -> Option> { + if self.test_type() == Any::MyGame_Example2_Monster { + self.test().map(super::example_2::Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_m(&self) -> Option> { + if self.any_unique_type() == AnyUniqueAliases::M { + self.any_unique().map(Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_ts(&self) -> Option> { + if self.any_unique_type() == AnyUniqueAliases::TS { + self.any_unique().map(TestSimpleTableWithEnum::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_unique_as_m2(&self) -> Option> { + if self.any_unique_type() == AnyUniqueAliases::M2 { + self.any_unique().map(super::example_2::Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m1(&self) -> Option> { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M1 { + self.any_ambiguous().map(Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m2(&self) -> Option> { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M2 { + self.any_ambiguous().map(Monster::init_from_table) + } else { + None + } + } + + #[inline] + #[allow(non_snake_case)] + pub fn any_ambiguous_as_m3(&self) -> Option> { + if self.any_ambiguous_type() == AnyAmbiguousAliases::M3 { + self.any_ambiguous().map(Monster::init_from_table) + } else { + None + } + } + +} + +impl flatbuffers::Verifiable for Monster<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("pos", Self::VT_POS, false)? + .visit_field::("mana", Self::VT_MANA, false)? + .visit_field::("hp", Self::VT_HP, false)? + .visit_field::>("name", Self::VT_NAME, true)? + .visit_field::>>("inventory", Self::VT_INVENTORY, false)? + .visit_field::("color", Self::VT_COLOR, false)? + .visit_union::("test_type", Self::VT_TEST_TYPE, "test", Self::VT_TEST, false, |key, v, pos| { + match key { + Any::Monster => v.verify_union_variant::>("Any::Monster", pos), + Any::TestSimpleTableWithEnum => v.verify_union_variant::>("Any::TestSimpleTableWithEnum", pos), + Any::MyGame_Example2_Monster => v.verify_union_variant::>("Any::MyGame_Example2_Monster", pos), + _ => Ok(()), + } + })? + .visit_field::>>("test4", Self::VT_TEST4, false)? + .visit_field::>>>("testarrayofstring", Self::VT_TESTARRAYOFSTRING, false)? + .visit_field::>>>("testarrayoftables", Self::VT_TESTARRAYOFTABLES, false)? + .visit_field::>("enemy", Self::VT_ENEMY, false)? + .visit_field::>>("testnestedflatbuffer", Self::VT_TESTNESTEDFLATBUFFER, false)? + .visit_field::>("testempty", Self::VT_TESTEMPTY, false)? + .visit_field::("testbool", Self::VT_TESTBOOL, false)? + .visit_field::("testhashs32_fnv1", Self::VT_TESTHASHS32_FNV1, false)? + .visit_field::("testhashu32_fnv1", Self::VT_TESTHASHU32_FNV1, false)? + .visit_field::("testhashs64_fnv1", Self::VT_TESTHASHS64_FNV1, false)? + .visit_field::("testhashu64_fnv1", Self::VT_TESTHASHU64_FNV1, false)? + .visit_field::("testhashs32_fnv1a", Self::VT_TESTHASHS32_FNV1A, false)? + .visit_field::("testhashu32_fnv1a", Self::VT_TESTHASHU32_FNV1A, false)? + .visit_field::("testhashs64_fnv1a", Self::VT_TESTHASHS64_FNV1A, false)? + .visit_field::("testhashu64_fnv1a", Self::VT_TESTHASHU64_FNV1A, false)? + .visit_field::>>("testarrayofbools", Self::VT_TESTARRAYOFBOOLS, false)? + .visit_field::("testf", Self::VT_TESTF, false)? + .visit_field::("testf2", Self::VT_TESTF2, false)? + .visit_field::("testf3", Self::VT_TESTF3, false)? + .visit_field::>>>("testarrayofstring2", Self::VT_TESTARRAYOFSTRING2, false)? + .visit_field::>>("testarrayofsortedstruct", Self::VT_TESTARRAYOFSORTEDSTRUCT, false)? + .visit_field::>>("flex", Self::VT_FLEX, false)? + .visit_field::>>("test5", Self::VT_TEST5, false)? + .visit_field::>>("vector_of_longs", Self::VT_VECTOR_OF_LONGS, false)? + .visit_field::>>("vector_of_doubles", Self::VT_VECTOR_OF_DOUBLES, false)? + .visit_field::>("parent_namespace_test", Self::VT_PARENT_NAMESPACE_TEST, false)? + .visit_field::>>>("vector_of_referrables", Self::VT_VECTOR_OF_REFERRABLES, false)? + .visit_field::("single_weak_reference", Self::VT_SINGLE_WEAK_REFERENCE, false)? + .visit_field::>>("vector_of_weak_references", Self::VT_VECTOR_OF_WEAK_REFERENCES, false)? + .visit_field::>>>("vector_of_strong_referrables", Self::VT_VECTOR_OF_STRONG_REFERRABLES, false)? + .visit_field::("co_owning_reference", Self::VT_CO_OWNING_REFERENCE, false)? + .visit_field::>>("vector_of_co_owning_references", Self::VT_VECTOR_OF_CO_OWNING_REFERENCES, false)? + .visit_field::("non_owning_reference", Self::VT_NON_OWNING_REFERENCE, false)? + .visit_field::>>("vector_of_non_owning_references", Self::VT_VECTOR_OF_NON_OWNING_REFERENCES, false)? + .visit_union::("any_unique_type", Self::VT_ANY_UNIQUE_TYPE, "any_unique", Self::VT_ANY_UNIQUE, false, |key, v, pos| { + match key { + AnyUniqueAliases::M => v.verify_union_variant::>("AnyUniqueAliases::M", pos), + AnyUniqueAliases::TS => v.verify_union_variant::>("AnyUniqueAliases::TS", pos), + AnyUniqueAliases::M2 => v.verify_union_variant::>("AnyUniqueAliases::M2", pos), + _ => Ok(()), + } + })? + .visit_union::("any_ambiguous_type", Self::VT_ANY_AMBIGUOUS_TYPE, "any_ambiguous", Self::VT_ANY_AMBIGUOUS, false, |key, v, pos| { + match key { + AnyAmbiguousAliases::M1 => v.verify_union_variant::>("AnyAmbiguousAliases::M1", pos), + AnyAmbiguousAliases::M2 => v.verify_union_variant::>("AnyAmbiguousAliases::M2", pos), + AnyAmbiguousAliases::M3 => v.verify_union_variant::>("AnyAmbiguousAliases::M3", pos), + _ => Ok(()), + } + })? + .visit_field::>>("vector_of_enums", Self::VT_VECTOR_OF_ENUMS, false)? + .visit_field::("signed_enum", Self::VT_SIGNED_ENUM, false)? + .visit_field::>>("testrequirednestedflatbuffer", Self::VT_TESTREQUIREDNESTEDFLATBUFFER, false)? + .visit_field::>>>("scalar_key_sorted_tables", Self::VT_SCALAR_KEY_SORTED_TABLES, false)? + .finish(); + Ok(()) + } +} +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 test_type: Any, + pub test: Option>, + pub test4: Option>>, + pub testarrayofstring: Option>>>, + pub testarrayoftables: Option>>>>, + pub enemy: Option>>, + pub testnestedflatbuffer: Option>>, + pub testempty: Option>>, + pub testbool: bool, + pub testhashs32_fnv1: i32, + pub testhashu32_fnv1: u32, + pub testhashs64_fnv1: i64, + pub testhashu64_fnv1: u64, + pub testhashs32_fnv1a: i32, + pub testhashu32_fnv1a: u32, + pub testhashs64_fnv1a: i64, + pub testhashu64_fnv1a: u64, + pub testarrayofbools: Option>>, + pub testf: f32, + pub testf2: f32, + pub testf3: f32, + pub testarrayofstring2: Option>>>, + pub testarrayofsortedstruct: Option>>, + pub flex: Option>>, + pub test5: Option>>, + pub vector_of_longs: Option>>, + pub vector_of_doubles: Option>>, + pub parent_namespace_test: Option>>, + pub vector_of_referrables: Option>>>>, + pub single_weak_reference: u64, + pub vector_of_weak_references: Option>>, + pub vector_of_strong_referrables: Option>>>>, + pub co_owning_reference: u64, + pub vector_of_co_owning_references: Option>>, + pub non_owning_reference: u64, + pub vector_of_non_owning_references: Option>>, + pub any_unique_type: AnyUniqueAliases, + pub any_unique: Option>, + pub any_ambiguous_type: AnyAmbiguousAliases, + pub any_ambiguous: Option>, + pub vector_of_enums: Option>>, + pub signed_enum: Race, + pub testrequirednestedflatbuffer: Option>>, + pub scalar_key_sorted_tables: Option>>>>, +} +impl<'a> Default for MonsterArgs<'a> { + #[inline] + fn default() -> Self { + MonsterArgs { + pos: None, + mana: 150, + hp: 100, + name: None, // required field + inventory: None, + color: Color::Blue, + test_type: Any::NONE, + test: None, + test4: None, + testarrayofstring: None, + testarrayoftables: None, + enemy: None, + testnestedflatbuffer: None, + testempty: None, + testbool: false, + testhashs32_fnv1: 0, + testhashu32_fnv1: 0, + testhashs64_fnv1: 0, + testhashu64_fnv1: 0, + testhashs32_fnv1a: 0, + testhashu32_fnv1a: 0, + testhashs64_fnv1a: 0, + testhashu64_fnv1a: 0, + testarrayofbools: None, + testf: 3.14159, + testf2: 3.0, + testf3: 0.0, + testarrayofstring2: None, + testarrayofsortedstruct: None, + flex: None, + test5: None, + vector_of_longs: None, + vector_of_doubles: None, + parent_namespace_test: None, + vector_of_referrables: None, + single_weak_reference: 0, + vector_of_weak_references: None, + vector_of_strong_referrables: None, + co_owning_reference: 0, + vector_of_co_owning_references: None, + non_owning_reference: 0, + vector_of_non_owning_references: None, + any_unique_type: AnyUniqueAliases::NONE, + any_unique: None, + any_ambiguous_type: AnyAmbiguousAliases::NONE, + any_ambiguous: None, + vector_of_enums: None, + signed_enum: Race::None, + testrequirednestedflatbuffer: None, + scalar_key_sorted_tables: 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: &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_test_type(&mut self, test_type: Any) { + self.fbb_.push_slot::(Monster::VT_TEST_TYPE, test_type, Any::NONE); + } + #[inline] + pub fn add_test(&mut self, test: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_TEST, test); + } + #[inline] + pub fn add_test4(&mut self, test4: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TEST4, test4); + } + #[inline] + pub fn add_testarrayofstring(&mut self, testarrayofstring: flatbuffers::WIPOffset>>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSTRING, testarrayofstring); + } + #[inline] + pub fn add_testarrayoftables(&mut self, testarrayoftables: flatbuffers::WIPOffset>>>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFTABLES, testarrayoftables); + } + #[inline] + pub fn add_enemy(&mut self, enemy: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_ENEMY, enemy); + } + #[inline] + pub fn add_testnestedflatbuffer(&mut self, testnestedflatbuffer: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTNESTEDFLATBUFFER, testnestedflatbuffer); + } + #[inline] + pub fn add_testempty(&mut self, testempty: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTEMPTY, testempty); + } + #[inline] + pub fn add_testbool(&mut self, testbool: bool) { + self.fbb_.push_slot::(Monster::VT_TESTBOOL, testbool, false); + } + #[inline] + pub fn add_testhashs32_fnv1(&mut self, testhashs32_fnv1: i32) { + self.fbb_.push_slot::(Monster::VT_TESTHASHS32_FNV1, testhashs32_fnv1, 0); + } + #[inline] + pub fn add_testhashu32_fnv1(&mut self, testhashu32_fnv1: u32) { + self.fbb_.push_slot::(Monster::VT_TESTHASHU32_FNV1, testhashu32_fnv1, 0); + } + #[inline] + pub fn add_testhashs64_fnv1(&mut self, testhashs64_fnv1: i64) { + self.fbb_.push_slot::(Monster::VT_TESTHASHS64_FNV1, testhashs64_fnv1, 0); + } + #[inline] + pub fn add_testhashu64_fnv1(&mut self, testhashu64_fnv1: u64) { + self.fbb_.push_slot::(Monster::VT_TESTHASHU64_FNV1, testhashu64_fnv1, 0); + } + #[inline] + pub fn add_testhashs32_fnv1a(&mut self, testhashs32_fnv1a: i32) { + self.fbb_.push_slot::(Monster::VT_TESTHASHS32_FNV1A, testhashs32_fnv1a, 0); + } + #[inline] + pub fn add_testhashu32_fnv1a(&mut self, testhashu32_fnv1a: u32) { + self.fbb_.push_slot::(Monster::VT_TESTHASHU32_FNV1A, testhashu32_fnv1a, 0); + } + #[inline] + pub fn add_testhashs64_fnv1a(&mut self, testhashs64_fnv1a: i64) { + self.fbb_.push_slot::(Monster::VT_TESTHASHS64_FNV1A, testhashs64_fnv1a, 0); + } + #[inline] + pub fn add_testhashu64_fnv1a(&mut self, testhashu64_fnv1a: u64) { + self.fbb_.push_slot::(Monster::VT_TESTHASHU64_FNV1A, testhashu64_fnv1a, 0); + } + #[inline] + pub fn add_testarrayofbools(&mut self, testarrayofbools: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFBOOLS, testarrayofbools); + } + #[inline] + pub fn add_testf(&mut self, testf: f32) { + self.fbb_.push_slot::(Monster::VT_TESTF, testf, 3.14159); + } + #[inline] + pub fn add_testf2(&mut self, testf2: f32) { + self.fbb_.push_slot::(Monster::VT_TESTF2, testf2, 3.0); + } + #[inline] + pub fn add_testf3(&mut self, testf3: f32) { + self.fbb_.push_slot::(Monster::VT_TESTF3, testf3, 0.0); + } + #[inline] + pub fn add_testarrayofstring2(&mut self, testarrayofstring2: flatbuffers::WIPOffset>>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSTRING2, testarrayofstring2); + } + #[inline] + pub fn add_testarrayofsortedstruct(&mut self, testarrayofsortedstruct: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSORTEDSTRUCT, testarrayofsortedstruct); + } + #[inline] + pub fn add_flex(&mut self, flex: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_FLEX, flex); + } + #[inline] + pub fn add_test5(&mut self, test5: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TEST5, test5); + } + #[inline] + pub fn add_vector_of_longs(&mut self, vector_of_longs: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_LONGS, vector_of_longs); + } + #[inline] + pub fn add_vector_of_doubles(&mut self, vector_of_doubles: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_DOUBLES, vector_of_doubles); + } + #[inline] + pub fn add_parent_namespace_test(&mut self, parent_namespace_test: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_PARENT_NAMESPACE_TEST, parent_namespace_test); + } + #[inline] + pub fn add_vector_of_referrables(&mut self, vector_of_referrables: flatbuffers::WIPOffset>>>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_REFERRABLES, vector_of_referrables); + } + #[inline] + pub fn add_single_weak_reference(&mut self, single_weak_reference: u64) { + self.fbb_.push_slot::(Monster::VT_SINGLE_WEAK_REFERENCE, single_weak_reference, 0); + } + #[inline] + pub fn add_vector_of_weak_references(&mut self, vector_of_weak_references: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_WEAK_REFERENCES, vector_of_weak_references); + } + #[inline] + pub fn add_vector_of_strong_referrables(&mut self, vector_of_strong_referrables: flatbuffers::WIPOffset>>>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_STRONG_REFERRABLES, vector_of_strong_referrables); + } + #[inline] + pub fn add_co_owning_reference(&mut self, co_owning_reference: u64) { + self.fbb_.push_slot::(Monster::VT_CO_OWNING_REFERENCE, co_owning_reference, 0); + } + #[inline] + pub fn add_vector_of_co_owning_references(&mut self, vector_of_co_owning_references: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_CO_OWNING_REFERENCES, vector_of_co_owning_references); + } + #[inline] + pub fn add_non_owning_reference(&mut self, non_owning_reference: u64) { + self.fbb_.push_slot::(Monster::VT_NON_OWNING_REFERENCE, non_owning_reference, 0); + } + #[inline] + pub fn add_vector_of_non_owning_references(&mut self, vector_of_non_owning_references: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references); + } + #[inline] + pub fn add_any_unique_type(&mut self, any_unique_type: AnyUniqueAliases) { + self.fbb_.push_slot::(Monster::VT_ANY_UNIQUE_TYPE, any_unique_type, AnyUniqueAliases::NONE); + } + #[inline] + pub fn add_any_unique(&mut self, any_unique: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_ANY_UNIQUE, any_unique); + } + #[inline] + pub fn add_any_ambiguous_type(&mut self, any_ambiguous_type: AnyAmbiguousAliases) { + self.fbb_.push_slot::(Monster::VT_ANY_AMBIGUOUS_TYPE, any_ambiguous_type, AnyAmbiguousAliases::NONE); + } + #[inline] + pub fn add_any_ambiguous(&mut self, any_ambiguous: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); + } + #[inline] + pub fn add_vector_of_enums(&mut self, vector_of_enums: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_ENUMS, vector_of_enums); + } + #[inline] + pub fn add_signed_enum(&mut self, signed_enum: Race) { + self.fbb_.push_slot::(Monster::VT_SIGNED_ENUM, signed_enum, Race::None); + } + #[inline] + pub fn add_testrequirednestedflatbuffer(&mut self, testrequirednestedflatbuffer: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(Monster::VT_TESTREQUIREDNESTEDFLATBUFFER, testrequirednestedflatbuffer); + } + #[inline] + pub fn add_scalar_key_sorted_tables(&mut self, scalar_key_sorted_tables: flatbuffers::WIPOffset>>>) { + self.fbb_.push_slot_always::>(Monster::VT_SCALAR_KEY_SORTED_TABLES, scalar_key_sorted_tables); + } + #[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_); + self.fbb_.required(o, Monster::VT_NAME,"name"); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for Monster<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Monster"); + ds.field("pos", &self.pos()); + ds.field("mana", &self.mana()); + ds.field("hp", &self.hp()); + ds.field("name", &self.name()); + ds.field("inventory", &self.inventory()); + ds.field("color", &self.color()); + ds.field("test_type", &self.test_type()); + match self.test_type() { + Any::Monster => { + if let Some(x) = self.test_as_monster() { + ds.field("test", &x) + } else { + ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + Any::TestSimpleTableWithEnum => { + if let Some(x) = self.test_as_test_simple_table_with_enum() { + ds.field("test", &x) + } else { + ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + Any::MyGame_Example2_Monster => { + if let Some(x) = self.test_as_my_game_example_2_monster() { + ds.field("test", &x) + } else { + ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("test", &x) + }, + }; + ds.field("test4", &self.test4()); + ds.field("testarrayofstring", &self.testarrayofstring()); + ds.field("testarrayoftables", &self.testarrayoftables()); + ds.field("enemy", &self.enemy()); + ds.field("testnestedflatbuffer", &self.testnestedflatbuffer()); + ds.field("testempty", &self.testempty()); + ds.field("testbool", &self.testbool()); + ds.field("testhashs32_fnv1", &self.testhashs32_fnv1()); + ds.field("testhashu32_fnv1", &self.testhashu32_fnv1()); + ds.field("testhashs64_fnv1", &self.testhashs64_fnv1()); + ds.field("testhashu64_fnv1", &self.testhashu64_fnv1()); + ds.field("testhashs32_fnv1a", &self.testhashs32_fnv1a()); + ds.field("testhashu32_fnv1a", &self.testhashu32_fnv1a()); + ds.field("testhashs64_fnv1a", &self.testhashs64_fnv1a()); + ds.field("testhashu64_fnv1a", &self.testhashu64_fnv1a()); + ds.field("testarrayofbools", &self.testarrayofbools()); + ds.field("testf", &self.testf()); + ds.field("testf2", &self.testf2()); + ds.field("testf3", &self.testf3()); + ds.field("testarrayofstring2", &self.testarrayofstring2()); + ds.field("testarrayofsortedstruct", &self.testarrayofsortedstruct()); + ds.field("flex", &self.flex()); + ds.field("test5", &self.test5()); + ds.field("vector_of_longs", &self.vector_of_longs()); + ds.field("vector_of_doubles", &self.vector_of_doubles()); + ds.field("parent_namespace_test", &self.parent_namespace_test()); + ds.field("vector_of_referrables", &self.vector_of_referrables()); + ds.field("single_weak_reference", &self.single_weak_reference()); + ds.field("vector_of_weak_references", &self.vector_of_weak_references()); + ds.field("vector_of_strong_referrables", &self.vector_of_strong_referrables()); + ds.field("co_owning_reference", &self.co_owning_reference()); + ds.field("vector_of_co_owning_references", &self.vector_of_co_owning_references()); + ds.field("non_owning_reference", &self.non_owning_reference()); + ds.field("vector_of_non_owning_references", &self.vector_of_non_owning_references()); + ds.field("any_unique_type", &self.any_unique_type()); + match self.any_unique_type() { + AnyUniqueAliases::M => { + if let Some(x) = self.any_unique_as_m() { + ds.field("any_unique", &x) + } else { + ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + AnyUniqueAliases::TS => { + if let Some(x) = self.any_unique_as_ts() { + ds.field("any_unique", &x) + } else { + ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + AnyUniqueAliases::M2 => { + if let Some(x) = self.any_unique_as_m2() { + ds.field("any_unique", &x) + } else { + ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("any_unique", &x) + }, + }; + ds.field("any_ambiguous_type", &self.any_ambiguous_type()); + match self.any_ambiguous_type() { + AnyAmbiguousAliases::M1 => { + if let Some(x) = self.any_ambiguous_as_m1() { + ds.field("any_ambiguous", &x) + } else { + ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + AnyAmbiguousAliases::M2 => { + if let Some(x) = self.any_ambiguous_as_m2() { + ds.field("any_ambiguous", &x) + } else { + ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + AnyAmbiguousAliases::M3 => { + if let Some(x) = self.any_ambiguous_as_m3() { + ds.field("any_ambiguous", &x) + } else { + ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("any_ambiguous", &x) + }, + }; + ds.field("vector_of_enums", &self.vector_of_enums()); + ds.field("signed_enum", &self.signed_enum()); + ds.field("testrequirednestedflatbuffer", &self.testrequirednestedflatbuffer()); + ds.field("scalar_key_sorted_tables", &self.scalar_key_sorted_tables()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct MonsterT { + pub pos: Option, + pub mana: i16, + pub hp: i16, + pub name: String, + pub inventory: Option>, + pub color: Color, + pub test: AnyT, + pub test4: Option>, + pub testarrayofstring: Option>, + pub testarrayoftables: Option>, + pub enemy: Option>, + pub testnestedflatbuffer: Option>, + pub testempty: Option>, + pub testbool: bool, + pub testhashs32_fnv1: i32, + pub testhashu32_fnv1: u32, + pub testhashs64_fnv1: i64, + pub testhashu64_fnv1: u64, + pub testhashs32_fnv1a: i32, + pub testhashu32_fnv1a: u32, + pub testhashs64_fnv1a: i64, + pub testhashu64_fnv1a: u64, + pub testarrayofbools: Option>, + pub testf: f32, + pub testf2: f32, + pub testf3: f32, + pub testarrayofstring2: Option>, + pub testarrayofsortedstruct: Option>, + pub flex: Option>, + pub test5: Option>, + pub vector_of_longs: Option>, + pub vector_of_doubles: Option>, + pub parent_namespace_test: Option>, + pub vector_of_referrables: Option>, + pub single_weak_reference: u64, + pub vector_of_weak_references: Option>, + pub vector_of_strong_referrables: Option>, + pub co_owning_reference: u64, + pub vector_of_co_owning_references: Option>, + pub non_owning_reference: u64, + pub vector_of_non_owning_references: Option>, + pub any_unique: AnyUniqueAliasesT, + pub any_ambiguous: AnyAmbiguousAliasesT, + pub vector_of_enums: Option>, + pub signed_enum: Race, + pub testrequirednestedflatbuffer: Option>, + pub scalar_key_sorted_tables: Option>, +} +impl Default for MonsterT { + fn default() -> Self { + Self { + pos: None, + mana: 150, + hp: 100, + name: "".to_string(), + inventory: None, + color: Color::Blue, + test: AnyT::NONE, + test4: None, + testarrayofstring: None, + testarrayoftables: None, + enemy: None, + testnestedflatbuffer: None, + testempty: None, + testbool: false, + testhashs32_fnv1: 0, + testhashu32_fnv1: 0, + testhashs64_fnv1: 0, + testhashu64_fnv1: 0, + testhashs32_fnv1a: 0, + testhashu32_fnv1a: 0, + testhashs64_fnv1a: 0, + testhashu64_fnv1a: 0, + testarrayofbools: None, + testf: 3.14159, + testf2: 3.0, + testf3: 0.0, + testarrayofstring2: None, + testarrayofsortedstruct: None, + flex: None, + test5: None, + vector_of_longs: None, + vector_of_doubles: None, + parent_namespace_test: None, + vector_of_referrables: None, + single_weak_reference: 0, + vector_of_weak_references: None, + vector_of_strong_referrables: None, + co_owning_reference: 0, + vector_of_co_owning_references: None, + non_owning_reference: 0, + vector_of_non_owning_references: None, + any_unique: AnyUniqueAliasesT::NONE, + any_ambiguous: AnyAmbiguousAliasesT::NONE, + vector_of_enums: None, + signed_enum: Race::None, + testrequirednestedflatbuffer: None, + scalar_key_sorted_tables: None, + } + } +} +impl MonsterT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let pos_tmp = self.pos.as_ref().map(|x| x.pack()); + let pos = pos_tmp.as_ref(); + let mana = self.mana; + let hp = self.hp; + let name = Some({ + let x = &self.name; + _fbb.create_string(x) + }); + let inventory = self.inventory.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let color = self.color; + let test_type = self.test.any_type(); + let test = self.test.pack(_fbb); + let test4 = self.test4.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) + }); + let testarrayofstring = self.testarrayofstring.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|s| s.as_ref()).collect();_fbb.create_vector_of_strings(&w) + }); + let testarrayoftables = self.testarrayoftables.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) + }); + let enemy = self.enemy.as_ref().map(|x|{ + x.pack(_fbb) + }); + let testnestedflatbuffer = self.testnestedflatbuffer.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let testempty = self.testempty.as_ref().map(|x|{ + x.pack(_fbb) + }); + let testbool = self.testbool; + let testhashs32_fnv1 = self.testhashs32_fnv1; + let testhashu32_fnv1 = self.testhashu32_fnv1; + let testhashs64_fnv1 = self.testhashs64_fnv1; + let testhashu64_fnv1 = self.testhashu64_fnv1; + let testhashs32_fnv1a = self.testhashs32_fnv1a; + let testhashu32_fnv1a = self.testhashu32_fnv1a; + let testhashs64_fnv1a = self.testhashs64_fnv1a; + let testhashu64_fnv1a = self.testhashu64_fnv1a; + let testarrayofbools = self.testarrayofbools.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let testf = self.testf; + let testf2 = self.testf2; + let testf3 = self.testf3; + let testarrayofstring2 = self.testarrayofstring2.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|s| s.as_ref()).collect();_fbb.create_vector_of_strings(&w) + }); + let testarrayofsortedstruct = self.testarrayofsortedstruct.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) + }); + let flex = self.flex.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let test5 = self.test5.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) + }); + let vector_of_longs = self.vector_of_longs.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let vector_of_doubles = self.vector_of_doubles.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let parent_namespace_test = self.parent_namespace_test.as_ref().map(|x|{ + x.pack(_fbb) + }); + let vector_of_referrables = self.vector_of_referrables.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) + }); + let single_weak_reference = self.single_weak_reference; + let vector_of_weak_references = self.vector_of_weak_references.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let vector_of_strong_referrables = self.vector_of_strong_referrables.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) + }); + let co_owning_reference = self.co_owning_reference; + let vector_of_co_owning_references = self.vector_of_co_owning_references.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let non_owning_reference = self.non_owning_reference; + let vector_of_non_owning_references = self.vector_of_non_owning_references.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let any_unique_type = self.any_unique.any_unique_aliases_type(); + let any_unique = self.any_unique.pack(_fbb); + let any_ambiguous_type = self.any_ambiguous.any_ambiguous_aliases_type(); + let any_ambiguous = self.any_ambiguous.pack(_fbb); + let vector_of_enums = self.vector_of_enums.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let signed_enum = self.signed_enum; + let testrequirednestedflatbuffer = self.testrequirednestedflatbuffer.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let scalar_key_sorted_tables = self.scalar_key_sorted_tables.as_ref().map(|x|{ + let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) + }); + Monster::create(_fbb, &MonsterArgs{ + pos, + mana, + hp, + name, + inventory, + color, + test_type, + test, + test4, + testarrayofstring, + testarrayoftables, + enemy, + testnestedflatbuffer, + testempty, + testbool, + testhashs32_fnv1, + testhashu32_fnv1, + testhashs64_fnv1, + testhashu64_fnv1, + testhashs32_fnv1a, + testhashu32_fnv1a, + testhashs64_fnv1a, + testhashu64_fnv1a, + testarrayofbools, + testf, + testf2, + testf3, + testarrayofstring2, + testarrayofsortedstruct, + flex, + test5, + vector_of_longs, + vector_of_doubles, + parent_namespace_test, + vector_of_referrables, + single_weak_reference, + vector_of_weak_references, + vector_of_strong_referrables, + co_owning_reference, + vector_of_co_owning_references, + non_owning_reference, + vector_of_non_owning_references, + any_unique_type, + any_unique, + any_ambiguous_type, + any_ambiguous, + vector_of_enums, + signed_enum, + testrequirednestedflatbuffer, + scalar_key_sorted_tables, + }) + } +} +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + unsafe { flatbuffers::root_unchecked::>(buf) } +} + +#[inline] +#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] +pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { + unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } +} + +#[inline] +/// Verifies that a buffer of bytes contains a `Monster` +/// and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn root_as_monster(buf: &[u8]) -> Result { + flatbuffers::root::(buf) +} +#[inline] +/// Verifies that a buffer of bytes contains a size prefixed +/// `Monster` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `size_prefixed_root_as_monster_unchecked`. +pub fn size_prefixed_root_as_monster(buf: &[u8]) -> Result { + flatbuffers::size_prefixed_root::(buf) +} +#[inline] +/// Verifies, with the given options, that a buffer of bytes +/// contains a `Monster` and returns it. +/// Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn root_as_monster_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::root_with_opts::>(opts, buf) +} +#[inline] +/// Verifies, with the given verifier options, that a buffer of +/// bytes contains a size prefixed `Monster` and returns +/// it. Note that verification is still experimental and may not +/// catch every error, or be maximally performant. For the +/// previous, unchecked, behavior use +/// `root_as_monster_unchecked`. +pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>( + opts: &'o flatbuffers::VerifierOptions, + buf: &'b [u8], +) -> Result, flatbuffers::InvalidFlatbuffer> { + flatbuffers::size_prefixed_root_with_opts::>(opts, buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a Monster and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid `Monster`. +pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster { + flatbuffers::root_unchecked::(buf) +} +#[inline] +/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it. +/// # Safety +/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`. +pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster { + flatbuffers::size_prefixed_root_unchecked::(buf) +} +pub const MONSTER_IDENTIFIER: &str = "MONS"; + +#[inline] +pub fn monster_buffer_has_identifier(buf: &[u8]) -> bool { + flatbuffers::buffer_has_identifier(buf, MONSTER_IDENTIFIER, false) +} + +#[inline] +pub fn monster_size_prefixed_buffer_has_identifier(buf: &[u8]) -> bool { + flatbuffers::buffer_has_identifier(buf, MONSTER_IDENTIFIER, true) +} + +pub const MONSTER_EXTENSION: &str = "mon"; + +#[inline] +pub fn finish_monster_buffer<'a, 'b>( + fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, + root: flatbuffers::WIPOffset>) { + fbb.finish(root, Some(MONSTER_IDENTIFIER)); +} + +#[inline] +pub fn finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { + fbb.finish_size_prefixed(root, Some(MONSTER_IDENTIFIER)); +} diff --git a/tests/monster_test/my_game/example/race_generated.rs b/tests/monster_test/my_game/example/race_generated.rs new file mode 100644 index 000000000..ab012f9bf --- /dev/null +++ b/tests/monster_test/my_game/example/race_generated.rs @@ -0,0 +1,101 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_RACE: i8 = -1; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_RACE: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_RACE: [Race; 4] = [ + Race::None, + Race::Human, + Race::Dwarf, + Race::Elf, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct Race(pub i8); +#[allow(non_upper_case_globals)] +impl Race { + pub const None: Self = Self(-1); + pub const Human: Self = Self(0); + pub const Dwarf: Self = Self(1); + pub const Elf: Self = Self(2); + + pub const ENUM_MIN: i8 = -1; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::None, + Self::Human, + Self::Dwarf, + Self::Elf, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::None => Some("None"), + Self::Human => Some("Human"), + Self::Dwarf => Some("Dwarf"), + Self::Elf => Some("Elf"), + _ => None, + } + } +} +impl std::fmt::Debug for Race { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for Race { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for Race { + type Output = Race; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for Race { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for Race { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Race {} diff --git a/tests/monster_test/my_game/example/referrable_generated.rs b/tests/monster_test/my_game/example/referrable_generated.rs new file mode 100644 index 000000000..f00d760a4 --- /dev/null +++ b/tests/monster_test/my_game/example/referrable_generated.rs @@ -0,0 +1,140 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum ReferrableOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct Referrable<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for Referrable<'a> { + type Inner = Referrable<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> Referrable<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Referrable" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + Referrable { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args ReferrableArgs) -> flatbuffers::WIPOffset> { + let mut builder = ReferrableBuilder::new(_fbb); + builder.add_id(args.id); + builder.finish() + } + + pub fn unpack(&self) -> ReferrableT { + let id = self.id(); + ReferrableT { + id, + } + } + pub const VT_ID: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn id(&self) -> u64 { + self._tab.get::(Referrable::VT_ID, Some(0)).unwrap() + } + #[inline] + pub fn key_compare_less_than(&self, o: &Referrable) -> bool { + self.id() < o.id() + } + + #[inline] + pub fn key_compare_with_value(&self, val: u64) -> ::std::cmp::Ordering { + let key = self.id(); + key.cmp(&val) + } +} + +impl flatbuffers::Verifiable for Referrable<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("id", Self::VT_ID, false)? + .finish(); + Ok(()) + } +} +pub struct ReferrableArgs { + pub id: u64, +} +impl<'a> Default for ReferrableArgs { + #[inline] + fn default() -> Self { + ReferrableArgs { + id: 0, + } + } +} +pub struct ReferrableBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> ReferrableBuilder<'a, 'b> { + #[inline] + pub fn add_id(&mut self, id: u64) { + self.fbb_.push_slot::(Referrable::VT_ID, id, 0); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ReferrableBuilder<'a, 'b> { + let start = _fbb.start_table(); + ReferrableBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for Referrable<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Referrable"); + ds.field("id", &self.id()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct ReferrableT { + pub id: u64, +} +impl Default for ReferrableT { + fn default() -> Self { + Self { + id: 0, + } + } +} +impl ReferrableT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let id = self.id; + Referrable::create(_fbb, &ReferrableArgs{ + id, + }) + } +} diff --git a/tests/monster_test/my_game/example/stat_generated.rs b/tests/monster_test/my_game/example/stat_generated.rs new file mode 100644 index 000000000..63e3f4073 --- /dev/null +++ b/tests/monster_test/my_game/example/stat_generated.rs @@ -0,0 +1,184 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum StatOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct Stat<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for Stat<'a> { + type Inner = Stat<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> Stat<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Stat" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + Stat { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args StatArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = StatBuilder::new(_fbb); + builder.add_val(args.val); + if let Some(x) = args.id { builder.add_id(x); } + builder.add_count(args.count); + builder.finish() + } + + pub fn unpack(&self) -> StatT { + let id = self.id().map(|x| { + x.to_string() + }); + let val = self.val(); + let count = self.count(); + StatT { + id, + val, + count, + } + } + pub const VT_ID: flatbuffers::VOffsetT = 4; + pub const VT_VAL: flatbuffers::VOffsetT = 6; + pub const VT_COUNT: flatbuffers::VOffsetT = 8; + + #[inline] + pub fn id(&self) -> Option<&'a str> { + self._tab.get::>(Stat::VT_ID, None) + } + #[inline] + pub fn val(&self) -> i64 { + self._tab.get::(Stat::VT_VAL, Some(0)).unwrap() + } + #[inline] + pub fn count(&self) -> u16 { + self._tab.get::(Stat::VT_COUNT, Some(0)).unwrap() + } + #[inline] + pub fn key_compare_less_than(&self, o: &Stat) -> bool { + self.count() < o.count() + } + + #[inline] + pub fn key_compare_with_value(&self, val: u16) -> ::std::cmp::Ordering { + let key = self.count(); + key.cmp(&val) + } +} + +impl flatbuffers::Verifiable for Stat<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("id", Self::VT_ID, false)? + .visit_field::("val", Self::VT_VAL, false)? + .visit_field::("count", Self::VT_COUNT, false)? + .finish(); + Ok(()) + } +} +pub struct StatArgs<'a> { + pub id: Option>, + pub val: i64, + pub count: u16, +} +impl<'a> Default for StatArgs<'a> { + #[inline] + fn default() -> Self { + StatArgs { + id: None, + val: 0, + count: 0, + } + } +} +pub struct StatBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> StatBuilder<'a, 'b> { + #[inline] + pub fn add_id(&mut self, id: flatbuffers::WIPOffset<&'b str>) { + self.fbb_.push_slot_always::>(Stat::VT_ID, id); + } + #[inline] + pub fn add_val(&mut self, val: i64) { + self.fbb_.push_slot::(Stat::VT_VAL, val, 0); + } + #[inline] + pub fn add_count(&mut self, count: u16) { + self.fbb_.push_slot::(Stat::VT_COUNT, count, 0); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> StatBuilder<'a, 'b> { + let start = _fbb.start_table(); + StatBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for Stat<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Stat"); + ds.field("id", &self.id()); + ds.field("val", &self.val()); + ds.field("count", &self.count()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct StatT { + pub id: Option, + pub val: i64, + pub count: u16, +} +impl Default for StatT { + fn default() -> Self { + Self { + id: None, + val: 0, + count: 0, + } + } +} +impl StatT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let id = self.id.as_ref().map(|x|{ + _fbb.create_string(x) + }); + let val = self.val; + let count = self.count; + Stat::create(_fbb, &StatArgs{ + id, + val, + count, + }) + } +} diff --git a/tests/monster_test/my_game/example/struct_of_structs_generated.rs b/tests/monster_test/my_game/example/struct_of_structs_generated.rs new file mode 100644 index 000000000..06c13f51b --- /dev/null +++ b/tests/monster_test/my_game/example/struct_of_structs_generated.rs @@ -0,0 +1,142 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct StructOfStructs, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct StructOfStructs(pub [u8; 20]); +impl Default for StructOfStructs { + fn default() -> Self { + Self([0; 20]) + } +} +impl std::fmt::Debug for StructOfStructs { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("StructOfStructs") + .field("a", &self.a()) + .field("b", &self.b()) + .field("c", &self.c()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for StructOfStructs {} +impl flatbuffers::SafeSliceAccess for StructOfStructs {} +impl<'a> flatbuffers::Follow<'a> for StructOfStructs { + type Inner = &'a StructOfStructs; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a StructOfStructs>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a StructOfStructs { + type Inner = &'a StructOfStructs; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for StructOfStructs { + type Output = StructOfStructs; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b StructOfStructs { + type Output = StructOfStructs; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const StructOfStructs as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for StructOfStructs { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> StructOfStructs { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: &Ability, + b: &Test, + c: &Ability, + ) -> Self { + let mut s = Self([0; 20]); + s.set_a(a); + s.set_b(b); + s.set_c(c); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.StructOfStructs" + } + + pub fn a(&self) -> &Ability { + unsafe { &*(self.0[0..].as_ptr() as *const Ability) } + } + + #[allow(clippy::identity_op)] + pub fn set_a(&mut self, x: &Ability) { + self.0[0..0 + 8].copy_from_slice(&x.0) + } + + pub fn b(&self) -> &Test { + unsafe { &*(self.0[8..].as_ptr() as *const Test) } + } + + #[allow(clippy::identity_op)] + pub fn set_b(&mut self, x: &Test) { + self.0[8..8 + 4].copy_from_slice(&x.0) + } + + pub fn c(&self) -> &Ability { + unsafe { &*(self.0[12..].as_ptr() as *const Ability) } + } + + #[allow(clippy::identity_op)] + pub fn set_c(&mut self, x: &Ability) { + self.0[12..12 + 8].copy_from_slice(&x.0) + } + + pub fn unpack(&self) -> StructOfStructsT { + StructOfStructsT { + a: self.a().unpack(), + b: self.b().unpack(), + c: self.c().unpack(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct StructOfStructsT { + pub a: AbilityT, + pub b: TestT, + pub c: AbilityT, +} +impl StructOfStructsT { + pub fn pack(&self) -> StructOfStructs { + StructOfStructs::new( + &self.a.pack(), + &self.b.pack(), + &self.c.pack(), + ) + } +} + diff --git a/tests/monster_test/my_game/example/test_generated.rs b/tests/monster_test/my_game/example/test_generated.rs new file mode 100644 index 000000000..e60f7fc59 --- /dev/null +++ b/tests/monster_test/my_game/example/test_generated.rs @@ -0,0 +1,155 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Test, aligned to 2 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Test(pub [u8; 4]); +impl Default for Test { + fn default() -> Self { + Self([0; 4]) + } +} +impl std::fmt::Debug for Test { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Test") + .field("a", &self.a()) + .field("b", &self.b()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Test {} +impl flatbuffers::SafeSliceAccess for Test {} +impl<'a> flatbuffers::Follow<'a> for Test { + type Inner = &'a Test; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Test>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Test { + type Inner = &'a Test; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Test { + type Output = Test; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Test as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Test { + type Output = Test; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Test as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for Test { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Test { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i16, + b: i8, + ) -> Self { + let mut s = Self([0; 4]); + s.set_a(a); + s.set_b(b); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Test" + } + + pub fn a(&self) -> i16 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i16) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i16 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn b(&self) -> i8 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[2..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_b(&mut self, x: i8) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i8 as *const u8, + self.0[2..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> TestT { + TestT { + a: self.a(), + b: self.b(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct TestT { + pub a: i16, + pub b: i8, +} +impl TestT { + pub fn pack(&self) -> Test { + Test::new( + self.a, + self.b, + ) + } +} + diff --git a/tests/monster_test/my_game/example/test_simple_table_with_enum_generated.rs b/tests/monster_test/my_game/example/test_simple_table_with_enum_generated.rs new file mode 100644 index 000000000..58421f4d6 --- /dev/null +++ b/tests/monster_test/my_game/example/test_simple_table_with_enum_generated.rs @@ -0,0 +1,130 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TestSimpleTableWithEnumOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TestSimpleTableWithEnum<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TestSimpleTableWithEnum<'a> { + type Inner = TestSimpleTableWithEnum<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TestSimpleTableWithEnum<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.TestSimpleTableWithEnum" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TestSimpleTableWithEnum { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TestSimpleTableWithEnumArgs) -> flatbuffers::WIPOffset> { + let mut builder = TestSimpleTableWithEnumBuilder::new(_fbb); + builder.add_color(args.color); + builder.finish() + } + + pub fn unpack(&self) -> TestSimpleTableWithEnumT { + let color = self.color(); + TestSimpleTableWithEnumT { + color, + } + } + pub const VT_COLOR: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn color(&self) -> Color { + self._tab.get::(TestSimpleTableWithEnum::VT_COLOR, Some(Color::Green)).unwrap() + } +} + +impl flatbuffers::Verifiable for TestSimpleTableWithEnum<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("color", Self::VT_COLOR, false)? + .finish(); + Ok(()) + } +} +pub struct TestSimpleTableWithEnumArgs { + pub color: Color, +} +impl<'a> Default for TestSimpleTableWithEnumArgs { + #[inline] + fn default() -> Self { + TestSimpleTableWithEnumArgs { + color: Color::Green, + } + } +} +pub struct TestSimpleTableWithEnumBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TestSimpleTableWithEnumBuilder<'a, 'b> { + #[inline] + pub fn add_color(&mut self, color: Color) { + self.fbb_.push_slot::(TestSimpleTableWithEnum::VT_COLOR, color, Color::Green); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TestSimpleTableWithEnumBuilder<'a, 'b> { + let start = _fbb.start_table(); + TestSimpleTableWithEnumBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TestSimpleTableWithEnum<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TestSimpleTableWithEnum"); + ds.field("color", &self.color()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TestSimpleTableWithEnumT { + pub color: Color, +} +impl Default for TestSimpleTableWithEnumT { + fn default() -> Self { + Self { + color: Color::Green, + } + } +} +impl TestSimpleTableWithEnumT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let color = self.color; + TestSimpleTableWithEnum::create(_fbb, &TestSimpleTableWithEnumArgs{ + color, + }) + } +} diff --git a/tests/monster_test/my_game/example/type_aliases_generated.rs b/tests/monster_test/my_game/example/type_aliases_generated.rs new file mode 100644 index 000000000..92ef626de --- /dev/null +++ b/tests/monster_test/my_game/example/type_aliases_generated.rs @@ -0,0 +1,358 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TypeAliasesOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TypeAliases<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TypeAliases<'a> { + type Inner = TypeAliases<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TypeAliases<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.TypeAliases" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TypeAliases { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TypeAliasesArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TypeAliasesBuilder::new(_fbb); + builder.add_f64_(args.f64_); + builder.add_u64_(args.u64_); + builder.add_i64_(args.i64_); + if let Some(x) = args.vf64 { builder.add_vf64(x); } + if let Some(x) = args.v8 { builder.add_v8(x); } + builder.add_f32_(args.f32_); + builder.add_u32_(args.u32_); + builder.add_i32_(args.i32_); + builder.add_u16_(args.u16_); + builder.add_i16_(args.i16_); + builder.add_u8_(args.u8_); + builder.add_i8_(args.i8_); + builder.finish() + } + + pub fn unpack(&self) -> TypeAliasesT { + let i8_ = self.i8_(); + let u8_ = self.u8_(); + let i16_ = self.i16_(); + let u16_ = self.u16_(); + let i32_ = self.i32_(); + let u32_ = self.u32_(); + let i64_ = self.i64_(); + let u64_ = self.u64_(); + let f32_ = self.f32_(); + let f64_ = self.f64_(); + let v8 = self.v8().map(|x| { + x.to_vec() + }); + let vf64 = self.vf64().map(|x| { + x.into_iter().collect() + }); + TypeAliasesT { + i8_, + u8_, + i16_, + u16_, + i32_, + u32_, + i64_, + u64_, + f32_, + f64_, + v8, + vf64, + } + } + pub const VT_I8_: flatbuffers::VOffsetT = 4; + pub const VT_U8_: flatbuffers::VOffsetT = 6; + pub const VT_I16_: flatbuffers::VOffsetT = 8; + pub const VT_U16_: flatbuffers::VOffsetT = 10; + pub const VT_I32_: flatbuffers::VOffsetT = 12; + pub const VT_U32_: flatbuffers::VOffsetT = 14; + pub const VT_I64_: flatbuffers::VOffsetT = 16; + pub const VT_U64_: flatbuffers::VOffsetT = 18; + pub const VT_F32_: flatbuffers::VOffsetT = 20; + pub const VT_F64_: flatbuffers::VOffsetT = 22; + pub const VT_V8: flatbuffers::VOffsetT = 24; + pub const VT_VF64: flatbuffers::VOffsetT = 26; + + #[inline] + pub fn i8_(&self) -> i8 { + self._tab.get::(TypeAliases::VT_I8_, Some(0)).unwrap() + } + #[inline] + pub fn u8_(&self) -> u8 { + self._tab.get::(TypeAliases::VT_U8_, Some(0)).unwrap() + } + #[inline] + pub fn i16_(&self) -> i16 { + self._tab.get::(TypeAliases::VT_I16_, Some(0)).unwrap() + } + #[inline] + pub fn u16_(&self) -> u16 { + self._tab.get::(TypeAliases::VT_U16_, Some(0)).unwrap() + } + #[inline] + pub fn i32_(&self) -> i32 { + self._tab.get::(TypeAliases::VT_I32_, Some(0)).unwrap() + } + #[inline] + pub fn u32_(&self) -> u32 { + self._tab.get::(TypeAliases::VT_U32_, Some(0)).unwrap() + } + #[inline] + pub fn i64_(&self) -> i64 { + self._tab.get::(TypeAliases::VT_I64_, Some(0)).unwrap() + } + #[inline] + pub fn u64_(&self) -> u64 { + self._tab.get::(TypeAliases::VT_U64_, Some(0)).unwrap() + } + #[inline] + pub fn f32_(&self) -> f32 { + self._tab.get::(TypeAliases::VT_F32_, Some(0.0)).unwrap() + } + #[inline] + pub fn f64_(&self) -> f64 { + self._tab.get::(TypeAliases::VT_F64_, Some(0.0)).unwrap() + } + #[inline] + pub fn v8(&self) -> Option<&'a [i8]> { + self._tab.get::>>(TypeAliases::VT_V8, None).map(|v| v.safe_slice()) + } + #[inline] + pub fn vf64(&self) -> Option> { + self._tab.get::>>(TypeAliases::VT_VF64, None) + } +} + +impl flatbuffers::Verifiable for TypeAliases<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("i8_", Self::VT_I8_, false)? + .visit_field::("u8_", Self::VT_U8_, false)? + .visit_field::("i16_", Self::VT_I16_, false)? + .visit_field::("u16_", Self::VT_U16_, false)? + .visit_field::("i32_", Self::VT_I32_, false)? + .visit_field::("u32_", Self::VT_U32_, false)? + .visit_field::("i64_", Self::VT_I64_, false)? + .visit_field::("u64_", Self::VT_U64_, false)? + .visit_field::("f32_", Self::VT_F32_, false)? + .visit_field::("f64_", Self::VT_F64_, false)? + .visit_field::>>("v8", Self::VT_V8, false)? + .visit_field::>>("vf64", Self::VT_VF64, false)? + .finish(); + Ok(()) + } +} +pub struct TypeAliasesArgs<'a> { + pub i8_: i8, + pub u8_: u8, + pub i16_: i16, + pub u16_: u16, + pub i32_: i32, + pub u32_: u32, + pub i64_: i64, + pub u64_: u64, + pub f32_: f32, + pub f64_: f64, + pub v8: Option>>, + pub vf64: Option>>, +} +impl<'a> Default for TypeAliasesArgs<'a> { + #[inline] + fn default() -> Self { + TypeAliasesArgs { + i8_: 0, + u8_: 0, + i16_: 0, + u16_: 0, + i32_: 0, + u32_: 0, + i64_: 0, + u64_: 0, + f32_: 0.0, + f64_: 0.0, + v8: None, + vf64: None, + } + } +} +pub struct TypeAliasesBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TypeAliasesBuilder<'a, 'b> { + #[inline] + pub fn add_i8_(&mut self, i8_: i8) { + self.fbb_.push_slot::(TypeAliases::VT_I8_, i8_, 0); + } + #[inline] + pub fn add_u8_(&mut self, u8_: u8) { + self.fbb_.push_slot::(TypeAliases::VT_U8_, u8_, 0); + } + #[inline] + pub fn add_i16_(&mut self, i16_: i16) { + self.fbb_.push_slot::(TypeAliases::VT_I16_, i16_, 0); + } + #[inline] + pub fn add_u16_(&mut self, u16_: u16) { + self.fbb_.push_slot::(TypeAliases::VT_U16_, u16_, 0); + } + #[inline] + pub fn add_i32_(&mut self, i32_: i32) { + self.fbb_.push_slot::(TypeAliases::VT_I32_, i32_, 0); + } + #[inline] + pub fn add_u32_(&mut self, u32_: u32) { + self.fbb_.push_slot::(TypeAliases::VT_U32_, u32_, 0); + } + #[inline] + pub fn add_i64_(&mut self, i64_: i64) { + self.fbb_.push_slot::(TypeAliases::VT_I64_, i64_, 0); + } + #[inline] + pub fn add_u64_(&mut self, u64_: u64) { + self.fbb_.push_slot::(TypeAliases::VT_U64_, u64_, 0); + } + #[inline] + pub fn add_f32_(&mut self, f32_: f32) { + self.fbb_.push_slot::(TypeAliases::VT_F32_, f32_, 0.0); + } + #[inline] + pub fn add_f64_(&mut self, f64_: f64) { + self.fbb_.push_slot::(TypeAliases::VT_F64_, f64_, 0.0); + } + #[inline] + pub fn add_v8(&mut self, v8: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TypeAliases::VT_V8, v8); + } + #[inline] + pub fn add_vf64(&mut self, vf64: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TypeAliases::VT_VF64, vf64); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TypeAliasesBuilder<'a, 'b> { + let start = _fbb.start_table(); + TypeAliasesBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TypeAliases<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TypeAliases"); + ds.field("i8_", &self.i8_()); + ds.field("u8_", &self.u8_()); + ds.field("i16_", &self.i16_()); + ds.field("u16_", &self.u16_()); + ds.field("i32_", &self.i32_()); + ds.field("u32_", &self.u32_()); + ds.field("i64_", &self.i64_()); + ds.field("u64_", &self.u64_()); + ds.field("f32_", &self.f32_()); + ds.field("f64_", &self.f64_()); + ds.field("v8", &self.v8()); + ds.field("vf64", &self.vf64()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TypeAliasesT { + pub i8_: i8, + pub u8_: u8, + pub i16_: i16, + pub u16_: u16, + pub i32_: i32, + pub u32_: u32, + pub i64_: i64, + pub u64_: u64, + pub f32_: f32, + pub f64_: f64, + pub v8: Option>, + pub vf64: Option>, +} +impl Default for TypeAliasesT { + fn default() -> Self { + Self { + i8_: 0, + u8_: 0, + i16_: 0, + u16_: 0, + i32_: 0, + u32_: 0, + i64_: 0, + u64_: 0, + f32_: 0.0, + f64_: 0.0, + v8: None, + vf64: None, + } + } +} +impl TypeAliasesT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let i8_ = self.i8_; + let u8_ = self.u8_; + let i16_ = self.i16_; + let u16_ = self.u16_; + let i32_ = self.i32_; + let u32_ = self.u32_; + let i64_ = self.i64_; + let u64_ = self.u64_; + let f32_ = self.f32_; + let f64_ = self.f64_; + let v8 = self.v8.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + let vf64 = self.vf64.as_ref().map(|x|{ + _fbb.create_vector(x) + }); + TypeAliases::create(_fbb, &TypeAliasesArgs{ + i8_, + u8_, + i16_, + u16_, + i32_, + u32_, + i64_, + u64_, + f32_, + f64_, + v8, + vf64, + }) + } +} diff --git a/tests/monster_test/my_game/example/vec_3_generated.rs b/tests/monster_test/my_game/example/vec_3_generated.rs new file mode 100644 index 000000000..ec4e27479 --- /dev/null +++ b/tests/monster_test/my_game/example/vec_3_generated.rs @@ -0,0 +1,257 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Vec3, aligned to 8 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Vec3(pub [u8; 32]); +impl Default for Vec3 { + fn default() -> Self { + Self([0; 32]) + } +} +impl std::fmt::Debug for Vec3 { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Vec3") + .field("x", &self.x()) + .field("y", &self.y()) + .field("z", &self.z()) + .field("test1", &self.test1()) + .field("test2", &self.test2()) + .field("test3", &self.test3()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for 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<'a> flatbuffers::Verifiable for Vec3 { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Vec3 { + #[allow(clippy::too_many_arguments)] + pub fn new( + x: f32, + y: f32, + z: f32, + test1: f64, + test2: Color, + test3: &Test, + ) -> Self { + let mut s = Self([0; 32]); + s.set_x(x); + s.set_y(y); + s.set_z(z); + s.set_test1(test1); + s.set_test2(test2); + s.set_test3(test3); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example.Vec3" + } + + pub fn x(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_x(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn y(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_y(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn z(&self) -> f32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[8..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_z(&mut self, x: f32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f32 as *const u8, + self.0[8..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn test1(&self) -> f64 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[16..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_test1(&mut self, x: f64) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const f64 as *const u8, + self.0[16..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn test2(&self) -> Color { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[24..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_test2(&mut self, x: Color) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const Color as *const u8, + self.0[24..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn test3(&self) -> &Test { + unsafe { &*(self.0[26..].as_ptr() as *const Test) } + } + + #[allow(clippy::identity_op)] + pub fn set_test3(&mut self, x: &Test) { + self.0[26..26 + 4].copy_from_slice(&x.0) + } + + pub fn unpack(&self) -> Vec3T { + Vec3T { + x: self.x(), + y: self.y(), + z: self.z(), + test1: self.test1(), + test2: self.test2(), + test3: self.test3().unpack(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct Vec3T { + pub x: f32, + pub y: f32, + pub z: f32, + pub test1: f64, + pub test2: Color, + pub test3: TestT, +} +impl Vec3T { + pub fn pack(&self) -> Vec3 { + Vec3::new( + self.x, + self.y, + self.z, + self.test1, + self.test2, + &self.test3.pack(), + ) + } +} + diff --git a/tests/monster_test/my_game/example_2/monster_generated.rs b/tests/monster_test/my_game/example_2/monster_generated.rs new file mode 100644 index 000000000..e7fd9ac4a --- /dev/null +++ b/tests/monster_test/my_game/example_2/monster_generated.rs @@ -0,0 +1,109 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum MonsterOffset {} +#[derive(Copy, Clone, 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, loc } } + } +} + +impl<'a> Monster<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.Example2.Monster" + } + + #[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) -> flatbuffers::WIPOffset> { + let mut builder = MonsterBuilder::new(_fbb); + builder.finish() + } + + pub fn unpack(&self) -> MonsterT { + MonsterT { + } + } +} + +impl flatbuffers::Verifiable for Monster<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .finish(); + Ok(()) + } +} +pub struct MonsterArgs { +} +impl<'a> Default for MonsterArgs { + #[inline] + fn default() -> Self { + MonsterArgs { + } + } +} +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 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()) + } +} + +impl std::fmt::Debug for Monster<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Monster"); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct MonsterT { +} +impl Default for MonsterT { + fn default() -> Self { + Self { + } + } +} +impl MonsterT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + Monster::create(_fbb, &MonsterArgs{ + }) + } +} diff --git a/tests/monster_test/my_game/in_parent_namespace_generated.rs b/tests/monster_test/my_game/in_parent_namespace_generated.rs new file mode 100644 index 000000000..e45a58c97 --- /dev/null +++ b/tests/monster_test/my_game/in_parent_namespace_generated.rs @@ -0,0 +1,109 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum InParentNamespaceOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct InParentNamespace<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for InParentNamespace<'a> { + type Inner = InParentNamespace<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> InParentNamespace<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.InParentNamespace" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + InParentNamespace { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + _args: &'args InParentNamespaceArgs) -> flatbuffers::WIPOffset> { + let mut builder = InParentNamespaceBuilder::new(_fbb); + builder.finish() + } + + pub fn unpack(&self) -> InParentNamespaceT { + InParentNamespaceT { + } + } +} + +impl flatbuffers::Verifiable for InParentNamespace<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .finish(); + Ok(()) + } +} +pub struct InParentNamespaceArgs { +} +impl<'a> Default for InParentNamespaceArgs { + #[inline] + fn default() -> Self { + InParentNamespaceArgs { + } + } +} +pub struct InParentNamespaceBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> InParentNamespaceBuilder<'a, 'b> { + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> InParentNamespaceBuilder<'a, 'b> { + let start = _fbb.start_table(); + InParentNamespaceBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for InParentNamespace<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("InParentNamespace"); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct InParentNamespaceT { +} +impl Default for InParentNamespaceT { + fn default() -> Self { + Self { + } + } +} +impl InParentNamespaceT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + InParentNamespace::create(_fbb, &InParentNamespaceArgs{ + }) + } +} diff --git a/tests/monster_test/my_game/other_name_space/from_include_generated.rs b/tests/monster_test/my_game/other_name_space/from_include_generated.rs new file mode 100644 index 000000000..048bafdb8 --- /dev/null +++ b/tests/monster_test/my_game/other_name_space/from_include_generated.rs @@ -0,0 +1,89 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_FROM_INCLUDE: i64 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [ + FromInclude::IncludeVal, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct FromInclude(pub i64); +#[allow(non_upper_case_globals)] +impl FromInclude { + pub const IncludeVal: Self = Self(0); + + pub const ENUM_MIN: i64 = 0; + pub const ENUM_MAX: i64 = 0; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::IncludeVal, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::IncludeVal => Some("IncludeVal"), + _ => None, + } + } +} +impl std::fmt::Debug for FromInclude { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for FromInclude { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for FromInclude { + type Output = FromInclude; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for FromInclude { + #[inline] + fn to_little_endian(self) -> Self { + let b = i64::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i64::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for FromInclude { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i64::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for FromInclude {} diff --git a/tests/monster_test/my_game/other_name_space/table_b_generated.rs b/tests/monster_test/my_game/other_name_space/table_b_generated.rs new file mode 100644 index 000000000..6d863e34f --- /dev/null +++ b/tests/monster_test/my_game/other_name_space/table_b_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableBOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableB<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableB<'a> { + type Inner = TableB<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableB<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.TableB" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableB { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableBArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableBBuilder::new(_fbb); + if let Some(x) = args.a { builder.add_a(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableBT { + let a = self.a().map(|x| { + Box::new(x.unpack()) + }); + TableBT { + a, + } + } + pub const VT_A: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn a(&self) -> Option> { + self._tab.get::>(TableB::VT_A, None) + } +} + +impl flatbuffers::Verifiable for TableB<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("a", Self::VT_A, false)? + .finish(); + Ok(()) + } +} +pub struct TableBArgs<'a> { + pub a: Option>>, +} +impl<'a> Default for TableBArgs<'a> { + #[inline] + fn default() -> Self { + TableBArgs { + a: None, + } + } +} +pub struct TableBBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableBBuilder<'a, 'b> { + #[inline] + pub fn add_a(&mut self, a: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableB::VT_A, a); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableBBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableBBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableB<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableB"); + ds.field("a", &self.a()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableBT { + pub a: Option>, +} +impl Default for TableBT { + fn default() -> Self { + Self { + a: None, + } + } +} +impl TableBT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let a = self.a.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableB::create(_fbb, &TableBArgs{ + a, + }) + } +} diff --git a/tests/monster_test/my_game/other_name_space/unused_generated.rs b/tests/monster_test/my_game/other_name_space/unused_generated.rs new file mode 100644 index 000000000..41725f3fa --- /dev/null +++ b/tests/monster_test/my_game/other_name_space/unused_generated.rs @@ -0,0 +1,126 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct Unused, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct Unused(pub [u8; 4]); +impl Default for Unused { + fn default() -> Self { + Self([0; 4]) + } +} +impl std::fmt::Debug for Unused { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Unused") + .field("a", &self.a()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for Unused {} +impl flatbuffers::SafeSliceAccess for Unused {} +impl<'a> flatbuffers::Follow<'a> for Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a Unused>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a Unused { + type Inner = &'a Unused; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for Unused { + type Output = Unused; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b Unused { + type Output = Unused; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const Unused as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for Unused { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> Unused { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i32, + ) -> Self { + let mut s = Self([0; 4]); + s.set_a(a); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "MyGame.OtherNameSpace.Unused" + } + + pub fn a(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> UnusedT { + UnusedT { + a: self.a(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct UnusedT { + pub a: i32, +} +impl UnusedT { + pub fn pack(&self) -> Unused { + Unused::new( + self.a, + ) + } +} + diff --git a/tests/monster_test/table_a_generated.rs b/tests/monster_test/table_a_generated.rs new file mode 100644 index 000000000..97f5c267c --- /dev/null +++ b/tests/monster_test/table_a_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableAOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableA<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableA<'a> { + type Inner = TableA<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableA<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "TableA" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableA { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableAArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableABuilder::new(_fbb); + if let Some(x) = args.b { builder.add_b(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableAT { + let b = self.b().map(|x| { + Box::new(x.unpack()) + }); + TableAT { + b, + } + } + pub const VT_B: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn b(&self) -> Option> { + self._tab.get::>(TableA::VT_B, None) + } +} + +impl flatbuffers::Verifiable for TableA<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("b", Self::VT_B, false)? + .finish(); + Ok(()) + } +} +pub struct TableAArgs<'a> { + pub b: Option>>, +} +impl<'a> Default for TableAArgs<'a> { + #[inline] + fn default() -> Self { + TableAArgs { + b: None, + } + } +} +pub struct TableABuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableABuilder<'a, 'b> { + #[inline] + pub fn add_b(&mut self, b: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableA::VT_B, b); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableABuilder<'a, 'b> { + let start = _fbb.start_table(); + TableABuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableA<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableA"); + ds.field("b", &self.b()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableAT { + pub b: Option>, +} +impl Default for TableAT { + fn default() -> Self { + Self { + b: None, + } + } +} +impl TableAT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let b = self.b.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableA::create(_fbb, &TableAArgs{ + b, + }) + } +} diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs deleted file mode 100644 index 86cf73780..000000000 --- a/tests/monster_test_generated.rs +++ /dev/null @@ -1,4024 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use crate::include_test2_generated::*; -use crate::include_test1_generated::*; -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod my_game { - - use crate::include_test2_generated::*; - use crate::include_test1_generated::*; - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -pub enum InParentNamespaceOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct InParentNamespace<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for InParentNamespace<'a> { - type Inner = InParentNamespace<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> InParentNamespace<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.InParentNamespace" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - InParentNamespace { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - _args: &'args InParentNamespaceArgs) -> flatbuffers::WIPOffset> { - let mut builder = InParentNamespaceBuilder::new(_fbb); - builder.finish() - } - - pub fn unpack(&self) -> InParentNamespaceT { - InParentNamespaceT { - } - } -} - -impl flatbuffers::Verifiable for InParentNamespace<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .finish(); - Ok(()) - } -} -pub struct InParentNamespaceArgs { -} -impl<'a> Default for InParentNamespaceArgs { - #[inline] - fn default() -> Self { - InParentNamespaceArgs { - } - } -} -pub struct InParentNamespaceBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> InParentNamespaceBuilder<'a, 'b> { - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> InParentNamespaceBuilder<'a, 'b> { - let start = _fbb.start_table(); - InParentNamespaceBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for InParentNamespace<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("InParentNamespace"); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct InParentNamespaceT { -} -impl Default for InParentNamespaceT { - fn default() -> Self { - Self { - } - } -} -impl InParentNamespaceT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - InParentNamespace::create(_fbb, &InParentNamespaceArgs{ - }) - } -} -#[allow(unused_imports, dead_code)] -pub mod example_2 { - - use crate::include_test2_generated::*; - use crate::include_test1_generated::*; - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -pub enum MonsterOffset {} -#[derive(Copy, Clone, 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, loc } } - } -} - -impl<'a> Monster<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example2.Monster" - } - - #[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) -> flatbuffers::WIPOffset> { - let mut builder = MonsterBuilder::new(_fbb); - builder.finish() - } - - pub fn unpack(&self) -> MonsterT { - MonsterT { - } - } -} - -impl flatbuffers::Verifiable for Monster<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .finish(); - Ok(()) - } -} -pub struct MonsterArgs { -} -impl<'a> Default for MonsterArgs { - #[inline] - fn default() -> Self { - MonsterArgs { - } - } -} -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 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()) - } -} - -impl std::fmt::Debug for Monster<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Monster"); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct MonsterT { -} -impl Default for MonsterT { - fn default() -> Self { - Self { - } - } -} -impl MonsterT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - Monster::create(_fbb, &MonsterArgs{ - }) - } -} -} // pub mod Example2 - -#[allow(unused_imports, dead_code)] -pub mod example { - - use crate::include_test2_generated::*; - use crate::include_test1_generated::*; - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(non_upper_case_globals)] -mod bitflags_color { - flatbuffers::bitflags::bitflags! { - /// Composite components of Monster color. - #[derive(Default)] - pub struct Color: u8 { - const Red = 1; - /// \brief color Green - /// Green is bit_flag with value (1u << 1) - const Green = 2; - /// \brief color Blue (1u << 3) - const Blue = 8; - } - } -} -pub use self::bitflags_color::Color; - -impl<'a> flatbuffers::Follow<'a> for Color { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - unsafe { Self::from_bits_unchecked(b) } - } -} - -impl flatbuffers::Push for Color { - type Output = Color; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.bits()); } - } -} - -impl flatbuffers::EndianScalar for Color { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.bits()); - unsafe { Self::from_bits_unchecked(b) } - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.bits()); - unsafe { Self::from_bits_unchecked(b) } - } -} - -impl<'a> flatbuffers::Verifiable for Color { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Color {} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_RACE: i8 = -1; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_RACE: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_RACE: [Race; 4] = [ - Race::None, - Race::Human, - Race::Dwarf, - Race::Elf, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct Race(pub i8); -#[allow(non_upper_case_globals)] -impl Race { - pub const None: Self = Self(-1); - pub const Human: Self = Self(0); - pub const Dwarf: Self = Self(1); - pub const Elf: Self = Self(2); - - pub const ENUM_MIN: i8 = -1; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::None, - Self::Human, - Self::Dwarf, - Self::Elf, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::None => Some("None"), - Self::Human => Some("Human"), - Self::Dwarf => Some("Dwarf"), - Self::Elf => Some("Elf"), - _ => None, - } - } -} -impl std::fmt::Debug for Race { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for Race { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for Race { - type Output = Race; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for Race { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for Race { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Race {} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ANY: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ANY: u8 = 3; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ANY: [Any; 4] = [ - Any::NONE, - Any::Monster, - Any::TestSimpleTableWithEnum, - Any::MyGame_Example2_Monster, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct Any(pub u8); -#[allow(non_upper_case_globals)] -impl Any { - pub const NONE: Self = Self(0); - pub const Monster: Self = Self(1); - pub const TestSimpleTableWithEnum: Self = Self(2); - pub const MyGame_Example2_Monster: Self = Self(3); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 3; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::Monster, - Self::TestSimpleTableWithEnum, - Self::MyGame_Example2_Monster, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::Monster => Some("Monster"), - Self::TestSimpleTableWithEnum => Some("TestSimpleTableWithEnum"), - Self::MyGame_Example2_Monster => Some("MyGame_Example2_Monster"), - _ => None, - } - } -} -impl std::fmt::Debug for Any { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for Any { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for Any { - type Output = Any; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for Any { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for Any { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Any {} -pub struct AnyUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum AnyT { - NONE, - Monster(Box), - TestSimpleTableWithEnum(Box), - MyGameExample2Monster(Box), -} -impl Default for AnyT { - fn default() -> Self { - Self::NONE - } -} -impl AnyT { - pub fn any_type(&self) -> Any { - match self { - Self::NONE => Any::NONE, - Self::Monster(_) => Any::Monster, - Self::TestSimpleTableWithEnum(_) => Any::TestSimpleTableWithEnum, - Self::MyGameExample2Monster(_) => Any::MyGame_Example2_Monster, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::Monster(v) => Some(v.pack(fbb).as_union_value()), - Self::TestSimpleTableWithEnum(v) => Some(v.pack(fbb).as_union_value()), - Self::MyGameExample2Monster(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned MonsterT, setting the union to NONE. - pub fn take_monster(&mut self) -> Option> { - if let Self::Monster(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::Monster(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the MonsterT. - pub fn as_monster(&self) -> Option<&MonsterT> { - if let Self::Monster(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the MonsterT. - pub fn as_monster_mut(&mut self) -> Option<&mut MonsterT> { - if let Self::Monster(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned TestSimpleTableWithEnumT, setting the union to NONE. - pub fn take_test_simple_table_with_enum(&mut self) -> Option> { - if let Self::TestSimpleTableWithEnum(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::TestSimpleTableWithEnum(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the TestSimpleTableWithEnumT. - pub fn as_test_simple_table_with_enum(&self) -> Option<&TestSimpleTableWithEnumT> { - if let Self::TestSimpleTableWithEnum(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the TestSimpleTableWithEnumT. - pub fn as_test_simple_table_with_enum_mut(&mut self) -> Option<&mut TestSimpleTableWithEnumT> { - if let Self::TestSimpleTableWithEnum(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned super::example_2::MonsterT, setting the union to NONE. - pub fn take_my_game_example_2_monster(&mut self) -> Option> { - if let Self::MyGameExample2Monster(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::MyGameExample2Monster(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the super::example_2::MonsterT. - pub fn as_my_game_example_2_monster(&self) -> Option<&super::example_2::MonsterT> { - if let Self::MyGameExample2Monster(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the super::example_2::MonsterT. - pub fn as_my_game_example_2_monster_mut(&mut self) -> Option<&mut super::example_2::MonsterT> { - if let Self::MyGameExample2Monster(v) = self { Some(v.as_mut()) } else { None } - } -} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ANY_UNIQUE_ALIASES: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ANY_UNIQUE_ALIASES: u8 = 3; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ANY_UNIQUE_ALIASES: [AnyUniqueAliases; 4] = [ - AnyUniqueAliases::NONE, - AnyUniqueAliases::M, - AnyUniqueAliases::TS, - AnyUniqueAliases::M2, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct AnyUniqueAliases(pub u8); -#[allow(non_upper_case_globals)] -impl AnyUniqueAliases { - pub const NONE: Self = Self(0); - pub const M: Self = Self(1); - pub const TS: Self = Self(2); - pub const M2: Self = Self(3); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 3; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::M, - Self::TS, - Self::M2, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::M => Some("M"), - Self::TS => Some("TS"), - Self::M2 => Some("M2"), - _ => None, - } - } -} -impl std::fmt::Debug for AnyUniqueAliases { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for AnyUniqueAliases { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for AnyUniqueAliases { - type Output = AnyUniqueAliases; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for AnyUniqueAliases { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for AnyUniqueAliases { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for AnyUniqueAliases {} -pub struct AnyUniqueAliasesUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum AnyUniqueAliasesT { - NONE, - M(Box), - TS(Box), - M2(Box), -} -impl Default for AnyUniqueAliasesT { - fn default() -> Self { - Self::NONE - } -} -impl AnyUniqueAliasesT { - pub fn any_unique_aliases_type(&self) -> AnyUniqueAliases { - match self { - Self::NONE => AnyUniqueAliases::NONE, - Self::M(_) => AnyUniqueAliases::M, - Self::TS(_) => AnyUniqueAliases::TS, - Self::M2(_) => AnyUniqueAliases::M2, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::M(v) => Some(v.pack(fbb).as_union_value()), - Self::TS(v) => Some(v.pack(fbb).as_union_value()), - Self::M2(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned MonsterT, setting the union to NONE. - pub fn take_m(&mut self) -> Option> { - if let Self::M(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::M(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the MonsterT. - pub fn as_m(&self) -> Option<&MonsterT> { - if let Self::M(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the MonsterT. - pub fn as_m_mut(&mut self) -> Option<&mut MonsterT> { - if let Self::M(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned TestSimpleTableWithEnumT, setting the union to NONE. - pub fn take_ts(&mut self) -> Option> { - if let Self::TS(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::TS(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the TestSimpleTableWithEnumT. - pub fn as_ts(&self) -> Option<&TestSimpleTableWithEnumT> { - if let Self::TS(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the TestSimpleTableWithEnumT. - pub fn as_ts_mut(&mut self) -> Option<&mut TestSimpleTableWithEnumT> { - if let Self::TS(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned super::example_2::MonsterT, setting the union to NONE. - pub fn take_m2(&mut self) -> Option> { - if let Self::M2(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::M2(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the super::example_2::MonsterT. - pub fn as_m2(&self) -> Option<&super::example_2::MonsterT> { - if let Self::M2(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the super::example_2::MonsterT. - pub fn as_m2_mut(&mut self) -> Option<&mut super::example_2::MonsterT> { - if let Self::M2(v) = self { Some(v.as_mut()) } else { None } - } -} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ANY_AMBIGUOUS_ALIASES: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ANY_AMBIGUOUS_ALIASES: u8 = 3; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES: [AnyAmbiguousAliases; 4] = [ - AnyAmbiguousAliases::NONE, - AnyAmbiguousAliases::M1, - AnyAmbiguousAliases::M2, - AnyAmbiguousAliases::M3, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct AnyAmbiguousAliases(pub u8); -#[allow(non_upper_case_globals)] -impl AnyAmbiguousAliases { - pub const NONE: Self = Self(0); - pub const M1: Self = Self(1); - pub const M2: Self = Self(2); - pub const M3: Self = Self(3); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 3; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::M1, - Self::M2, - Self::M3, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::M1 => Some("M1"), - Self::M2 => Some("M2"), - Self::M3 => Some("M3"), - _ => None, - } - } -} -impl std::fmt::Debug for AnyAmbiguousAliases { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for AnyAmbiguousAliases { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for AnyAmbiguousAliases { - type Output = AnyAmbiguousAliases; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for AnyAmbiguousAliases { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for AnyAmbiguousAliases { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for AnyAmbiguousAliases {} -pub struct AnyAmbiguousAliasesUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum AnyAmbiguousAliasesT { - NONE, - M1(Box), - M2(Box), - M3(Box), -} -impl Default for AnyAmbiguousAliasesT { - fn default() -> Self { - Self::NONE - } -} -impl AnyAmbiguousAliasesT { - pub fn any_ambiguous_aliases_type(&self) -> AnyAmbiguousAliases { - match self { - Self::NONE => AnyAmbiguousAliases::NONE, - Self::M1(_) => AnyAmbiguousAliases::M1, - Self::M2(_) => AnyAmbiguousAliases::M2, - Self::M3(_) => AnyAmbiguousAliases::M3, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::M1(v) => Some(v.pack(fbb).as_union_value()), - Self::M2(v) => Some(v.pack(fbb).as_union_value()), - Self::M3(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned MonsterT, setting the union to NONE. - pub fn take_m1(&mut self) -> Option> { - if let Self::M1(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::M1(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the MonsterT. - pub fn as_m1(&self) -> Option<&MonsterT> { - if let Self::M1(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the MonsterT. - pub fn as_m1_mut(&mut self) -> Option<&mut MonsterT> { - if let Self::M1(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned MonsterT, setting the union to NONE. - pub fn take_m2(&mut self) -> Option> { - if let Self::M2(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::M2(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the MonsterT. - pub fn as_m2(&self) -> Option<&MonsterT> { - if let Self::M2(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the MonsterT. - pub fn as_m2_mut(&mut self) -> Option<&mut MonsterT> { - if let Self::M2(v) = self { Some(v.as_mut()) } else { None } - } - /// If the union variant matches, return the owned MonsterT, setting the union to NONE. - pub fn take_m3(&mut self) -> Option> { - if let Self::M3(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::M3(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the MonsterT. - pub fn as_m3(&self) -> Option<&MonsterT> { - if let Self::M3(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the MonsterT. - pub fn as_m3_mut(&mut self) -> Option<&mut MonsterT> { - if let Self::M3(v) = self { Some(v.as_mut()) } else { None } - } -} -// struct Test, aligned to 2 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct Test(pub [u8; 4]); -impl Default for Test { - fn default() -> Self { - Self([0; 4]) - } -} -impl std::fmt::Debug for Test { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Test") - .field("a", &self.a()) - .field("b", &self.b()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Test {} -impl flatbuffers::SafeSliceAccess for Test {} -impl<'a> flatbuffers::Follow<'a> for Test { - type Inner = &'a Test; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a Test>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a Test { - type Inner = &'a Test; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for Test { - type Output = Test; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const Test as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b Test { - type Output = Test; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const Test as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for Test { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> Test { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: i16, - b: i8, - ) -> Self { - let mut s = Self([0; 4]); - s.set_a(a); - s.set_b(b); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Test" - } - - pub fn a(&self) -> i16 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_a(&mut self, x: i16) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i16 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn b(&self) -> i8 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[2..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_b(&mut self, x: i8) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i8 as *const u8, - self.0[2..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn unpack(&self) -> TestT { - TestT { - a: self.a(), - b: self.b(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct TestT { - pub a: i16, - pub b: i8, -} -impl TestT { - pub fn pack(&self) -> Test { - Test::new( - self.a, - self.b, - ) - } -} - -// struct Vec3, aligned to 8 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct Vec3(pub [u8; 32]); -impl Default for Vec3 { - fn default() -> Self { - Self([0; 32]) - } -} -impl std::fmt::Debug for Vec3 { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Vec3") - .field("x", &self.x()) - .field("y", &self.y()) - .field("z", &self.z()) - .field("test1", &self.test1()) - .field("test2", &self.test2()) - .field("test3", &self.test3()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for 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<'a> flatbuffers::Verifiable for Vec3 { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> Vec3 { - #[allow(clippy::too_many_arguments)] - pub fn new( - x: f32, - y: f32, - z: f32, - test1: f64, - test2: Color, - test3: &Test, - ) -> Self { - let mut s = Self([0; 32]); - s.set_x(x); - s.set_y(y); - s.set_z(z); - s.set_test1(test1); - s.set_test2(test2); - s.set_test3(&test3); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Vec3" - } - - pub fn x(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_x(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn y(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[4..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_y(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[4..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn z(&self) -> f32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[8..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_z(&mut self, x: f32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f32 as *const u8, - self.0[8..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn test1(&self) -> f64 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[16..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_test1(&mut self, x: f64) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const f64 as *const u8, - self.0[16..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn test2(&self) -> Color { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[24..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_test2(&mut self, x: Color) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const Color as *const u8, - self.0[24..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn test3(&self) -> &Test { - unsafe { &*(self.0[26..].as_ptr() as *const Test) } - } - - #[allow(clippy::identity_op)] - pub fn set_test3(&mut self, x: &Test) { - self.0[26..26 + 4].copy_from_slice(&x.0) - } - - pub fn unpack(&self) -> Vec3T { - Vec3T { - x: self.x(), - y: self.y(), - z: self.z(), - test1: self.test1(), - test2: self.test2(), - test3: self.test3().unpack(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct Vec3T { - pub x: f32, - pub y: f32, - pub z: f32, - pub test1: f64, - pub test2: Color, - pub test3: TestT, -} -impl Vec3T { - pub fn pack(&self) -> Vec3 { - Vec3::new( - self.x, - self.y, - self.z, - self.test1, - self.test2, - &self.test3.pack(), - ) - } -} - -// struct Ability, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct Ability(pub [u8; 8]); -impl Default for Ability { - fn default() -> Self { - Self([0; 8]) - } -} -impl std::fmt::Debug for Ability { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("Ability") - .field("id", &self.id()) - .field("distance", &self.distance()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for Ability {} -impl flatbuffers::SafeSliceAccess for Ability {} -impl<'a> flatbuffers::Follow<'a> for Ability { - type Inner = &'a Ability; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a Ability>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a Ability { - type Inner = &'a Ability; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for Ability { - type Output = Ability; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const Ability as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b Ability { - type Output = Ability; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const Ability as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for Ability { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> Ability { - #[allow(clippy::too_many_arguments)] - pub fn new( - id: u32, - distance: u32, - ) -> Self { - let mut s = Self([0; 8]); - s.set_id(id); - s.set_distance(distance); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Ability" - } - - pub fn id(&self) -> u32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_id(&mut self, x: u32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const u32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - #[inline] - pub fn key_compare_less_than(&self, o: &Ability) -> bool { - self.id() < o.id() - } - - #[inline] - pub fn key_compare_with_value(&self, val: u32) -> ::std::cmp::Ordering { - let key = self.id(); - key.cmp(&val) - } - pub fn distance(&self) -> u32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[4..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_distance(&mut self, x: u32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const u32 as *const u8, - self.0[4..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn unpack(&self) -> AbilityT { - AbilityT { - id: self.id(), - distance: self.distance(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct AbilityT { - pub id: u32, - pub distance: u32, -} -impl AbilityT { - pub fn pack(&self) -> Ability { - Ability::new( - self.id, - self.distance, - ) - } -} - -// struct StructOfStructs, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct StructOfStructs(pub [u8; 20]); -impl Default for StructOfStructs { - fn default() -> Self { - Self([0; 20]) - } -} -impl std::fmt::Debug for StructOfStructs { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("StructOfStructs") - .field("a", &self.a()) - .field("b", &self.b()) - .field("c", &self.c()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for StructOfStructs {} -impl flatbuffers::SafeSliceAccess for StructOfStructs {} -impl<'a> flatbuffers::Follow<'a> for StructOfStructs { - type Inner = &'a StructOfStructs; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a StructOfStructs>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a StructOfStructs { - type Inner = &'a StructOfStructs; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for StructOfStructs { - type Output = StructOfStructs; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b StructOfStructs { - type Output = StructOfStructs; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const StructOfStructs as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for StructOfStructs { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> StructOfStructs { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: &Ability, - b: &Test, - c: &Ability, - ) -> Self { - let mut s = Self([0; 20]); - s.set_a(&a); - s.set_b(&b); - s.set_c(&c); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.StructOfStructs" - } - - pub fn a(&self) -> &Ability { - unsafe { &*(self.0[0..].as_ptr() as *const Ability) } - } - - #[allow(clippy::identity_op)] - pub fn set_a(&mut self, x: &Ability) { - self.0[0..0 + 8].copy_from_slice(&x.0) - } - - pub fn b(&self) -> &Test { - unsafe { &*(self.0[8..].as_ptr() as *const Test) } - } - - #[allow(clippy::identity_op)] - pub fn set_b(&mut self, x: &Test) { - self.0[8..8 + 4].copy_from_slice(&x.0) - } - - pub fn c(&self) -> &Ability { - unsafe { &*(self.0[12..].as_ptr() as *const Ability) } - } - - #[allow(clippy::identity_op)] - pub fn set_c(&mut self, x: &Ability) { - self.0[12..12 + 8].copy_from_slice(&x.0) - } - - pub fn unpack(&self) -> StructOfStructsT { - StructOfStructsT { - a: self.a().unpack(), - b: self.b().unpack(), - c: self.c().unpack(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct StructOfStructsT { - pub a: AbilityT, - pub b: TestT, - pub c: AbilityT, -} -impl StructOfStructsT { - pub fn pack(&self) -> StructOfStructs { - StructOfStructs::new( - &self.a.pack(), - &self.b.pack(), - &self.c.pack(), - ) - } -} - -pub enum TestSimpleTableWithEnumOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TestSimpleTableWithEnum<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TestSimpleTableWithEnum<'a> { - type Inner = TestSimpleTableWithEnum<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TestSimpleTableWithEnum<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.TestSimpleTableWithEnum" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TestSimpleTableWithEnum { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TestSimpleTableWithEnumArgs) -> flatbuffers::WIPOffset> { - let mut builder = TestSimpleTableWithEnumBuilder::new(_fbb); - builder.add_color(args.color); - builder.finish() - } - - pub fn unpack(&self) -> TestSimpleTableWithEnumT { - let color = self.color(); - TestSimpleTableWithEnumT { - color, - } - } - pub const VT_COLOR: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn color(&self) -> Color { - self._tab.get::(TestSimpleTableWithEnum::VT_COLOR, Some(Color::Green)).unwrap() - } -} - -impl flatbuffers::Verifiable for TestSimpleTableWithEnum<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"color", Self::VT_COLOR, false)? - .finish(); - Ok(()) - } -} -pub struct TestSimpleTableWithEnumArgs { - pub color: Color, -} -impl<'a> Default for TestSimpleTableWithEnumArgs { - #[inline] - fn default() -> Self { - TestSimpleTableWithEnumArgs { - color: Color::Green, - } - } -} -pub struct TestSimpleTableWithEnumBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TestSimpleTableWithEnumBuilder<'a, 'b> { - #[inline] - pub fn add_color(&mut self, color: Color) { - self.fbb_.push_slot::(TestSimpleTableWithEnum::VT_COLOR, color, Color::Green); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TestSimpleTableWithEnumBuilder<'a, 'b> { - let start = _fbb.start_table(); - TestSimpleTableWithEnumBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TestSimpleTableWithEnum<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TestSimpleTableWithEnum"); - ds.field("color", &self.color()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TestSimpleTableWithEnumT { - pub color: Color, -} -impl Default for TestSimpleTableWithEnumT { - fn default() -> Self { - Self { - color: Color::Green, - } - } -} -impl TestSimpleTableWithEnumT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let color = self.color; - TestSimpleTableWithEnum::create(_fbb, &TestSimpleTableWithEnumArgs{ - color, - }) - } -} -pub enum StatOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct Stat<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Stat<'a> { - type Inner = Stat<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> Stat<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Stat" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Stat { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args StatArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = StatBuilder::new(_fbb); - builder.add_val(args.val); - if let Some(x) = args.id { builder.add_id(x); } - builder.add_count(args.count); - builder.finish() - } - - pub fn unpack(&self) -> StatT { - let id = self.id().map(|x| { - x.to_string() - }); - let val = self.val(); - let count = self.count(); - StatT { - id, - val, - count, - } - } - pub const VT_ID: flatbuffers::VOffsetT = 4; - pub const VT_VAL: flatbuffers::VOffsetT = 6; - pub const VT_COUNT: flatbuffers::VOffsetT = 8; - - #[inline] - pub fn id(&self) -> Option<&'a str> { - self._tab.get::>(Stat::VT_ID, None) - } - #[inline] - pub fn val(&self) -> i64 { - self._tab.get::(Stat::VT_VAL, Some(0)).unwrap() - } - #[inline] - pub fn count(&self) -> u16 { - self._tab.get::(Stat::VT_COUNT, Some(0)).unwrap() - } - #[inline] - pub fn key_compare_less_than(&self, o: &Stat) -> bool { - self.count() < o.count() - } - - #[inline] - pub fn key_compare_with_value(&self, val: u16) -> ::std::cmp::Ordering { - let key = self.count(); - key.cmp(&val) - } -} - -impl flatbuffers::Verifiable for Stat<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"id", Self::VT_ID, false)? - .visit_field::(&"val", Self::VT_VAL, false)? - .visit_field::(&"count", Self::VT_COUNT, false)? - .finish(); - Ok(()) - } -} -pub struct StatArgs<'a> { - pub id: Option>, - pub val: i64, - pub count: u16, -} -impl<'a> Default for StatArgs<'a> { - #[inline] - fn default() -> Self { - StatArgs { - id: None, - val: 0, - count: 0, - } - } -} -pub struct StatBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> StatBuilder<'a, 'b> { - #[inline] - pub fn add_id(&mut self, id: flatbuffers::WIPOffset<&'b str>) { - self.fbb_.push_slot_always::>(Stat::VT_ID, id); - } - #[inline] - pub fn add_val(&mut self, val: i64) { - self.fbb_.push_slot::(Stat::VT_VAL, val, 0); - } - #[inline] - pub fn add_count(&mut self, count: u16) { - self.fbb_.push_slot::(Stat::VT_COUNT, count, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> StatBuilder<'a, 'b> { - let start = _fbb.start_table(); - StatBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for Stat<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Stat"); - ds.field("id", &self.id()); - ds.field("val", &self.val()); - ds.field("count", &self.count()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct StatT { - pub id: Option, - pub val: i64, - pub count: u16, -} -impl Default for StatT { - fn default() -> Self { - Self { - id: None, - val: 0, - count: 0, - } - } -} -impl StatT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let id = self.id.as_ref().map(|x|{ - _fbb.create_string(x) - }); - let val = self.val; - let count = self.count; - Stat::create(_fbb, &StatArgs{ - id, - val, - count, - }) - } -} -pub enum ReferrableOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct Referrable<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for Referrable<'a> { - type Inner = Referrable<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> Referrable<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Referrable" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - Referrable { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args ReferrableArgs) -> flatbuffers::WIPOffset> { - let mut builder = ReferrableBuilder::new(_fbb); - builder.add_id(args.id); - builder.finish() - } - - pub fn unpack(&self) -> ReferrableT { - let id = self.id(); - ReferrableT { - id, - } - } - pub const VT_ID: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn id(&self) -> u64 { - self._tab.get::(Referrable::VT_ID, Some(0)).unwrap() - } - #[inline] - pub fn key_compare_less_than(&self, o: &Referrable) -> bool { - self.id() < o.id() - } - - #[inline] - pub fn key_compare_with_value(&self, val: u64) -> ::std::cmp::Ordering { - let key = self.id(); - key.cmp(&val) - } -} - -impl flatbuffers::Verifiable for Referrable<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"id", Self::VT_ID, false)? - .finish(); - Ok(()) - } -} -pub struct ReferrableArgs { - pub id: u64, -} -impl<'a> Default for ReferrableArgs { - #[inline] - fn default() -> Self { - ReferrableArgs { - id: 0, - } - } -} -pub struct ReferrableBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> ReferrableBuilder<'a, 'b> { - #[inline] - pub fn add_id(&mut self, id: u64) { - self.fbb_.push_slot::(Referrable::VT_ID, id, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> ReferrableBuilder<'a, 'b> { - let start = _fbb.start_table(); - ReferrableBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for Referrable<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Referrable"); - ds.field("id", &self.id()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct ReferrableT { - pub id: u64, -} -impl Default for ReferrableT { - fn default() -> Self { - Self { - id: 0, - } - } -} -impl ReferrableT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let id = self.id; - Referrable::create(_fbb, &ReferrableArgs{ - id, - }) - } -} -pub enum MonsterOffset {} -#[derive(Copy, Clone, PartialEq)] - -/// an example documentation comment: "monster object" -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, loc } } - } -} - -impl<'a> Monster<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.Monster" - } - - #[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); - builder.add_non_owning_reference(args.non_owning_reference); - builder.add_co_owning_reference(args.co_owning_reference); - builder.add_single_weak_reference(args.single_weak_reference); - builder.add_testhashu64_fnv1a(args.testhashu64_fnv1a); - builder.add_testhashs64_fnv1a(args.testhashs64_fnv1a); - builder.add_testhashu64_fnv1(args.testhashu64_fnv1); - builder.add_testhashs64_fnv1(args.testhashs64_fnv1); - if let Some(x) = args.scalar_key_sorted_tables { builder.add_scalar_key_sorted_tables(x); } - if let Some(x) = args.testrequirednestedflatbuffer { builder.add_testrequirednestedflatbuffer(x); } - if let Some(x) = args.vector_of_enums { builder.add_vector_of_enums(x); } - if let Some(x) = args.any_ambiguous { builder.add_any_ambiguous(x); } - if let Some(x) = args.any_unique { builder.add_any_unique(x); } - if let Some(x) = args.vector_of_non_owning_references { builder.add_vector_of_non_owning_references(x); } - if let Some(x) = args.vector_of_co_owning_references { builder.add_vector_of_co_owning_references(x); } - if let Some(x) = args.vector_of_strong_referrables { builder.add_vector_of_strong_referrables(x); } - if let Some(x) = args.vector_of_weak_references { builder.add_vector_of_weak_references(x); } - if let Some(x) = args.vector_of_referrables { builder.add_vector_of_referrables(x); } - if let Some(x) = args.parent_namespace_test { builder.add_parent_namespace_test(x); } - if let Some(x) = args.vector_of_doubles { builder.add_vector_of_doubles(x); } - if let Some(x) = args.vector_of_longs { builder.add_vector_of_longs(x); } - if let Some(x) = args.test5 { builder.add_test5(x); } - if let Some(x) = args.flex { builder.add_flex(x); } - if let Some(x) = args.testarrayofsortedstruct { builder.add_testarrayofsortedstruct(x); } - if let Some(x) = args.testarrayofstring2 { builder.add_testarrayofstring2(x); } - builder.add_testf3(args.testf3); - builder.add_testf2(args.testf2); - builder.add_testf(args.testf); - if let Some(x) = args.testarrayofbools { builder.add_testarrayofbools(x); } - builder.add_testhashu32_fnv1a(args.testhashu32_fnv1a); - builder.add_testhashs32_fnv1a(args.testhashs32_fnv1a); - builder.add_testhashu32_fnv1(args.testhashu32_fnv1); - builder.add_testhashs32_fnv1(args.testhashs32_fnv1); - if let Some(x) = args.testempty { builder.add_testempty(x); } - if let Some(x) = args.testnestedflatbuffer { builder.add_testnestedflatbuffer(x); } - if let Some(x) = args.enemy { builder.add_enemy(x); } - if let Some(x) = args.testarrayoftables { builder.add_testarrayoftables(x); } - if let Some(x) = args.testarrayofstring { builder.add_testarrayofstring(x); } - if let Some(x) = args.test4 { builder.add_test4(x); } - if let Some(x) = args.test { builder.add_test(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_signed_enum(args.signed_enum); - builder.add_any_ambiguous_type(args.any_ambiguous_type); - builder.add_any_unique_type(args.any_unique_type); - builder.add_testbool(args.testbool); - builder.add_test_type(args.test_type); - builder.add_color(args.color); - builder.finish() - } - - pub fn unpack(&self) -> MonsterT { - let pos = self.pos().map(|x| { - x.unpack() - }); - let mana = self.mana(); - let hp = self.hp(); - let name = { - let x = self.name(); - x.to_string() - }; - let inventory = self.inventory().map(|x| { - x.to_vec() - }); - let color = self.color(); - let test = match self.test_type() { - Any::NONE => AnyT::NONE, - Any::Monster => AnyT::Monster(Box::new( - self.test_as_monster() - .expect("Invalid union table, expected `Any::Monster`.") - .unpack() - )), - Any::TestSimpleTableWithEnum => AnyT::TestSimpleTableWithEnum(Box::new( - self.test_as_test_simple_table_with_enum() - .expect("Invalid union table, expected `Any::TestSimpleTableWithEnum`.") - .unpack() - )), - Any::MyGame_Example2_Monster => AnyT::MyGameExample2Monster(Box::new( - self.test_as_my_game_example_2_monster() - .expect("Invalid union table, expected `Any::MyGame_Example2_Monster`.") - .unpack() - )), - _ => AnyT::NONE, - }; - let test4 = self.test4().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let testarrayofstring = self.testarrayofstring().map(|x| { - x.iter().map(|s| s.to_string()).collect() - }); - let testarrayoftables = self.testarrayoftables().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let enemy = self.enemy().map(|x| { - Box::new(x.unpack()) - }); - let testnestedflatbuffer = self.testnestedflatbuffer().map(|x| { - x.to_vec() - }); - let testempty = self.testempty().map(|x| { - Box::new(x.unpack()) - }); - let testbool = self.testbool(); - let testhashs32_fnv1 = self.testhashs32_fnv1(); - let testhashu32_fnv1 = self.testhashu32_fnv1(); - let testhashs64_fnv1 = self.testhashs64_fnv1(); - let testhashu64_fnv1 = self.testhashu64_fnv1(); - let testhashs32_fnv1a = self.testhashs32_fnv1a(); - let testhashu32_fnv1a = self.testhashu32_fnv1a(); - let testhashs64_fnv1a = self.testhashs64_fnv1a(); - let testhashu64_fnv1a = self.testhashu64_fnv1a(); - let testarrayofbools = self.testarrayofbools().map(|x| { - x.to_vec() - }); - let testf = self.testf(); - let testf2 = self.testf2(); - let testf3 = self.testf3(); - let testarrayofstring2 = self.testarrayofstring2().map(|x| { - x.iter().map(|s| s.to_string()).collect() - }); - let testarrayofsortedstruct = self.testarrayofsortedstruct().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let flex = self.flex().map(|x| { - x.to_vec() - }); - let test5 = self.test5().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let vector_of_longs = self.vector_of_longs().map(|x| { - x.into_iter().collect() - }); - let vector_of_doubles = self.vector_of_doubles().map(|x| { - x.into_iter().collect() - }); - let parent_namespace_test = self.parent_namespace_test().map(|x| { - Box::new(x.unpack()) - }); - let vector_of_referrables = self.vector_of_referrables().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let single_weak_reference = self.single_weak_reference(); - let vector_of_weak_references = self.vector_of_weak_references().map(|x| { - x.into_iter().collect() - }); - let vector_of_strong_referrables = self.vector_of_strong_referrables().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - let co_owning_reference = self.co_owning_reference(); - let vector_of_co_owning_references = self.vector_of_co_owning_references().map(|x| { - x.into_iter().collect() - }); - let non_owning_reference = self.non_owning_reference(); - let vector_of_non_owning_references = self.vector_of_non_owning_references().map(|x| { - x.into_iter().collect() - }); - let any_unique = match self.any_unique_type() { - AnyUniqueAliases::NONE => AnyUniqueAliasesT::NONE, - AnyUniqueAliases::M => AnyUniqueAliasesT::M(Box::new( - self.any_unique_as_m() - .expect("Invalid union table, expected `AnyUniqueAliases::M`.") - .unpack() - )), - AnyUniqueAliases::TS => AnyUniqueAliasesT::TS(Box::new( - self.any_unique_as_ts() - .expect("Invalid union table, expected `AnyUniqueAliases::TS`.") - .unpack() - )), - AnyUniqueAliases::M2 => AnyUniqueAliasesT::M2(Box::new( - self.any_unique_as_m2() - .expect("Invalid union table, expected `AnyUniqueAliases::M2`.") - .unpack() - )), - _ => AnyUniqueAliasesT::NONE, - }; - let any_ambiguous = match self.any_ambiguous_type() { - AnyAmbiguousAliases::NONE => AnyAmbiguousAliasesT::NONE, - AnyAmbiguousAliases::M1 => AnyAmbiguousAliasesT::M1(Box::new( - self.any_ambiguous_as_m1() - .expect("Invalid union table, expected `AnyAmbiguousAliases::M1`.") - .unpack() - )), - AnyAmbiguousAliases::M2 => AnyAmbiguousAliasesT::M2(Box::new( - self.any_ambiguous_as_m2() - .expect("Invalid union table, expected `AnyAmbiguousAliases::M2`.") - .unpack() - )), - AnyAmbiguousAliases::M3 => AnyAmbiguousAliasesT::M3(Box::new( - self.any_ambiguous_as_m3() - .expect("Invalid union table, expected `AnyAmbiguousAliases::M3`.") - .unpack() - )), - _ => AnyAmbiguousAliasesT::NONE, - }; - let vector_of_enums = self.vector_of_enums().map(|x| { - x.into_iter().collect() - }); - let signed_enum = self.signed_enum(); - let testrequirednestedflatbuffer = self.testrequirednestedflatbuffer().map(|x| { - x.to_vec() - }); - let scalar_key_sorted_tables = self.scalar_key_sorted_tables().map(|x| { - x.iter().map(|t| t.unpack()).collect() - }); - MonsterT { - pos, - mana, - hp, - name, - inventory, - color, - test, - test4, - testarrayofstring, - testarrayoftables, - enemy, - testnestedflatbuffer, - testempty, - testbool, - testhashs32_fnv1, - testhashu32_fnv1, - testhashs64_fnv1, - testhashu64_fnv1, - testhashs32_fnv1a, - testhashu32_fnv1a, - testhashs64_fnv1a, - testhashu64_fnv1a, - testarrayofbools, - testf, - testf2, - testf3, - testarrayofstring2, - testarrayofsortedstruct, - flex, - test5, - vector_of_longs, - vector_of_doubles, - parent_namespace_test, - vector_of_referrables, - single_weak_reference, - vector_of_weak_references, - vector_of_strong_referrables, - co_owning_reference, - vector_of_co_owning_references, - non_owning_reference, - vector_of_non_owning_references, - any_unique, - any_ambiguous, - vector_of_enums, - signed_enum, - testrequirednestedflatbuffer, - scalar_key_sorted_tables, - } - } - 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_TEST_TYPE: flatbuffers::VOffsetT = 18; - pub const VT_TEST: flatbuffers::VOffsetT = 20; - pub const VT_TEST4: flatbuffers::VOffsetT = 22; - pub const VT_TESTARRAYOFSTRING: flatbuffers::VOffsetT = 24; - pub const VT_TESTARRAYOFTABLES: flatbuffers::VOffsetT = 26; - pub const VT_ENEMY: flatbuffers::VOffsetT = 28; - pub const VT_TESTNESTEDFLATBUFFER: flatbuffers::VOffsetT = 30; - pub const VT_TESTEMPTY: flatbuffers::VOffsetT = 32; - pub const VT_TESTBOOL: flatbuffers::VOffsetT = 34; - pub const VT_TESTHASHS32_FNV1: flatbuffers::VOffsetT = 36; - pub const VT_TESTHASHU32_FNV1: flatbuffers::VOffsetT = 38; - pub const VT_TESTHASHS64_FNV1: flatbuffers::VOffsetT = 40; - pub const VT_TESTHASHU64_FNV1: flatbuffers::VOffsetT = 42; - pub const VT_TESTHASHS32_FNV1A: flatbuffers::VOffsetT = 44; - pub const VT_TESTHASHU32_FNV1A: flatbuffers::VOffsetT = 46; - pub const VT_TESTHASHS64_FNV1A: flatbuffers::VOffsetT = 48; - pub const VT_TESTHASHU64_FNV1A: flatbuffers::VOffsetT = 50; - pub const VT_TESTARRAYOFBOOLS: flatbuffers::VOffsetT = 52; - pub const VT_TESTF: flatbuffers::VOffsetT = 54; - pub const VT_TESTF2: flatbuffers::VOffsetT = 56; - pub const VT_TESTF3: flatbuffers::VOffsetT = 58; - pub const VT_TESTARRAYOFSTRING2: flatbuffers::VOffsetT = 60; - pub const VT_TESTARRAYOFSORTEDSTRUCT: flatbuffers::VOffsetT = 62; - pub const VT_FLEX: flatbuffers::VOffsetT = 64; - pub const VT_TEST5: flatbuffers::VOffsetT = 66; - pub const VT_VECTOR_OF_LONGS: flatbuffers::VOffsetT = 68; - pub const VT_VECTOR_OF_DOUBLES: flatbuffers::VOffsetT = 70; - pub const VT_PARENT_NAMESPACE_TEST: flatbuffers::VOffsetT = 72; - pub const VT_VECTOR_OF_REFERRABLES: flatbuffers::VOffsetT = 74; - pub const VT_SINGLE_WEAK_REFERENCE: flatbuffers::VOffsetT = 76; - pub const VT_VECTOR_OF_WEAK_REFERENCES: flatbuffers::VOffsetT = 78; - pub const VT_VECTOR_OF_STRONG_REFERRABLES: flatbuffers::VOffsetT = 80; - pub const VT_CO_OWNING_REFERENCE: flatbuffers::VOffsetT = 82; - pub const VT_VECTOR_OF_CO_OWNING_REFERENCES: flatbuffers::VOffsetT = 84; - pub const VT_NON_OWNING_REFERENCE: flatbuffers::VOffsetT = 86; - pub const VT_VECTOR_OF_NON_OWNING_REFERENCES: flatbuffers::VOffsetT = 88; - pub const VT_ANY_UNIQUE_TYPE: flatbuffers::VOffsetT = 90; - pub const VT_ANY_UNIQUE: flatbuffers::VOffsetT = 92; - pub const VT_ANY_AMBIGUOUS_TYPE: flatbuffers::VOffsetT = 94; - pub const VT_ANY_AMBIGUOUS: flatbuffers::VOffsetT = 96; - pub const VT_VECTOR_OF_ENUMS: flatbuffers::VOffsetT = 98; - pub const VT_SIGNED_ENUM: flatbuffers::VOffsetT = 100; - pub const VT_TESTREQUIREDNESTEDFLATBUFFER: flatbuffers::VOffsetT = 102; - pub const VT_SCALAR_KEY_SORTED_TABLES: flatbuffers::VOffsetT = 104; - - #[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) -> &'a str { - self._tab.get::>(Monster::VT_NAME, None).unwrap() - } - #[inline] - pub fn key_compare_less_than(&self, o: &Monster) -> bool { - self.name() < o.name() - } - - #[inline] - pub fn key_compare_with_value(&self, val: & str) -> ::std::cmp::Ordering { - let key = self.name(); - key.cmp(&val) - } - #[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 test_type(&self) -> Any { - self._tab.get::(Monster::VT_TEST_TYPE, Some(Any::NONE)).unwrap() - } - #[inline] - pub fn test(&self) -> Option> { - self._tab.get::>>(Monster::VT_TEST, None) - } - #[inline] - pub fn test4(&self) -> Option<&'a [Test]> { - self._tab.get::>>(Monster::VT_TEST4, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn testarrayofstring(&self) -> Option>> { - self._tab.get::>>>(Monster::VT_TESTARRAYOFSTRING, None) - } - /// an example documentation comment: this will end up in the generated code - /// multiline too - #[inline] - pub fn testarrayoftables(&self) -> Option>>> { - self._tab.get::>>>(Monster::VT_TESTARRAYOFTABLES, None) - } - #[inline] - pub fn enemy(&self) -> Option> { - self._tab.get::>(Monster::VT_ENEMY, None) - } - #[inline] - pub fn testnestedflatbuffer(&self) -> Option<&'a [u8]> { - self._tab.get::>>(Monster::VT_TESTNESTEDFLATBUFFER, None).map(|v| v.safe_slice()) - } - pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option> { - self.testnestedflatbuffer().map(|data| { - use flatbuffers::Follow; - >>::follow(data, 0) - }) - } - #[inline] - pub fn testempty(&self) -> Option> { - self._tab.get::>(Monster::VT_TESTEMPTY, None) - } - #[inline] - pub fn testbool(&self) -> bool { - self._tab.get::(Monster::VT_TESTBOOL, Some(false)).unwrap() - } - #[inline] - pub fn testhashs32_fnv1(&self) -> i32 { - self._tab.get::(Monster::VT_TESTHASHS32_FNV1, Some(0)).unwrap() - } - #[inline] - pub fn testhashu32_fnv1(&self) -> u32 { - self._tab.get::(Monster::VT_TESTHASHU32_FNV1, Some(0)).unwrap() - } - #[inline] - pub fn testhashs64_fnv1(&self) -> i64 { - self._tab.get::(Monster::VT_TESTHASHS64_FNV1, Some(0)).unwrap() - } - #[inline] - pub fn testhashu64_fnv1(&self) -> u64 { - self._tab.get::(Monster::VT_TESTHASHU64_FNV1, Some(0)).unwrap() - } - #[inline] - pub fn testhashs32_fnv1a(&self) -> i32 { - self._tab.get::(Monster::VT_TESTHASHS32_FNV1A, Some(0)).unwrap() - } - #[inline] - pub fn testhashu32_fnv1a(&self) -> u32 { - self._tab.get::(Monster::VT_TESTHASHU32_FNV1A, Some(0)).unwrap() - } - #[inline] - pub fn testhashs64_fnv1a(&self) -> i64 { - self._tab.get::(Monster::VT_TESTHASHS64_FNV1A, Some(0)).unwrap() - } - #[inline] - pub fn testhashu64_fnv1a(&self) -> u64 { - self._tab.get::(Monster::VT_TESTHASHU64_FNV1A, Some(0)).unwrap() - } - #[inline] - pub fn testarrayofbools(&self) -> Option<&'a [bool]> { - self._tab.get::>>(Monster::VT_TESTARRAYOFBOOLS, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn testf(&self) -> f32 { - self._tab.get::(Monster::VT_TESTF, Some(3.14159)).unwrap() - } - #[inline] - pub fn testf2(&self) -> f32 { - self._tab.get::(Monster::VT_TESTF2, Some(3.0)).unwrap() - } - #[inline] - pub fn testf3(&self) -> f32 { - self._tab.get::(Monster::VT_TESTF3, Some(0.0)).unwrap() - } - #[inline] - pub fn testarrayofstring2(&self) -> Option>> { - self._tab.get::>>>(Monster::VT_TESTARRAYOFSTRING2, None) - } - #[inline] - pub fn testarrayofsortedstruct(&self) -> Option<&'a [Ability]> { - self._tab.get::>>(Monster::VT_TESTARRAYOFSORTEDSTRUCT, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn flex(&self) -> Option<&'a [u8]> { - self._tab.get::>>(Monster::VT_FLEX, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn test5(&self) -> Option<&'a [Test]> { - self._tab.get::>>(Monster::VT_TEST5, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn vector_of_longs(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_LONGS, None) - } - #[inline] - pub fn vector_of_doubles(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_DOUBLES, None) - } - #[inline] - pub fn parent_namespace_test(&self) -> Option> { - self._tab.get::>(Monster::VT_PARENT_NAMESPACE_TEST, None) - } - #[inline] - pub fn vector_of_referrables(&self) -> Option>>> { - self._tab.get::>>>(Monster::VT_VECTOR_OF_REFERRABLES, None) - } - #[inline] - pub fn single_weak_reference(&self) -> u64 { - self._tab.get::(Monster::VT_SINGLE_WEAK_REFERENCE, Some(0)).unwrap() - } - #[inline] - pub fn vector_of_weak_references(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_WEAK_REFERENCES, None) - } - #[inline] - pub fn vector_of_strong_referrables(&self) -> Option>>> { - self._tab.get::>>>(Monster::VT_VECTOR_OF_STRONG_REFERRABLES, None) - } - #[inline] - pub fn co_owning_reference(&self) -> u64 { - self._tab.get::(Monster::VT_CO_OWNING_REFERENCE, Some(0)).unwrap() - } - #[inline] - pub fn vector_of_co_owning_references(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_CO_OWNING_REFERENCES, None) - } - #[inline] - pub fn non_owning_reference(&self) -> u64 { - self._tab.get::(Monster::VT_NON_OWNING_REFERENCE, Some(0)).unwrap() - } - #[inline] - pub fn vector_of_non_owning_references(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, None) - } - #[inline] - pub fn any_unique_type(&self) -> AnyUniqueAliases { - self._tab.get::(Monster::VT_ANY_UNIQUE_TYPE, Some(AnyUniqueAliases::NONE)).unwrap() - } - #[inline] - pub fn any_unique(&self) -> Option> { - self._tab.get::>>(Monster::VT_ANY_UNIQUE, None) - } - #[inline] - pub fn any_ambiguous_type(&self) -> AnyAmbiguousAliases { - self._tab.get::(Monster::VT_ANY_AMBIGUOUS_TYPE, Some(AnyAmbiguousAliases::NONE)).unwrap() - } - #[inline] - pub fn any_ambiguous(&self) -> Option> { - self._tab.get::>>(Monster::VT_ANY_AMBIGUOUS, None) - } - #[inline] - pub fn vector_of_enums(&self) -> Option> { - self._tab.get::>>(Monster::VT_VECTOR_OF_ENUMS, None) - } - #[inline] - pub fn signed_enum(&self) -> Race { - self._tab.get::(Monster::VT_SIGNED_ENUM, Some(Race::None)).unwrap() - } - #[inline] - pub fn testrequirednestedflatbuffer(&self) -> Option<&'a [u8]> { - self._tab.get::>>(Monster::VT_TESTREQUIREDNESTEDFLATBUFFER, None).map(|v| v.safe_slice()) - } - pub fn testrequirednestedflatbuffer_nested_flatbuffer(&'a self) -> Option> { - self.testrequirednestedflatbuffer().map(|data| { - use flatbuffers::Follow; - >>::follow(data, 0) - }) - } - #[inline] - pub fn scalar_key_sorted_tables(&self) -> Option>>> { - self._tab.get::>>>(Monster::VT_SCALAR_KEY_SORTED_TABLES, None) - } - #[inline] - #[allow(non_snake_case)] - pub fn test_as_monster(&self) -> Option> { - if self.test_type() == Any::Monster { - self.test().map(Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn test_as_test_simple_table_with_enum(&self) -> Option> { - if self.test_type() == Any::TestSimpleTableWithEnum { - self.test().map(TestSimpleTableWithEnum::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn test_as_my_game_example_2_monster(&self) -> Option> { - if self.test_type() == Any::MyGame_Example2_Monster { - self.test().map(super::example_2::Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_unique_as_m(&self) -> Option> { - if self.any_unique_type() == AnyUniqueAliases::M { - self.any_unique().map(Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_unique_as_ts(&self) -> Option> { - if self.any_unique_type() == AnyUniqueAliases::TS { - self.any_unique().map(TestSimpleTableWithEnum::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_unique_as_m2(&self) -> Option> { - if self.any_unique_type() == AnyUniqueAliases::M2 { - self.any_unique().map(super::example_2::Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_ambiguous_as_m1(&self) -> Option> { - if self.any_ambiguous_type() == AnyAmbiguousAliases::M1 { - self.any_ambiguous().map(Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_ambiguous_as_m2(&self) -> Option> { - if self.any_ambiguous_type() == AnyAmbiguousAliases::M2 { - self.any_ambiguous().map(Monster::init_from_table) - } else { - None - } - } - - #[inline] - #[allow(non_snake_case)] - pub fn any_ambiguous_as_m3(&self) -> Option> { - if self.any_ambiguous_type() == AnyAmbiguousAliases::M3 { - self.any_ambiguous().map(Monster::init_from_table) - } else { - None - } - } - -} - -impl flatbuffers::Verifiable for Monster<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"pos", Self::VT_POS, false)? - .visit_field::(&"mana", Self::VT_MANA, false)? - .visit_field::(&"hp", Self::VT_HP, false)? - .visit_field::>(&"name", Self::VT_NAME, true)? - .visit_field::>>(&"inventory", Self::VT_INVENTORY, false)? - .visit_field::(&"color", Self::VT_COLOR, false)? - .visit_union::(&"test_type", Self::VT_TEST_TYPE, &"test", Self::VT_TEST, false, |key, v, pos| { - match key { - Any::Monster => v.verify_union_variant::>("Any::Monster", pos), - Any::TestSimpleTableWithEnum => v.verify_union_variant::>("Any::TestSimpleTableWithEnum", pos), - Any::MyGame_Example2_Monster => v.verify_union_variant::>("Any::MyGame_Example2_Monster", pos), - _ => Ok(()), - } - })? - .visit_field::>>(&"test4", Self::VT_TEST4, false)? - .visit_field::>>>(&"testarrayofstring", Self::VT_TESTARRAYOFSTRING, false)? - .visit_field::>>>(&"testarrayoftables", Self::VT_TESTARRAYOFTABLES, false)? - .visit_field::>(&"enemy", Self::VT_ENEMY, false)? - .visit_field::>>(&"testnestedflatbuffer", Self::VT_TESTNESTEDFLATBUFFER, false)? - .visit_field::>(&"testempty", Self::VT_TESTEMPTY, false)? - .visit_field::(&"testbool", Self::VT_TESTBOOL, false)? - .visit_field::(&"testhashs32_fnv1", Self::VT_TESTHASHS32_FNV1, false)? - .visit_field::(&"testhashu32_fnv1", Self::VT_TESTHASHU32_FNV1, false)? - .visit_field::(&"testhashs64_fnv1", Self::VT_TESTHASHS64_FNV1, false)? - .visit_field::(&"testhashu64_fnv1", Self::VT_TESTHASHU64_FNV1, false)? - .visit_field::(&"testhashs32_fnv1a", Self::VT_TESTHASHS32_FNV1A, false)? - .visit_field::(&"testhashu32_fnv1a", Self::VT_TESTHASHU32_FNV1A, false)? - .visit_field::(&"testhashs64_fnv1a", Self::VT_TESTHASHS64_FNV1A, false)? - .visit_field::(&"testhashu64_fnv1a", Self::VT_TESTHASHU64_FNV1A, false)? - .visit_field::>>(&"testarrayofbools", Self::VT_TESTARRAYOFBOOLS, false)? - .visit_field::(&"testf", Self::VT_TESTF, false)? - .visit_field::(&"testf2", Self::VT_TESTF2, false)? - .visit_field::(&"testf3", Self::VT_TESTF3, false)? - .visit_field::>>>(&"testarrayofstring2", Self::VT_TESTARRAYOFSTRING2, false)? - .visit_field::>>(&"testarrayofsortedstruct", Self::VT_TESTARRAYOFSORTEDSTRUCT, false)? - .visit_field::>>(&"flex", Self::VT_FLEX, false)? - .visit_field::>>(&"test5", Self::VT_TEST5, false)? - .visit_field::>>(&"vector_of_longs", Self::VT_VECTOR_OF_LONGS, false)? - .visit_field::>>(&"vector_of_doubles", Self::VT_VECTOR_OF_DOUBLES, false)? - .visit_field::>(&"parent_namespace_test", Self::VT_PARENT_NAMESPACE_TEST, false)? - .visit_field::>>>(&"vector_of_referrables", Self::VT_VECTOR_OF_REFERRABLES, false)? - .visit_field::(&"single_weak_reference", Self::VT_SINGLE_WEAK_REFERENCE, false)? - .visit_field::>>(&"vector_of_weak_references", Self::VT_VECTOR_OF_WEAK_REFERENCES, false)? - .visit_field::>>>(&"vector_of_strong_referrables", Self::VT_VECTOR_OF_STRONG_REFERRABLES, false)? - .visit_field::(&"co_owning_reference", Self::VT_CO_OWNING_REFERENCE, false)? - .visit_field::>>(&"vector_of_co_owning_references", Self::VT_VECTOR_OF_CO_OWNING_REFERENCES, false)? - .visit_field::(&"non_owning_reference", Self::VT_NON_OWNING_REFERENCE, false)? - .visit_field::>>(&"vector_of_non_owning_references", Self::VT_VECTOR_OF_NON_OWNING_REFERENCES, false)? - .visit_union::(&"any_unique_type", Self::VT_ANY_UNIQUE_TYPE, &"any_unique", Self::VT_ANY_UNIQUE, false, |key, v, pos| { - match key { - AnyUniqueAliases::M => v.verify_union_variant::>("AnyUniqueAliases::M", pos), - AnyUniqueAliases::TS => v.verify_union_variant::>("AnyUniqueAliases::TS", pos), - AnyUniqueAliases::M2 => v.verify_union_variant::>("AnyUniqueAliases::M2", pos), - _ => Ok(()), - } - })? - .visit_union::(&"any_ambiguous_type", Self::VT_ANY_AMBIGUOUS_TYPE, &"any_ambiguous", Self::VT_ANY_AMBIGUOUS, false, |key, v, pos| { - match key { - AnyAmbiguousAliases::M1 => v.verify_union_variant::>("AnyAmbiguousAliases::M1", pos), - AnyAmbiguousAliases::M2 => v.verify_union_variant::>("AnyAmbiguousAliases::M2", pos), - AnyAmbiguousAliases::M3 => v.verify_union_variant::>("AnyAmbiguousAliases::M3", pos), - _ => Ok(()), - } - })? - .visit_field::>>(&"vector_of_enums", Self::VT_VECTOR_OF_ENUMS, false)? - .visit_field::(&"signed_enum", Self::VT_SIGNED_ENUM, false)? - .visit_field::>>(&"testrequirednestedflatbuffer", Self::VT_TESTREQUIREDNESTEDFLATBUFFER, false)? - .visit_field::>>>(&"scalar_key_sorted_tables", Self::VT_SCALAR_KEY_SORTED_TABLES, false)? - .finish(); - Ok(()) - } -} -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 test_type: Any, - pub test: Option>, - pub test4: Option>>, - pub testarrayofstring: Option>>>, - pub testarrayoftables: Option>>>>, - pub enemy: Option>>, - pub testnestedflatbuffer: Option>>, - pub testempty: Option>>, - pub testbool: bool, - pub testhashs32_fnv1: i32, - pub testhashu32_fnv1: u32, - pub testhashs64_fnv1: i64, - pub testhashu64_fnv1: u64, - pub testhashs32_fnv1a: i32, - pub testhashu32_fnv1a: u32, - pub testhashs64_fnv1a: i64, - pub testhashu64_fnv1a: u64, - pub testarrayofbools: Option>>, - pub testf: f32, - pub testf2: f32, - pub testf3: f32, - pub testarrayofstring2: Option>>>, - pub testarrayofsortedstruct: Option>>, - pub flex: Option>>, - pub test5: Option>>, - pub vector_of_longs: Option>>, - pub vector_of_doubles: Option>>, - pub parent_namespace_test: Option>>, - pub vector_of_referrables: Option>>>>, - pub single_weak_reference: u64, - pub vector_of_weak_references: Option>>, - pub vector_of_strong_referrables: Option>>>>, - pub co_owning_reference: u64, - pub vector_of_co_owning_references: Option>>, - pub non_owning_reference: u64, - pub vector_of_non_owning_references: Option>>, - pub any_unique_type: AnyUniqueAliases, - pub any_unique: Option>, - pub any_ambiguous_type: AnyAmbiguousAliases, - pub any_ambiguous: Option>, - pub vector_of_enums: Option>>, - pub signed_enum: Race, - pub testrequirednestedflatbuffer: Option>>, - pub scalar_key_sorted_tables: Option>>>>, -} -impl<'a> Default for MonsterArgs<'a> { - #[inline] - fn default() -> Self { - MonsterArgs { - pos: None, - mana: 150, - hp: 100, - name: None, // required field - inventory: None, - color: Color::Blue, - test_type: Any::NONE, - test: None, - test4: None, - testarrayofstring: None, - testarrayoftables: None, - enemy: None, - testnestedflatbuffer: None, - testempty: None, - testbool: false, - testhashs32_fnv1: 0, - testhashu32_fnv1: 0, - testhashs64_fnv1: 0, - testhashu64_fnv1: 0, - testhashs32_fnv1a: 0, - testhashu32_fnv1a: 0, - testhashs64_fnv1a: 0, - testhashu64_fnv1a: 0, - testarrayofbools: None, - testf: 3.14159, - testf2: 3.0, - testf3: 0.0, - testarrayofstring2: None, - testarrayofsortedstruct: None, - flex: None, - test5: None, - vector_of_longs: None, - vector_of_doubles: None, - parent_namespace_test: None, - vector_of_referrables: None, - single_weak_reference: 0, - vector_of_weak_references: None, - vector_of_strong_referrables: None, - co_owning_reference: 0, - vector_of_co_owning_references: None, - non_owning_reference: 0, - vector_of_non_owning_references: None, - any_unique_type: AnyUniqueAliases::NONE, - any_unique: None, - any_ambiguous_type: AnyAmbiguousAliases::NONE, - any_ambiguous: None, - vector_of_enums: None, - signed_enum: Race::None, - testrequirednestedflatbuffer: None, - scalar_key_sorted_tables: 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: &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_test_type(&mut self, test_type: Any) { - self.fbb_.push_slot::(Monster::VT_TEST_TYPE, test_type, Any::NONE); - } - #[inline] - pub fn add_test(&mut self, test: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(Monster::VT_TEST, test); - } - #[inline] - pub fn add_test4(&mut self, test4: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TEST4, test4); - } - #[inline] - pub fn add_testarrayofstring(&mut self, testarrayofstring: flatbuffers::WIPOffset>>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSTRING, testarrayofstring); - } - #[inline] - pub fn add_testarrayoftables(&mut self, testarrayoftables: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFTABLES, testarrayoftables); - } - #[inline] - pub fn add_enemy(&mut self, enemy: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_ENEMY, enemy); - } - #[inline] - pub fn add_testnestedflatbuffer(&mut self, testnestedflatbuffer: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTNESTEDFLATBUFFER, testnestedflatbuffer); - } - #[inline] - pub fn add_testempty(&mut self, testempty: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTEMPTY, testempty); - } - #[inline] - pub fn add_testbool(&mut self, testbool: bool) { - self.fbb_.push_slot::(Monster::VT_TESTBOOL, testbool, false); - } - #[inline] - pub fn add_testhashs32_fnv1(&mut self, testhashs32_fnv1: i32) { - self.fbb_.push_slot::(Monster::VT_TESTHASHS32_FNV1, testhashs32_fnv1, 0); - } - #[inline] - pub fn add_testhashu32_fnv1(&mut self, testhashu32_fnv1: u32) { - self.fbb_.push_slot::(Monster::VT_TESTHASHU32_FNV1, testhashu32_fnv1, 0); - } - #[inline] - pub fn add_testhashs64_fnv1(&mut self, testhashs64_fnv1: i64) { - self.fbb_.push_slot::(Monster::VT_TESTHASHS64_FNV1, testhashs64_fnv1, 0); - } - #[inline] - pub fn add_testhashu64_fnv1(&mut self, testhashu64_fnv1: u64) { - self.fbb_.push_slot::(Monster::VT_TESTHASHU64_FNV1, testhashu64_fnv1, 0); - } - #[inline] - pub fn add_testhashs32_fnv1a(&mut self, testhashs32_fnv1a: i32) { - self.fbb_.push_slot::(Monster::VT_TESTHASHS32_FNV1A, testhashs32_fnv1a, 0); - } - #[inline] - pub fn add_testhashu32_fnv1a(&mut self, testhashu32_fnv1a: u32) { - self.fbb_.push_slot::(Monster::VT_TESTHASHU32_FNV1A, testhashu32_fnv1a, 0); - } - #[inline] - pub fn add_testhashs64_fnv1a(&mut self, testhashs64_fnv1a: i64) { - self.fbb_.push_slot::(Monster::VT_TESTHASHS64_FNV1A, testhashs64_fnv1a, 0); - } - #[inline] - pub fn add_testhashu64_fnv1a(&mut self, testhashu64_fnv1a: u64) { - self.fbb_.push_slot::(Monster::VT_TESTHASHU64_FNV1A, testhashu64_fnv1a, 0); - } - #[inline] - pub fn add_testarrayofbools(&mut self, testarrayofbools: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFBOOLS, testarrayofbools); - } - #[inline] - pub fn add_testf(&mut self, testf: f32) { - self.fbb_.push_slot::(Monster::VT_TESTF, testf, 3.14159); - } - #[inline] - pub fn add_testf2(&mut self, testf2: f32) { - self.fbb_.push_slot::(Monster::VT_TESTF2, testf2, 3.0); - } - #[inline] - pub fn add_testf3(&mut self, testf3: f32) { - self.fbb_.push_slot::(Monster::VT_TESTF3, testf3, 0.0); - } - #[inline] - pub fn add_testarrayofstring2(&mut self, testarrayofstring2: flatbuffers::WIPOffset>>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSTRING2, testarrayofstring2); - } - #[inline] - pub fn add_testarrayofsortedstruct(&mut self, testarrayofsortedstruct: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTARRAYOFSORTEDSTRUCT, testarrayofsortedstruct); - } - #[inline] - pub fn add_flex(&mut self, flex: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_FLEX, flex); - } - #[inline] - pub fn add_test5(&mut self, test5: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TEST5, test5); - } - #[inline] - pub fn add_vector_of_longs(&mut self, vector_of_longs: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_LONGS, vector_of_longs); - } - #[inline] - pub fn add_vector_of_doubles(&mut self, vector_of_doubles: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_DOUBLES, vector_of_doubles); - } - #[inline] - pub fn add_parent_namespace_test(&mut self, parent_namespace_test: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_PARENT_NAMESPACE_TEST, parent_namespace_test); - } - #[inline] - pub fn add_vector_of_referrables(&mut self, vector_of_referrables: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_REFERRABLES, vector_of_referrables); - } - #[inline] - pub fn add_single_weak_reference(&mut self, single_weak_reference: u64) { - self.fbb_.push_slot::(Monster::VT_SINGLE_WEAK_REFERENCE, single_weak_reference, 0); - } - #[inline] - pub fn add_vector_of_weak_references(&mut self, vector_of_weak_references: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_WEAK_REFERENCES, vector_of_weak_references); - } - #[inline] - pub fn add_vector_of_strong_referrables(&mut self, vector_of_strong_referrables: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_STRONG_REFERRABLES, vector_of_strong_referrables); - } - #[inline] - pub fn add_co_owning_reference(&mut self, co_owning_reference: u64) { - self.fbb_.push_slot::(Monster::VT_CO_OWNING_REFERENCE, co_owning_reference, 0); - } - #[inline] - pub fn add_vector_of_co_owning_references(&mut self, vector_of_co_owning_references: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_CO_OWNING_REFERENCES, vector_of_co_owning_references); - } - #[inline] - pub fn add_non_owning_reference(&mut self, non_owning_reference: u64) { - self.fbb_.push_slot::(Monster::VT_NON_OWNING_REFERENCE, non_owning_reference, 0); - } - #[inline] - pub fn add_vector_of_non_owning_references(&mut self, vector_of_non_owning_references: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references); - } - #[inline] - pub fn add_any_unique_type(&mut self, any_unique_type: AnyUniqueAliases) { - self.fbb_.push_slot::(Monster::VT_ANY_UNIQUE_TYPE, any_unique_type, AnyUniqueAliases::NONE); - } - #[inline] - pub fn add_any_unique(&mut self, any_unique: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(Monster::VT_ANY_UNIQUE, any_unique); - } - #[inline] - pub fn add_any_ambiguous_type(&mut self, any_ambiguous_type: AnyAmbiguousAliases) { - self.fbb_.push_slot::(Monster::VT_ANY_AMBIGUOUS_TYPE, any_ambiguous_type, AnyAmbiguousAliases::NONE); - } - #[inline] - pub fn add_any_ambiguous(&mut self, any_ambiguous: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); - } - #[inline] - pub fn add_vector_of_enums(&mut self, vector_of_enums: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_VECTOR_OF_ENUMS, vector_of_enums); - } - #[inline] - pub fn add_signed_enum(&mut self, signed_enum: Race) { - self.fbb_.push_slot::(Monster::VT_SIGNED_ENUM, signed_enum, Race::None); - } - #[inline] - pub fn add_testrequirednestedflatbuffer(&mut self, testrequirednestedflatbuffer: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(Monster::VT_TESTREQUIREDNESTEDFLATBUFFER, testrequirednestedflatbuffer); - } - #[inline] - pub fn add_scalar_key_sorted_tables(&mut self, scalar_key_sorted_tables: flatbuffers::WIPOffset>>>) { - self.fbb_.push_slot_always::>(Monster::VT_SCALAR_KEY_SORTED_TABLES, scalar_key_sorted_tables); - } - #[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_); - self.fbb_.required(o, Monster::VT_NAME,"name"); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for Monster<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("Monster"); - ds.field("pos", &self.pos()); - ds.field("mana", &self.mana()); - ds.field("hp", &self.hp()); - ds.field("name", &self.name()); - ds.field("inventory", &self.inventory()); - ds.field("color", &self.color()); - ds.field("test_type", &self.test_type()); - match self.test_type() { - Any::Monster => { - if let Some(x) = self.test_as_monster() { - ds.field("test", &x) - } else { - ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - Any::TestSimpleTableWithEnum => { - if let Some(x) = self.test_as_test_simple_table_with_enum() { - ds.field("test", &x) - } else { - ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - Any::MyGame_Example2_Monster => { - if let Some(x) = self.test_as_my_game_example_2_monster() { - ds.field("test", &x) - } else { - ds.field("test", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - _ => { - let x: Option<()> = None; - ds.field("test", &x) - }, - }; - ds.field("test4", &self.test4()); - ds.field("testarrayofstring", &self.testarrayofstring()); - ds.field("testarrayoftables", &self.testarrayoftables()); - ds.field("enemy", &self.enemy()); - ds.field("testnestedflatbuffer", &self.testnestedflatbuffer()); - ds.field("testempty", &self.testempty()); - ds.field("testbool", &self.testbool()); - ds.field("testhashs32_fnv1", &self.testhashs32_fnv1()); - ds.field("testhashu32_fnv1", &self.testhashu32_fnv1()); - ds.field("testhashs64_fnv1", &self.testhashs64_fnv1()); - ds.field("testhashu64_fnv1", &self.testhashu64_fnv1()); - ds.field("testhashs32_fnv1a", &self.testhashs32_fnv1a()); - ds.field("testhashu32_fnv1a", &self.testhashu32_fnv1a()); - ds.field("testhashs64_fnv1a", &self.testhashs64_fnv1a()); - ds.field("testhashu64_fnv1a", &self.testhashu64_fnv1a()); - ds.field("testarrayofbools", &self.testarrayofbools()); - ds.field("testf", &self.testf()); - ds.field("testf2", &self.testf2()); - ds.field("testf3", &self.testf3()); - ds.field("testarrayofstring2", &self.testarrayofstring2()); - ds.field("testarrayofsortedstruct", &self.testarrayofsortedstruct()); - ds.field("flex", &self.flex()); - ds.field("test5", &self.test5()); - ds.field("vector_of_longs", &self.vector_of_longs()); - ds.field("vector_of_doubles", &self.vector_of_doubles()); - ds.field("parent_namespace_test", &self.parent_namespace_test()); - ds.field("vector_of_referrables", &self.vector_of_referrables()); - ds.field("single_weak_reference", &self.single_weak_reference()); - ds.field("vector_of_weak_references", &self.vector_of_weak_references()); - ds.field("vector_of_strong_referrables", &self.vector_of_strong_referrables()); - ds.field("co_owning_reference", &self.co_owning_reference()); - ds.field("vector_of_co_owning_references", &self.vector_of_co_owning_references()); - ds.field("non_owning_reference", &self.non_owning_reference()); - ds.field("vector_of_non_owning_references", &self.vector_of_non_owning_references()); - ds.field("any_unique_type", &self.any_unique_type()); - match self.any_unique_type() { - AnyUniqueAliases::M => { - if let Some(x) = self.any_unique_as_m() { - ds.field("any_unique", &x) - } else { - ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - AnyUniqueAliases::TS => { - if let Some(x) = self.any_unique_as_ts() { - ds.field("any_unique", &x) - } else { - ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - AnyUniqueAliases::M2 => { - if let Some(x) = self.any_unique_as_m2() { - ds.field("any_unique", &x) - } else { - ds.field("any_unique", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - _ => { - let x: Option<()> = None; - ds.field("any_unique", &x) - }, - }; - ds.field("any_ambiguous_type", &self.any_ambiguous_type()); - match self.any_ambiguous_type() { - AnyAmbiguousAliases::M1 => { - if let Some(x) = self.any_ambiguous_as_m1() { - ds.field("any_ambiguous", &x) - } else { - ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - AnyAmbiguousAliases::M2 => { - if let Some(x) = self.any_ambiguous_as_m2() { - ds.field("any_ambiguous", &x) - } else { - ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - AnyAmbiguousAliases::M3 => { - if let Some(x) = self.any_ambiguous_as_m3() { - ds.field("any_ambiguous", &x) - } else { - ds.field("any_ambiguous", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - _ => { - let x: Option<()> = None; - ds.field("any_ambiguous", &x) - }, - }; - ds.field("vector_of_enums", &self.vector_of_enums()); - ds.field("signed_enum", &self.signed_enum()); - ds.field("testrequirednestedflatbuffer", &self.testrequirednestedflatbuffer()); - ds.field("scalar_key_sorted_tables", &self.scalar_key_sorted_tables()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct MonsterT { - pub pos: Option, - pub mana: i16, - pub hp: i16, - pub name: String, - pub inventory: Option>, - pub color: Color, - pub test: AnyT, - pub test4: Option>, - pub testarrayofstring: Option>, - pub testarrayoftables: Option>, - pub enemy: Option>, - pub testnestedflatbuffer: Option>, - pub testempty: Option>, - pub testbool: bool, - pub testhashs32_fnv1: i32, - pub testhashu32_fnv1: u32, - pub testhashs64_fnv1: i64, - pub testhashu64_fnv1: u64, - pub testhashs32_fnv1a: i32, - pub testhashu32_fnv1a: u32, - pub testhashs64_fnv1a: i64, - pub testhashu64_fnv1a: u64, - pub testarrayofbools: Option>, - pub testf: f32, - pub testf2: f32, - pub testf3: f32, - pub testarrayofstring2: Option>, - pub testarrayofsortedstruct: Option>, - pub flex: Option>, - pub test5: Option>, - pub vector_of_longs: Option>, - pub vector_of_doubles: Option>, - pub parent_namespace_test: Option>, - pub vector_of_referrables: Option>, - pub single_weak_reference: u64, - pub vector_of_weak_references: Option>, - pub vector_of_strong_referrables: Option>, - pub co_owning_reference: u64, - pub vector_of_co_owning_references: Option>, - pub non_owning_reference: u64, - pub vector_of_non_owning_references: Option>, - pub any_unique: AnyUniqueAliasesT, - pub any_ambiguous: AnyAmbiguousAliasesT, - pub vector_of_enums: Option>, - pub signed_enum: Race, - pub testrequirednestedflatbuffer: Option>, - pub scalar_key_sorted_tables: Option>, -} -impl Default for MonsterT { - fn default() -> Self { - Self { - pos: None, - mana: 150, - hp: 100, - name: "".to_string(), - inventory: None, - color: Color::Blue, - test: AnyT::NONE, - test4: None, - testarrayofstring: None, - testarrayoftables: None, - enemy: None, - testnestedflatbuffer: None, - testempty: None, - testbool: false, - testhashs32_fnv1: 0, - testhashu32_fnv1: 0, - testhashs64_fnv1: 0, - testhashu64_fnv1: 0, - testhashs32_fnv1a: 0, - testhashu32_fnv1a: 0, - testhashs64_fnv1a: 0, - testhashu64_fnv1a: 0, - testarrayofbools: None, - testf: 3.14159, - testf2: 3.0, - testf3: 0.0, - testarrayofstring2: None, - testarrayofsortedstruct: None, - flex: None, - test5: None, - vector_of_longs: None, - vector_of_doubles: None, - parent_namespace_test: None, - vector_of_referrables: None, - single_weak_reference: 0, - vector_of_weak_references: None, - vector_of_strong_referrables: None, - co_owning_reference: 0, - vector_of_co_owning_references: None, - non_owning_reference: 0, - vector_of_non_owning_references: None, - any_unique: AnyUniqueAliasesT::NONE, - any_ambiguous: AnyAmbiguousAliasesT::NONE, - vector_of_enums: None, - signed_enum: Race::None, - testrequirednestedflatbuffer: None, - scalar_key_sorted_tables: None, - } - } -} -impl MonsterT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let pos_tmp = self.pos.as_ref().map(|x| x.pack()); - let pos = pos_tmp.as_ref(); - let mana = self.mana; - let hp = self.hp; - let name = Some({ - let x = &self.name; - _fbb.create_string(x) - }); - let inventory = self.inventory.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let color = self.color; - let test_type = self.test.any_type(); - let test = self.test.pack(_fbb); - let test4 = self.test4.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) - }); - let testarrayofstring = self.testarrayofstring.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|s| s.as_ref()).collect();_fbb.create_vector_of_strings(&w) - }); - let testarrayoftables = self.testarrayoftables.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) - }); - let enemy = self.enemy.as_ref().map(|x|{ - x.pack(_fbb) - }); - let testnestedflatbuffer = self.testnestedflatbuffer.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let testempty = self.testempty.as_ref().map(|x|{ - x.pack(_fbb) - }); - let testbool = self.testbool; - let testhashs32_fnv1 = self.testhashs32_fnv1; - let testhashu32_fnv1 = self.testhashu32_fnv1; - let testhashs64_fnv1 = self.testhashs64_fnv1; - let testhashu64_fnv1 = self.testhashu64_fnv1; - let testhashs32_fnv1a = self.testhashs32_fnv1a; - let testhashu32_fnv1a = self.testhashu32_fnv1a; - let testhashs64_fnv1a = self.testhashs64_fnv1a; - let testhashu64_fnv1a = self.testhashu64_fnv1a; - let testarrayofbools = self.testarrayofbools.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let testf = self.testf; - let testf2 = self.testf2; - let testf3 = self.testf3; - let testarrayofstring2 = self.testarrayofstring2.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|s| s.as_ref()).collect();_fbb.create_vector_of_strings(&w) - }); - let testarrayofsortedstruct = self.testarrayofsortedstruct.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) - }); - let flex = self.flex.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let test5 = self.test5.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w) - }); - let vector_of_longs = self.vector_of_longs.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let vector_of_doubles = self.vector_of_doubles.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let parent_namespace_test = self.parent_namespace_test.as_ref().map(|x|{ - x.pack(_fbb) - }); - let vector_of_referrables = self.vector_of_referrables.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) - }); - let single_weak_reference = self.single_weak_reference; - let vector_of_weak_references = self.vector_of_weak_references.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let vector_of_strong_referrables = self.vector_of_strong_referrables.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) - }); - let co_owning_reference = self.co_owning_reference; - let vector_of_co_owning_references = self.vector_of_co_owning_references.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let non_owning_reference = self.non_owning_reference; - let vector_of_non_owning_references = self.vector_of_non_owning_references.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let any_unique_type = self.any_unique.any_unique_aliases_type(); - let any_unique = self.any_unique.pack(_fbb); - let any_ambiguous_type = self.any_ambiguous.any_ambiguous_aliases_type(); - let any_ambiguous = self.any_ambiguous.pack(_fbb); - let vector_of_enums = self.vector_of_enums.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let signed_enum = self.signed_enum; - let testrequirednestedflatbuffer = self.testrequirednestedflatbuffer.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let scalar_key_sorted_tables = self.scalar_key_sorted_tables.as_ref().map(|x|{ - let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w) - }); - Monster::create(_fbb, &MonsterArgs{ - pos, - mana, - hp, - name, - inventory, - color, - test_type, - test, - test4, - testarrayofstring, - testarrayoftables, - enemy, - testnestedflatbuffer, - testempty, - testbool, - testhashs32_fnv1, - testhashu32_fnv1, - testhashs64_fnv1, - testhashu64_fnv1, - testhashs32_fnv1a, - testhashu32_fnv1a, - testhashs64_fnv1a, - testhashu64_fnv1a, - testarrayofbools, - testf, - testf2, - testf3, - testarrayofstring2, - testarrayofsortedstruct, - flex, - test5, - vector_of_longs, - vector_of_doubles, - parent_namespace_test, - vector_of_referrables, - single_weak_reference, - vector_of_weak_references, - vector_of_strong_referrables, - co_owning_reference, - vector_of_co_owning_references, - non_owning_reference, - vector_of_non_owning_references, - any_unique_type, - any_unique, - any_ambiguous_type, - any_ambiguous, - vector_of_enums, - signed_enum, - testrequirednestedflatbuffer, - scalar_key_sorted_tables, - }) - } -} -pub enum TypeAliasesOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TypeAliases<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TypeAliases<'a> { - type Inner = TypeAliases<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TypeAliases<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "MyGame.Example.TypeAliases" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TypeAliases { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TypeAliasesArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = TypeAliasesBuilder::new(_fbb); - builder.add_f64_(args.f64_); - builder.add_u64_(args.u64_); - builder.add_i64_(args.i64_); - if let Some(x) = args.vf64 { builder.add_vf64(x); } - if let Some(x) = args.v8 { builder.add_v8(x); } - builder.add_f32_(args.f32_); - builder.add_u32_(args.u32_); - builder.add_i32_(args.i32_); - builder.add_u16_(args.u16_); - builder.add_i16_(args.i16_); - builder.add_u8_(args.u8_); - builder.add_i8_(args.i8_); - builder.finish() - } - - pub fn unpack(&self) -> TypeAliasesT { - let i8_ = self.i8_(); - let u8_ = self.u8_(); - let i16_ = self.i16_(); - let u16_ = self.u16_(); - let i32_ = self.i32_(); - let u32_ = self.u32_(); - let i64_ = self.i64_(); - let u64_ = self.u64_(); - let f32_ = self.f32_(); - let f64_ = self.f64_(); - let v8 = self.v8().map(|x| { - x.to_vec() - }); - let vf64 = self.vf64().map(|x| { - x.into_iter().collect() - }); - TypeAliasesT { - i8_, - u8_, - i16_, - u16_, - i32_, - u32_, - i64_, - u64_, - f32_, - f64_, - v8, - vf64, - } - } - pub const VT_I8_: flatbuffers::VOffsetT = 4; - pub const VT_U8_: flatbuffers::VOffsetT = 6; - pub const VT_I16_: flatbuffers::VOffsetT = 8; - pub const VT_U16_: flatbuffers::VOffsetT = 10; - pub const VT_I32_: flatbuffers::VOffsetT = 12; - pub const VT_U32_: flatbuffers::VOffsetT = 14; - pub const VT_I64_: flatbuffers::VOffsetT = 16; - pub const VT_U64_: flatbuffers::VOffsetT = 18; - pub const VT_F32_: flatbuffers::VOffsetT = 20; - pub const VT_F64_: flatbuffers::VOffsetT = 22; - pub const VT_V8: flatbuffers::VOffsetT = 24; - pub const VT_VF64: flatbuffers::VOffsetT = 26; - - #[inline] - pub fn i8_(&self) -> i8 { - self._tab.get::(TypeAliases::VT_I8_, Some(0)).unwrap() - } - #[inline] - pub fn u8_(&self) -> u8 { - self._tab.get::(TypeAliases::VT_U8_, Some(0)).unwrap() - } - #[inline] - pub fn i16_(&self) -> i16 { - self._tab.get::(TypeAliases::VT_I16_, Some(0)).unwrap() - } - #[inline] - pub fn u16_(&self) -> u16 { - self._tab.get::(TypeAliases::VT_U16_, Some(0)).unwrap() - } - #[inline] - pub fn i32_(&self) -> i32 { - self._tab.get::(TypeAliases::VT_I32_, Some(0)).unwrap() - } - #[inline] - pub fn u32_(&self) -> u32 { - self._tab.get::(TypeAliases::VT_U32_, Some(0)).unwrap() - } - #[inline] - pub fn i64_(&self) -> i64 { - self._tab.get::(TypeAliases::VT_I64_, Some(0)).unwrap() - } - #[inline] - pub fn u64_(&self) -> u64 { - self._tab.get::(TypeAliases::VT_U64_, Some(0)).unwrap() - } - #[inline] - pub fn f32_(&self) -> f32 { - self._tab.get::(TypeAliases::VT_F32_, Some(0.0)).unwrap() - } - #[inline] - pub fn f64_(&self) -> f64 { - self._tab.get::(TypeAliases::VT_F64_, Some(0.0)).unwrap() - } - #[inline] - pub fn v8(&self) -> Option<&'a [i8]> { - self._tab.get::>>(TypeAliases::VT_V8, None).map(|v| v.safe_slice()) - } - #[inline] - pub fn vf64(&self) -> Option> { - self._tab.get::>>(TypeAliases::VT_VF64, None) - } -} - -impl flatbuffers::Verifiable for TypeAliases<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"i8_", Self::VT_I8_, false)? - .visit_field::(&"u8_", Self::VT_U8_, false)? - .visit_field::(&"i16_", Self::VT_I16_, false)? - .visit_field::(&"u16_", Self::VT_U16_, false)? - .visit_field::(&"i32_", Self::VT_I32_, false)? - .visit_field::(&"u32_", Self::VT_U32_, false)? - .visit_field::(&"i64_", Self::VT_I64_, false)? - .visit_field::(&"u64_", Self::VT_U64_, false)? - .visit_field::(&"f32_", Self::VT_F32_, false)? - .visit_field::(&"f64_", Self::VT_F64_, false)? - .visit_field::>>(&"v8", Self::VT_V8, false)? - .visit_field::>>(&"vf64", Self::VT_VF64, false)? - .finish(); - Ok(()) - } -} -pub struct TypeAliasesArgs<'a> { - pub i8_: i8, - pub u8_: u8, - pub i16_: i16, - pub u16_: u16, - pub i32_: i32, - pub u32_: u32, - pub i64_: i64, - pub u64_: u64, - pub f32_: f32, - pub f64_: f64, - pub v8: Option>>, - pub vf64: Option>>, -} -impl<'a> Default for TypeAliasesArgs<'a> { - #[inline] - fn default() -> Self { - TypeAliasesArgs { - i8_: 0, - u8_: 0, - i16_: 0, - u16_: 0, - i32_: 0, - u32_: 0, - i64_: 0, - u64_: 0, - f32_: 0.0, - f64_: 0.0, - v8: None, - vf64: None, - } - } -} -pub struct TypeAliasesBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TypeAliasesBuilder<'a, 'b> { - #[inline] - pub fn add_i8_(&mut self, i8_: i8) { - self.fbb_.push_slot::(TypeAliases::VT_I8_, i8_, 0); - } - #[inline] - pub fn add_u8_(&mut self, u8_: u8) { - self.fbb_.push_slot::(TypeAliases::VT_U8_, u8_, 0); - } - #[inline] - pub fn add_i16_(&mut self, i16_: i16) { - self.fbb_.push_slot::(TypeAliases::VT_I16_, i16_, 0); - } - #[inline] - pub fn add_u16_(&mut self, u16_: u16) { - self.fbb_.push_slot::(TypeAliases::VT_U16_, u16_, 0); - } - #[inline] - pub fn add_i32_(&mut self, i32_: i32) { - self.fbb_.push_slot::(TypeAliases::VT_I32_, i32_, 0); - } - #[inline] - pub fn add_u32_(&mut self, u32_: u32) { - self.fbb_.push_slot::(TypeAliases::VT_U32_, u32_, 0); - } - #[inline] - pub fn add_i64_(&mut self, i64_: i64) { - self.fbb_.push_slot::(TypeAliases::VT_I64_, i64_, 0); - } - #[inline] - pub fn add_u64_(&mut self, u64_: u64) { - self.fbb_.push_slot::(TypeAliases::VT_U64_, u64_, 0); - } - #[inline] - pub fn add_f32_(&mut self, f32_: f32) { - self.fbb_.push_slot::(TypeAliases::VT_F32_, f32_, 0.0); - } - #[inline] - pub fn add_f64_(&mut self, f64_: f64) { - self.fbb_.push_slot::(TypeAliases::VT_F64_, f64_, 0.0); - } - #[inline] - pub fn add_v8(&mut self, v8: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TypeAliases::VT_V8, v8); - } - #[inline] - pub fn add_vf64(&mut self, vf64: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TypeAliases::VT_VF64, vf64); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TypeAliasesBuilder<'a, 'b> { - let start = _fbb.start_table(); - TypeAliasesBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TypeAliases<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TypeAliases"); - ds.field("i8_", &self.i8_()); - ds.field("u8_", &self.u8_()); - ds.field("i16_", &self.i16_()); - ds.field("u16_", &self.u16_()); - ds.field("i32_", &self.i32_()); - ds.field("u32_", &self.u32_()); - ds.field("i64_", &self.i64_()); - ds.field("u64_", &self.u64_()); - ds.field("f32_", &self.f32_()); - ds.field("f64_", &self.f64_()); - ds.field("v8", &self.v8()); - ds.field("vf64", &self.vf64()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TypeAliasesT { - pub i8_: i8, - pub u8_: u8, - pub i16_: i16, - pub u16_: u16, - pub i32_: i32, - pub u32_: u32, - pub i64_: i64, - pub u64_: u64, - pub f32_: f32, - pub f64_: f64, - pub v8: Option>, - pub vf64: Option>, -} -impl Default for TypeAliasesT { - fn default() -> Self { - Self { - i8_: 0, - u8_: 0, - i16_: 0, - u16_: 0, - i32_: 0, - u32_: 0, - i64_: 0, - u64_: 0, - f32_: 0.0, - f64_: 0.0, - v8: None, - vf64: None, - } - } -} -impl TypeAliasesT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let i8_ = self.i8_; - let u8_ = self.u8_; - let i16_ = self.i16_; - let u16_ = self.u16_; - let i32_ = self.i32_; - let u32_ = self.u32_; - let i64_ = self.i64_; - let u64_ = self.u64_; - let f32_ = self.f32_; - let f64_ = self.f64_; - let v8 = self.v8.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - let vf64 = self.vf64.as_ref().map(|x|{ - _fbb.create_vector(x) - }); - TypeAliases::create(_fbb, &TypeAliasesArgs{ - i8_, - u8_, - i16_, - u16_, - i32_, - u32_, - i64_, - u64_, - f32_, - f64_, - v8, - vf64, - }) - } -} -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { - unsafe { flatbuffers::root_unchecked::>(buf) } -} - -#[inline] -#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")] -pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> { - unsafe { flatbuffers::size_prefixed_root_unchecked::>(buf) } -} - -#[inline] -/// Verifies that a buffer of bytes contains a `Monster` -/// and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn root_as_monster(buf: &[u8]) -> Result { - flatbuffers::root::(buf) -} -#[inline] -/// Verifies that a buffer of bytes contains a size prefixed -/// `Monster` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `size_prefixed_root_as_monster_unchecked`. -pub fn size_prefixed_root_as_monster(buf: &[u8]) -> Result { - flatbuffers::size_prefixed_root::(buf) -} -#[inline] -/// Verifies, with the given options, that a buffer of bytes -/// contains a `Monster` and returns it. -/// Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn root_as_monster_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::root_with_opts::>(opts, buf) -} -#[inline] -/// Verifies, with the given verifier options, that a buffer of -/// bytes contains a size prefixed `Monster` and returns -/// it. Note that verification is still experimental and may not -/// catch every error, or be maximally performant. For the -/// previous, unchecked, behavior use -/// `root_as_monster_unchecked`. -pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>( - opts: &'o flatbuffers::VerifierOptions, - buf: &'b [u8], -) -> Result, flatbuffers::InvalidFlatbuffer> { - flatbuffers::size_prefixed_root_with_opts::>(opts, buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a Monster and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid `Monster`. -pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster { - flatbuffers::root_unchecked::(buf) -} -#[inline] -/// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it. -/// # Safety -/// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`. -pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster { - flatbuffers::size_prefixed_root_unchecked::(buf) -} -pub const MONSTER_IDENTIFIER: &str = "MONS"; - -#[inline] -pub fn monster_buffer_has_identifier(buf: &[u8]) -> bool { - flatbuffers::buffer_has_identifier(buf, MONSTER_IDENTIFIER, false) -} - -#[inline] -pub fn monster_size_prefixed_buffer_has_identifier(buf: &[u8]) -> bool { - flatbuffers::buffer_has_identifier(buf, MONSTER_IDENTIFIER, true) -} - -pub const MONSTER_EXTENSION: &str = "mon"; - -#[inline] -pub fn finish_monster_buffer<'a, 'b>( - fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, - root: flatbuffers::WIPOffset>) { - fbb.finish(root, Some(MONSTER_IDENTIFIER)); -} - -#[inline] -pub fn finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { - fbb.finish_size_prefixed(root, Some(MONSTER_IDENTIFIER)); -} -} // pub mod Example -} // pub mod MyGame - diff --git a/tests/more_defaults/abc_generated.rs b/tests/more_defaults/abc_generated.rs new file mode 100644 index 000000000..8622d9ba1 --- /dev/null +++ b/tests/more_defaults/abc_generated.rs @@ -0,0 +1,97 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ABC: i32 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ABC: i32 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ABC: [ABC; 3] = [ + ABC::A, + ABC::B, + ABC::C, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct ABC(pub i32); +#[allow(non_upper_case_globals)] +impl ABC { + pub const A: Self = Self(0); + pub const B: Self = Self(1); + pub const C: Self = Self(2); + + pub const ENUM_MIN: i32 = 0; + pub const ENUM_MAX: i32 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::A, + Self::B, + Self::C, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::A => Some("A"), + Self::B => Some("B"), + Self::C => Some("C"), + _ => None, + } + } +} +impl std::fmt::Debug for ABC { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for ABC { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for ABC { + type Output = ABC; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for ABC { + #[inline] + fn to_little_endian(self) -> Self { + let b = i32::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i32::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for ABC { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i32::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for ABC {} diff --git a/tests/more_defaults/mod.rs b/tests/more_defaults/mod.rs new file mode 100644 index 000000000..639f8acb1 --- /dev/null +++ b/tests/more_defaults/mod.rs @@ -0,0 +1,5 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +mod abc_generated; +pub use self::abc_generated::*; +mod more_defaults_generated; +pub use self::more_defaults_generated::*; diff --git a/tests/more_defaults_generated.rs b/tests/more_defaults/more_defaults_generated.rs similarity index 75% rename from tests/more_defaults_generated.rs rename to tests/more_defaults/more_defaults_generated.rs index be818bc27..cc163ff23 100644 --- a/tests/more_defaults_generated.rs +++ b/tests/more_defaults/more_defaults_generated.rs @@ -1,104 +1,9 @@ // automatically generated by the FlatBuffers compiler, do not modify - - - +extern crate flatbuffers; use std::mem; use std::cmp::Ordering; - -extern crate flatbuffers; use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ABC: i32 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ABC: i32 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ABC: [ABC; 3] = [ - ABC::A, - ABC::B, - ABC::C, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct ABC(pub i32); -#[allow(non_upper_case_globals)] -impl ABC { - pub const A: Self = Self(0); - pub const B: Self = Self(1); - pub const C: Self = Self(2); - - pub const ENUM_MIN: i32 = 0; - pub const ENUM_MAX: i32 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::A, - Self::B, - Self::C, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::A => Some("A"), - Self::B => Some("B"), - Self::C => Some("C"), - _ => None, - } - } -} -impl std::fmt::Debug for ABC { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for ABC { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for ABC { - type Output = ABC; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for ABC { - #[inline] - fn to_little_endian(self) -> Self { - let b = i32::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i32::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for ABC { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i32::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for ABC {} +use super::*; pub enum MoreDefaultsOffset {} #[derive(Copy, Clone, PartialEq)] @@ -115,6 +20,10 @@ impl<'a> flatbuffers::Follow<'a> for MoreDefaults<'a> { } impl<'a> MoreDefaults<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "MoreDefaults" + } + #[inline] pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { MoreDefaults { _tab: table } @@ -207,12 +116,12 @@ impl flatbuffers::Verifiable for MoreDefaults<'_> { ) -> Result<(), flatbuffers::InvalidFlatbuffer> { use self::flatbuffers::Verifiable; v.visit_table(pos)? - .visit_field::>>(&"ints", Self::VT_INTS, false)? - .visit_field::>>(&"floats", Self::VT_FLOATS, false)? - .visit_field::>(&"empty_string", Self::VT_EMPTY_STRING, false)? - .visit_field::>(&"some_string", Self::VT_SOME_STRING, false)? - .visit_field::>>(&"abcs", Self::VT_ABCS, false)? - .visit_field::>>(&"bools", Self::VT_BOOLS, false)? + .visit_field::>>("ints", Self::VT_INTS, false)? + .visit_field::>>("floats", Self::VT_FLOATS, false)? + .visit_field::>("empty_string", Self::VT_EMPTY_STRING, false)? + .visit_field::>("some_string", Self::VT_SOME_STRING, false)? + .visit_field::>>("abcs", Self::VT_ABCS, false)? + .visit_field::>>("bools", Self::VT_BOOLS, false)? .finish(); Ok(()) } diff --git a/tests/namespace_test/mod.rs b/tests/namespace_test/mod.rs new file mode 100644 index 000000000..722f85368 --- /dev/null +++ b/tests/namespace_test/mod.rs @@ -0,0 +1,24 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod namespace_a { + use super::*; + pub mod namespace_b { + use super::*; + mod union_in_nested_ns_generated; + pub use self::union_in_nested_ns_generated::*; + mod enum_in_nested_ns_generated; + pub use self::enum_in_nested_ns_generated::*; + mod table_in_nested_ns_generated; + pub use self::table_in_nested_ns_generated::*; + mod struct_in_nested_ns_generated; + pub use self::struct_in_nested_ns_generated::*; + } // namespace_b + mod table_in_first_ns_generated; + pub use self::table_in_first_ns_generated::*; + mod second_table_in_a_generated; + pub use self::second_table_in_a_generated::*; +} // namespace_a +pub mod namespace_c { + use super::*; + mod table_in_c_generated; + pub use self::table_in_c_generated::*; +} // namespace_c diff --git a/tests/namespace_test/namespace_a/namespace_b/enum_in_nested_ns_generated.rs b/tests/namespace_test/namespace_a/namespace_b/enum_in_nested_ns_generated.rs new file mode 100644 index 000000000..b6366385a --- /dev/null +++ b/tests/namespace_test/namespace_a/namespace_b/enum_in_nested_ns_generated.rs @@ -0,0 +1,97 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [ + EnumInNestedNS::A, + EnumInNestedNS::B, + EnumInNestedNS::C, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct EnumInNestedNS(pub i8); +#[allow(non_upper_case_globals)] +impl EnumInNestedNS { + pub const A: Self = Self(0); + pub const B: Self = Self(1); + pub const C: Self = Self(2); + + pub const ENUM_MIN: i8 = 0; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::A, + Self::B, + Self::C, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::A => Some("A"), + Self::B => Some("B"), + Self::C => Some("C"), + _ => None, + } + } +} +impl std::fmt::Debug for EnumInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for EnumInNestedNS { + type Output = EnumInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for EnumInNestedNS { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for EnumInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {} diff --git a/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs b/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs new file mode 100644 index 000000000..2b2495dc7 --- /dev/null +++ b/tests/namespace_test/namespace_a/namespace_b/struct_in_nested_ns_generated.rs @@ -0,0 +1,155 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +// struct StructInNestedNS, aligned to 4 +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq)] +pub struct StructInNestedNS(pub [u8; 8]); +impl Default for StructInNestedNS { + fn default() -> Self { + Self([0; 8]) + } +} +impl std::fmt::Debug for StructInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("StructInNestedNS") + .field("a", &self.a()) + .field("b", &self.b()) + .finish() + } +} + +impl flatbuffers::SimpleToVerifyInSlice for StructInNestedNS {} +impl flatbuffers::SafeSliceAccess for StructInNestedNS {} +impl<'a> flatbuffers::Follow<'a> for StructInNestedNS { + type Inner = &'a StructInNestedNS; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + <&'a StructInNestedNS>::follow(buf, loc) + } +} +impl<'a> flatbuffers::Follow<'a> for &'a StructInNestedNS { + type Inner = &'a StructInNestedNS; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + flatbuffers::follow_cast_ref::(buf, loc) + } +} +impl<'b> flatbuffers::Push for StructInNestedNS { + type Output = StructInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(self as *const StructInNestedNS as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} +impl<'b> flatbuffers::Push for &'b StructInNestedNS { + type Output = StructInNestedNS; + + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + let src = unsafe { + ::std::slice::from_raw_parts(*self as *const StructInNestedNS as *const u8, Self::size()) + }; + dst.copy_from_slice(src); + } +} + +impl<'a> flatbuffers::Verifiable for StructInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.in_buffer::(pos) + } +} +impl<'a> StructInNestedNS { + #[allow(clippy::too_many_arguments)] + pub fn new( + a: i32, + b: i32, + ) -> Self { + let mut s = Self([0; 8]); + s.set_a(a); + s.set_b(b); + s + } + + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.NamespaceB.StructInNestedNS" + } + + pub fn a(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[0..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_a(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[0..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn b(&self) -> i32 { + let mut mem = core::mem::MaybeUninit::::uninit(); + unsafe { + core::ptr::copy_nonoverlapping( + self.0[4..].as_ptr(), + mem.as_mut_ptr() as *mut u8, + core::mem::size_of::(), + ); + mem.assume_init() + }.from_little_endian() + } + + pub fn set_b(&mut self, x: i32) { + let x_le = x.to_little_endian(); + unsafe { + core::ptr::copy_nonoverlapping( + &x_le as *const i32 as *const u8, + self.0[4..].as_mut_ptr(), + core::mem::size_of::(), + ); + } + } + + pub fn unpack(&self) -> StructInNestedNST { + StructInNestedNST { + a: self.a(), + b: self.b(), + } + } +} + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct StructInNestedNST { + pub a: i32, + pub b: i32, +} +impl StructInNestedNST { + pub fn pack(&self) -> StructInNestedNS { + StructInNestedNS::new( + self.a, + self.b, + ) + } +} + diff --git a/tests/namespace_test/namespace_a/namespace_b/table_in_nested_ns_generated.rs b/tests/namespace_test/namespace_a/namespace_b/table_in_nested_ns_generated.rs new file mode 100644 index 000000000..1426d954c --- /dev/null +++ b/tests/namespace_test/namespace_a/namespace_b/table_in_nested_ns_generated.rs @@ -0,0 +1,130 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableInNestedNSOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableInNestedNS<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableInNestedNS<'a> { + type Inner = TableInNestedNS<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableInNestedNS<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.NamespaceB.TableInNestedNS" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableInNestedNS { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableInNestedNSArgs) -> flatbuffers::WIPOffset> { + let mut builder = TableInNestedNSBuilder::new(_fbb); + builder.add_foo(args.foo); + builder.finish() + } + + pub fn unpack(&self) -> TableInNestedNST { + let foo = self.foo(); + TableInNestedNST { + foo, + } + } + pub const VT_FOO: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn foo(&self) -> i32 { + self._tab.get::(TableInNestedNS::VT_FOO, Some(0)).unwrap() + } +} + +impl flatbuffers::Verifiable for TableInNestedNS<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::("foo", Self::VT_FOO, false)? + .finish(); + Ok(()) + } +} +pub struct TableInNestedNSArgs { + pub foo: i32, +} +impl<'a> Default for TableInNestedNSArgs { + #[inline] + fn default() -> Self { + TableInNestedNSArgs { + foo: 0, + } + } +} +pub struct TableInNestedNSBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableInNestedNSBuilder<'a, 'b> { + #[inline] + pub fn add_foo(&mut self, foo: i32) { + self.fbb_.push_slot::(TableInNestedNS::VT_FOO, foo, 0); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInNestedNSBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableInNestedNSBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableInNestedNS<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableInNestedNS"); + ds.field("foo", &self.foo()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableInNestedNST { + pub foo: i32, +} +impl Default for TableInNestedNST { + fn default() -> Self { + Self { + foo: 0, + } + } +} +impl TableInNestedNST { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let foo = self.foo; + TableInNestedNS::create(_fbb, &TableInNestedNSArgs{ + foo, + }) + } +} diff --git a/tests/namespace_test/namespace_a/namespace_b/union_in_nested_ns_generated.rs b/tests/namespace_test/namespace_a/namespace_b/union_in_nested_ns_generated.rs new file mode 100644 index 000000000..8d650a513 --- /dev/null +++ b/tests/namespace_test/namespace_a/namespace_b/union_in_nested_ns_generated.rs @@ -0,0 +1,142 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_UNION_IN_NESTED_NS: u8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_UNION_IN_NESTED_NS: u8 = 1; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_UNION_IN_NESTED_NS: [UnionInNestedNS; 2] = [ + UnionInNestedNS::NONE, + UnionInNestedNS::TableInNestedNS, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct UnionInNestedNS(pub u8); +#[allow(non_upper_case_globals)] +impl UnionInNestedNS { + pub const NONE: Self = Self(0); + pub const TableInNestedNS: Self = Self(1); + + pub const ENUM_MIN: u8 = 0; + pub const ENUM_MAX: u8 = 1; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::NONE, + Self::TableInNestedNS, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::NONE => Some("NONE"), + Self::TableInNestedNS => Some("TableInNestedNS"), + _ => None, + } + } +} +impl std::fmt::Debug for UnionInNestedNS { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for UnionInNestedNS { + type Output = UnionInNestedNS; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for UnionInNestedNS { + #[inline] + fn to_little_endian(self) -> Self { + let b = u8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = u8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for UnionInNestedNS { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + u8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for UnionInNestedNS {} +pub struct UnionInNestedNSUnionTableOffset {} + +#[allow(clippy::upper_case_acronyms)] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub enum UnionInNestedNST { + NONE, + TableInNestedNS(Box), +} +impl Default for UnionInNestedNST { + fn default() -> Self { + Self::NONE + } +} +impl UnionInNestedNST { + pub fn union_in_nested_ns_type(&self) -> UnionInNestedNS { + match self { + Self::NONE => UnionInNestedNS::NONE, + Self::TableInNestedNS(_) => UnionInNestedNS::TableInNestedNS, + } + } + pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { + match self { + Self::NONE => None, + Self::TableInNestedNS(v) => Some(v.pack(fbb).as_union_value()), + } + } + /// If the union variant matches, return the owned TableInNestedNST, setting the union to NONE. + pub fn take_table_in_nested_ns(&mut self) -> Option> { + if let Self::TableInNestedNS(_) = self { + let v = std::mem::replace(self, Self::NONE); + if let Self::TableInNestedNS(w) = v { + Some(w) + } else { + unreachable!() + } + } else { + None + } + } + /// If the union variant matches, return a reference to the TableInNestedNST. + pub fn as_table_in_nested_ns(&self) -> Option<&TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_ref()) } else { None } + } + /// If the union variant matches, return a mutable reference to the TableInNestedNST. + pub fn as_table_in_nested_ns_mut(&mut self) -> Option<&mut TableInNestedNST> { + if let Self::TableInNestedNS(v) = self { Some(v.as_mut()) } else { None } + } +} diff --git a/tests/namespace_test/namespace_a/second_table_in_a_generated.rs b/tests/namespace_test/namespace_a/second_table_in_a_generated.rs new file mode 100644 index 000000000..027828663 --- /dev/null +++ b/tests/namespace_test/namespace_a/second_table_in_a_generated.rs @@ -0,0 +1,134 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum SecondTableInAOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct SecondTableInA<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for SecondTableInA<'a> { + type Inner = SecondTableInA<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> SecondTableInA<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.SecondTableInA" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + SecondTableInA { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args SecondTableInAArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = SecondTableInABuilder::new(_fbb); + if let Some(x) = args.refer_to_c { builder.add_refer_to_c(x); } + builder.finish() + } + + pub fn unpack(&self) -> SecondTableInAT { + let refer_to_c = self.refer_to_c().map(|x| { + Box::new(x.unpack()) + }); + SecondTableInAT { + refer_to_c, + } + } + pub const VT_REFER_TO_C: flatbuffers::VOffsetT = 4; + + #[inline] + pub fn refer_to_c(&self) -> Option> { + self._tab.get::>(SecondTableInA::VT_REFER_TO_C, None) + } +} + +impl flatbuffers::Verifiable for SecondTableInA<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("refer_to_c", Self::VT_REFER_TO_C, false)? + .finish(); + Ok(()) + } +} +pub struct SecondTableInAArgs<'a> { + pub refer_to_c: Option>>, +} +impl<'a> Default for SecondTableInAArgs<'a> { + #[inline] + fn default() -> Self { + SecondTableInAArgs { + refer_to_c: None, + } + } +} +pub struct SecondTableInABuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> SecondTableInABuilder<'a, 'b> { + #[inline] + pub fn add_refer_to_c(&mut self, refer_to_c: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(SecondTableInA::VT_REFER_TO_C, refer_to_c); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> SecondTableInABuilder<'a, 'b> { + let start = _fbb.start_table(); + SecondTableInABuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for SecondTableInA<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("SecondTableInA"); + ds.field("refer_to_c", &self.refer_to_c()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct SecondTableInAT { + pub refer_to_c: Option>, +} +impl Default for SecondTableInAT { + fn default() -> Self { + Self { + refer_to_c: None, + } + } +} +impl SecondTableInAT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let refer_to_c = self.refer_to_c.as_ref().map(|x|{ + x.pack(_fbb) + }); + SecondTableInA::create(_fbb, &SecondTableInAArgs{ + refer_to_c, + }) + } +} diff --git a/tests/namespace_test/namespace_a/table_in_first_ns_generated.rs b/tests/namespace_test/namespace_a/table_in_first_ns_generated.rs new file mode 100644 index 000000000..a8e4cdc15 --- /dev/null +++ b/tests/namespace_test/namespace_a/table_in_first_ns_generated.rs @@ -0,0 +1,247 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableInFirstNSOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableInFirstNS<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableInFirstNS<'a> { + type Inner = TableInFirstNS<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableInFirstNS<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceA.TableInFirstNS" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableInFirstNS { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableInFirstNSArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableInFirstNSBuilder::new(_fbb); + if let Some(x) = args.foo_struct { builder.add_foo_struct(x); } + if let Some(x) = args.foo_union { builder.add_foo_union(x); } + if let Some(x) = args.foo_table { builder.add_foo_table(x); } + builder.add_foo_union_type(args.foo_union_type); + builder.add_foo_enum(args.foo_enum); + builder.finish() + } + + pub fn unpack(&self) -> TableInFirstNST { + let foo_table = self.foo_table().map(|x| { + Box::new(x.unpack()) + }); + let foo_enum = self.foo_enum(); + let foo_union = match self.foo_union_type() { + namespace_b::UnionInNestedNS::NONE => namespace_b::UnionInNestedNST::NONE, + namespace_b::UnionInNestedNS::TableInNestedNS => namespace_b::UnionInNestedNST::TableInNestedNS(Box::new( + self.foo_union_as_table_in_nested_ns() + .expect("Invalid union table, expected `namespace_b::UnionInNestedNS::TableInNestedNS`.") + .unpack() + )), + _ => namespace_b::UnionInNestedNST::NONE, + }; + let foo_struct = self.foo_struct().map(|x| { + x.unpack() + }); + TableInFirstNST { + foo_table, + foo_enum, + foo_union, + foo_struct, + } + } + pub const VT_FOO_TABLE: flatbuffers::VOffsetT = 4; + pub const VT_FOO_ENUM: flatbuffers::VOffsetT = 6; + pub const VT_FOO_UNION_TYPE: flatbuffers::VOffsetT = 8; + pub const VT_FOO_UNION: flatbuffers::VOffsetT = 10; + pub const VT_FOO_STRUCT: flatbuffers::VOffsetT = 12; + + #[inline] + pub fn foo_table(&self) -> Option> { + self._tab.get::>(TableInFirstNS::VT_FOO_TABLE, None) + } + #[inline] + pub fn foo_enum(&self) -> namespace_b::EnumInNestedNS { + self._tab.get::(TableInFirstNS::VT_FOO_ENUM, Some(namespace_b::EnumInNestedNS::A)).unwrap() + } + #[inline] + pub fn foo_union_type(&self) -> namespace_b::UnionInNestedNS { + self._tab.get::(TableInFirstNS::VT_FOO_UNION_TYPE, Some(namespace_b::UnionInNestedNS::NONE)).unwrap() + } + #[inline] + pub fn foo_union(&self) -> Option> { + self._tab.get::>>(TableInFirstNS::VT_FOO_UNION, None) + } + #[inline] + pub fn foo_struct(&self) -> Option<&'a namespace_b::StructInNestedNS> { + self._tab.get::(TableInFirstNS::VT_FOO_STRUCT, None) + } + #[inline] + #[allow(non_snake_case)] + pub fn foo_union_as_table_in_nested_ns(&self) -> Option> { + if self.foo_union_type() == namespace_b::UnionInNestedNS::TableInNestedNS { + self.foo_union().map(namespace_b::TableInNestedNS::init_from_table) + } else { + None + } + } + +} + +impl flatbuffers::Verifiable for TableInFirstNS<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("foo_table", Self::VT_FOO_TABLE, false)? + .visit_field::("foo_enum", Self::VT_FOO_ENUM, false)? + .visit_union::("foo_union_type", Self::VT_FOO_UNION_TYPE, "foo_union", Self::VT_FOO_UNION, false, |key, v, pos| { + match key { + namespace_b::UnionInNestedNS::TableInNestedNS => v.verify_union_variant::>("namespace_b::UnionInNestedNS::TableInNestedNS", pos), + _ => Ok(()), + } + })? + .visit_field::("foo_struct", Self::VT_FOO_STRUCT, false)? + .finish(); + Ok(()) + } +} +pub struct TableInFirstNSArgs<'a> { + pub foo_table: Option>>, + pub foo_enum: namespace_b::EnumInNestedNS, + pub foo_union_type: namespace_b::UnionInNestedNS, + pub foo_union: Option>, + pub foo_struct: Option<&'a namespace_b::StructInNestedNS>, +} +impl<'a> Default for TableInFirstNSArgs<'a> { + #[inline] + fn default() -> Self { + TableInFirstNSArgs { + foo_table: None, + foo_enum: namespace_b::EnumInNestedNS::A, + foo_union_type: namespace_b::UnionInNestedNS::NONE, + foo_union: None, + foo_struct: None, + } + } +} +pub struct TableInFirstNSBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableInFirstNSBuilder<'a, 'b> { + #[inline] + pub fn add_foo_table(&mut self, foo_table: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableInFirstNS::VT_FOO_TABLE, foo_table); + } + #[inline] + pub fn add_foo_enum(&mut self, foo_enum: namespace_b::EnumInNestedNS) { + self.fbb_.push_slot::(TableInFirstNS::VT_FOO_ENUM, foo_enum, namespace_b::EnumInNestedNS::A); + } + #[inline] + pub fn add_foo_union_type(&mut self, foo_union_type: namespace_b::UnionInNestedNS) { + self.fbb_.push_slot::(TableInFirstNS::VT_FOO_UNION_TYPE, foo_union_type, namespace_b::UnionInNestedNS::NONE); + } + #[inline] + pub fn add_foo_union(&mut self, foo_union: flatbuffers::WIPOffset) { + self.fbb_.push_slot_always::>(TableInFirstNS::VT_FOO_UNION, foo_union); + } + #[inline] + pub fn add_foo_struct(&mut self, foo_struct: &namespace_b::StructInNestedNS) { + self.fbb_.push_slot_always::<&namespace_b::StructInNestedNS>(TableInFirstNS::VT_FOO_STRUCT, foo_struct); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInFirstNSBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableInFirstNSBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableInFirstNS<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableInFirstNS"); + ds.field("foo_table", &self.foo_table()); + ds.field("foo_enum", &self.foo_enum()); + ds.field("foo_union_type", &self.foo_union_type()); + match self.foo_union_type() { + namespace_b::UnionInNestedNS::TableInNestedNS => { + if let Some(x) = self.foo_union_as_table_in_nested_ns() { + ds.field("foo_union", &x) + } else { + ds.field("foo_union", &"InvalidFlatbuffer: Union discriminant does not match value.") + } + }, + _ => { + let x: Option<()> = None; + ds.field("foo_union", &x) + }, + }; + ds.field("foo_struct", &self.foo_struct()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableInFirstNST { + pub foo_table: Option>, + pub foo_enum: namespace_b::EnumInNestedNS, + pub foo_union: namespace_b::UnionInNestedNST, + pub foo_struct: Option, +} +impl Default for TableInFirstNST { + fn default() -> Self { + Self { + foo_table: None, + foo_enum: namespace_b::EnumInNestedNS::A, + foo_union: namespace_b::UnionInNestedNST::NONE, + foo_struct: None, + } + } +} +impl TableInFirstNST { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let foo_table = self.foo_table.as_ref().map(|x|{ + x.pack(_fbb) + }); + let foo_enum = self.foo_enum; + let foo_union_type = self.foo_union.union_in_nested_ns_type(); + let foo_union = self.foo_union.pack(_fbb); + let foo_struct_tmp = self.foo_struct.as_ref().map(|x| x.pack()); + let foo_struct = foo_struct_tmp.as_ref(); + TableInFirstNS::create(_fbb, &TableInFirstNSArgs{ + foo_table, + foo_enum, + foo_union_type, + foo_union, + foo_struct, + }) + } +} diff --git a/tests/namespace_test/namespace_c/table_in_c_generated.rs b/tests/namespace_test/namespace_c/table_in_c_generated.rs new file mode 100644 index 000000000..855b28996 --- /dev/null +++ b/tests/namespace_test/namespace_c/table_in_c_generated.rs @@ -0,0 +1,158 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +pub enum TableInCOffset {} +#[derive(Copy, Clone, PartialEq)] + +pub struct TableInC<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for TableInC<'a> { + type Inner = TableInC<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { _tab: flatbuffers::Table { buf, loc } } + } +} + +impl<'a> TableInC<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "NamespaceC.TableInC" + } + + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + TableInC { _tab: table } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args TableInCArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = TableInCBuilder::new(_fbb); + if let Some(x) = args.refer_to_a2 { builder.add_refer_to_a2(x); } + if let Some(x) = args.refer_to_a1 { builder.add_refer_to_a1(x); } + builder.finish() + } + + pub fn unpack(&self) -> TableInCT { + let refer_to_a1 = self.refer_to_a1().map(|x| { + Box::new(x.unpack()) + }); + let refer_to_a2 = self.refer_to_a2().map(|x| { + Box::new(x.unpack()) + }); + TableInCT { + refer_to_a1, + refer_to_a2, + } + } + pub const VT_REFER_TO_A1: flatbuffers::VOffsetT = 4; + pub const VT_REFER_TO_A2: flatbuffers::VOffsetT = 6; + + #[inline] + pub fn refer_to_a1(&self) -> Option> { + self._tab.get::>(TableInC::VT_REFER_TO_A1, None) + } + #[inline] + pub fn refer_to_a2(&self) -> Option> { + self._tab.get::>(TableInC::VT_REFER_TO_A2, None) + } +} + +impl flatbuffers::Verifiable for TableInC<'_> { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + v.visit_table(pos)? + .visit_field::>("refer_to_a1", Self::VT_REFER_TO_A1, false)? + .visit_field::>("refer_to_a2", Self::VT_REFER_TO_A2, false)? + .finish(); + Ok(()) + } +} +pub struct TableInCArgs<'a> { + pub refer_to_a1: Option>>, + pub refer_to_a2: Option>>, +} +impl<'a> Default for TableInCArgs<'a> { + #[inline] + fn default() -> Self { + TableInCArgs { + refer_to_a1: None, + refer_to_a2: None, + } + } +} +pub struct TableInCBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> TableInCBuilder<'a, 'b> { + #[inline] + pub fn add_refer_to_a1(&mut self, refer_to_a1: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableInC::VT_REFER_TO_A1, refer_to_a1); + } + #[inline] + pub fn add_refer_to_a2(&mut self, refer_to_a2: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(TableInC::VT_REFER_TO_A2, refer_to_a2); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInCBuilder<'a, 'b> { + let start = _fbb.start_table(); + TableInCBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + +impl std::fmt::Debug for TableInC<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("TableInC"); + ds.field("refer_to_a1", &self.refer_to_a1()); + ds.field("refer_to_a2", &self.refer_to_a2()); + ds.finish() + } +} +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq)] +pub struct TableInCT { + pub refer_to_a1: Option>, + pub refer_to_a2: Option>, +} +impl Default for TableInCT { + fn default() -> Self { + Self { + refer_to_a1: None, + refer_to_a2: None, + } + } +} +impl TableInCT { + pub fn pack<'b>( + &self, + _fbb: &mut flatbuffers::FlatBufferBuilder<'b> + ) -> flatbuffers::WIPOffset> { + let refer_to_a1 = self.refer_to_a1.as_ref().map(|x|{ + x.pack(_fbb) + }); + let refer_to_a2 = self.refer_to_a2.as_ref().map(|x|{ + x.pack(_fbb) + }); + TableInC::create(_fbb, &TableInCArgs{ + refer_to_a1, + refer_to_a2, + }) + } +} diff --git a/tests/namespace_test/namespace_test1.ts b/tests/namespace_test/namespace_test1.ts new file mode 100644 index 000000000..9d8cf3342 --- /dev/null +++ b/tests/namespace_test/namespace_test1.ts @@ -0,0 +1,3 @@ +export { StructInNestedNS, StructInNestedNST } from './namespace-a/namespace-b/struct-in-nested-n-s'; +export { TableInNestedNS, TableInNestedNST } from './namespace-a/namespace-b/table-in-nested-n-s'; +export { UnionInNestedNS, unionToUnionInNestedNS, unionListToUnionInNestedNS } from './namespace-a/namespace-b/union-in-nested-n-s'; diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs deleted file mode 100644 index 37b0175a4..000000000 --- a/tests/namespace_test/namespace_test1_generated.rs +++ /dev/null @@ -1,529 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod namespace_a { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; -#[allow(unused_imports, dead_code)] -pub mod namespace_b { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_UNION_IN_NESTED_NS: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_UNION_IN_NESTED_NS: u8 = 1; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_UNION_IN_NESTED_NS: [UnionInNestedNS; 2] = [ - UnionInNestedNS::NONE, - UnionInNestedNS::TableInNestedNS, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct UnionInNestedNS(pub u8); -#[allow(non_upper_case_globals)] -impl UnionInNestedNS { - pub const NONE: Self = Self(0); - pub const TableInNestedNS: Self = Self(1); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 1; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::TableInNestedNS, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::TableInNestedNS => Some("TableInNestedNS"), - _ => None, - } - } -} -impl std::fmt::Debug for UnionInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for UnionInNestedNS { - type Output = UnionInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for UnionInNestedNS { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for UnionInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for UnionInNestedNS {} -pub struct UnionInNestedNSUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum UnionInNestedNST { - NONE, - TableInNestedNS(Box), -} -impl Default for UnionInNestedNST { - fn default() -> Self { - Self::NONE - } -} -impl UnionInNestedNST { - pub fn union_in_nested_ns_type(&self) -> UnionInNestedNS { - match self { - Self::NONE => UnionInNestedNS::NONE, - Self::TableInNestedNS(_) => UnionInNestedNS::TableInNestedNS, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::TableInNestedNS(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned TableInNestedNST, setting the union to NONE. - pub fn take_table_in_nested_ns(&mut self) -> Option> { - if let Self::TableInNestedNS(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::TableInNestedNS(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the TableInNestedNST. - pub fn as_table_in_nested_ns(&self) -> Option<&TableInNestedNST> { - if let Self::TableInNestedNS(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the TableInNestedNST. - pub fn as_table_in_nested_ns_mut(&mut self) -> Option<&mut TableInNestedNST> { - if let Self::TableInNestedNS(v) = self { Some(v.as_mut()) } else { None } - } -} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [ - EnumInNestedNS::A, - EnumInNestedNS::B, - EnumInNestedNS::C, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct EnumInNestedNS(pub i8); -#[allow(non_upper_case_globals)] -impl EnumInNestedNS { - pub const A: Self = Self(0); - pub const B: Self = Self(1); - pub const C: Self = Self(2); - - pub const ENUM_MIN: i8 = 0; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::A, - Self::B, - Self::C, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::A => Some("A"), - Self::B => Some("B"), - Self::C => Some("C"), - _ => None, - } - } -} -impl std::fmt::Debug for EnumInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for EnumInNestedNS { - type Output = EnumInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for EnumInNestedNS { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for EnumInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {} -// struct StructInNestedNS, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct StructInNestedNS(pub [u8; 8]); -impl Default for StructInNestedNS { - fn default() -> Self { - Self([0; 8]) - } -} -impl std::fmt::Debug for StructInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("StructInNestedNS") - .field("a", &self.a()) - .field("b", &self.b()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for StructInNestedNS {} -impl flatbuffers::SafeSliceAccess for StructInNestedNS {} -impl<'a> flatbuffers::Follow<'a> for StructInNestedNS { - type Inner = &'a StructInNestedNS; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a StructInNestedNS>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a StructInNestedNS { - type Inner = &'a StructInNestedNS; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for StructInNestedNS { - type Output = StructInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const StructInNestedNS as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b StructInNestedNS { - type Output = StructInNestedNS; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const StructInNestedNS as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for StructInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> StructInNestedNS { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: i32, - b: i32, - ) -> Self { - let mut s = Self([0; 8]); - s.set_a(a); - s.set_b(b); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.NamespaceB.StructInNestedNS" - } - - pub fn a(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_a(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn b(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[4..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_b(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[4..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn unpack(&self) -> StructInNestedNST { - StructInNestedNST { - a: self.a(), - b: self.b(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct StructInNestedNST { - pub a: i32, - pub b: i32, -} -impl StructInNestedNST { - pub fn pack(&self) -> StructInNestedNS { - StructInNestedNS::new( - self.a, - self.b, - ) - } -} - -pub enum TableInNestedNSOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TableInNestedNS<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TableInNestedNS<'a> { - type Inner = TableInNestedNS<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TableInNestedNS<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.NamespaceB.TableInNestedNS" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TableInNestedNS { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TableInNestedNSArgs) -> flatbuffers::WIPOffset> { - let mut builder = TableInNestedNSBuilder::new(_fbb); - builder.add_foo(args.foo); - builder.finish() - } - - pub fn unpack(&self) -> TableInNestedNST { - let foo = self.foo(); - TableInNestedNST { - foo, - } - } - pub const VT_FOO: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn foo(&self) -> i32 { - self._tab.get::(TableInNestedNS::VT_FOO, Some(0)).unwrap() - } -} - -impl flatbuffers::Verifiable for TableInNestedNS<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"foo", Self::VT_FOO, false)? - .finish(); - Ok(()) - } -} -pub struct TableInNestedNSArgs { - pub foo: i32, -} -impl<'a> Default for TableInNestedNSArgs { - #[inline] - fn default() -> Self { - TableInNestedNSArgs { - foo: 0, - } - } -} -pub struct TableInNestedNSBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TableInNestedNSBuilder<'a, 'b> { - #[inline] - pub fn add_foo(&mut self, foo: i32) { - self.fbb_.push_slot::(TableInNestedNS::VT_FOO, foo, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInNestedNSBuilder<'a, 'b> { - let start = _fbb.start_table(); - TableInNestedNSBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TableInNestedNS<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TableInNestedNS"); - ds.field("foo", &self.foo()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TableInNestedNST { - pub foo: i32, -} -impl Default for TableInNestedNST { - fn default() -> Self { - Self { - foo: 0, - } - } -} -impl TableInNestedNST { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let foo = self.foo; - TableInNestedNS::create(_fbb, &TableInNestedNSArgs{ - foo, - }) - } -} -} // pub mod NamespaceB -} // pub mod NamespaceA - diff --git a/tests/namespace_test/namespace_test2.ts b/tests/namespace_test/namespace_test2.ts new file mode 100644 index 000000000..01f1bd4cf --- /dev/null +++ b/tests/namespace_test/namespace_test2.ts @@ -0,0 +1,7 @@ +export { EnumInNestedNS } from './namespace-a/namespace-b/enum-in-nested-n-s'; +export { StructInNestedNS, StructInNestedNST } from './namespace-a/namespace-b/struct-in-nested-n-s'; +export { TableInNestedNS, TableInNestedNST } from './namespace-a/namespace-b/table-in-nested-n-s'; +export { UnionInNestedNS, unionToUnionInNestedNS, unionListToUnionInNestedNS } from './namespace-a/namespace-b/union-in-nested-n-s'; +export { SecondTableInA, SecondTableInAT } from './namespace-a/second-table-in-a'; +export { TableInFirstNS, TableInFirstNST } from './namespace-a/table-in-first-n-s'; +export { TableInC, TableInCT } from './namespace-c/table-in-c'; diff --git a/tests/namespace_test/namespace_test2_generated.rs b/tests/namespace_test/namespace_test2_generated.rs deleted file mode 100644 index 4db42a06a..000000000 --- a/tests/namespace_test/namespace_test2_generated.rs +++ /dev/null @@ -1,1062 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - - - -use std::mem; -use std::cmp::Ordering; - -extern crate flatbuffers; -use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod namespace_a { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; -#[allow(unused_imports, dead_code)] -pub mod namespace_b { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_UNION_IN_NESTED_NS: u8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_UNION_IN_NESTED_NS: u8 = 1; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_UNION_IN_NESTED_NS: [UnionInNestedNS; 2] = [ - UnionInNestedNS::NONE, - UnionInNestedNS::TableInNestedNS, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct UnionInNestedNS(pub u8); -#[allow(non_upper_case_globals)] -impl UnionInNestedNS { - pub const NONE: Self = Self(0); - pub const TableInNestedNS: Self = Self(1); - - pub const ENUM_MIN: u8 = 0; - pub const ENUM_MAX: u8 = 1; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::NONE, - Self::TableInNestedNS, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::NONE => Some("NONE"), - Self::TableInNestedNS => Some("TableInNestedNS"), - _ => None, - } - } -} -impl std::fmt::Debug for UnionInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for UnionInNestedNS { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for UnionInNestedNS { - type Output = UnionInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for UnionInNestedNS { - #[inline] - fn to_little_endian(self) -> Self { - let b = u8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = u8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for UnionInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - u8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for UnionInNestedNS {} -pub struct UnionInNestedNSUnionTableOffset {} - -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub enum UnionInNestedNST { - NONE, - TableInNestedNS(Box), -} -impl Default for UnionInNestedNST { - fn default() -> Self { - Self::NONE - } -} -impl UnionInNestedNST { - pub fn union_in_nested_ns_type(&self) -> UnionInNestedNS { - match self { - Self::NONE => UnionInNestedNS::NONE, - Self::TableInNestedNS(_) => UnionInNestedNS::TableInNestedNS, - } - } - pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option> { - match self { - Self::NONE => None, - Self::TableInNestedNS(v) => Some(v.pack(fbb).as_union_value()), - } - } - /// If the union variant matches, return the owned TableInNestedNST, setting the union to NONE. - pub fn take_table_in_nested_ns(&mut self) -> Option> { - if let Self::TableInNestedNS(_) = self { - let v = std::mem::replace(self, Self::NONE); - if let Self::TableInNestedNS(w) = v { - Some(w) - } else { - unreachable!() - } - } else { - None - } - } - /// If the union variant matches, return a reference to the TableInNestedNST. - pub fn as_table_in_nested_ns(&self) -> Option<&TableInNestedNST> { - if let Self::TableInNestedNS(v) = self { Some(v.as_ref()) } else { None } - } - /// If the union variant matches, return a mutable reference to the TableInNestedNST. - pub fn as_table_in_nested_ns_mut(&mut self) -> Option<&mut TableInNestedNST> { - if let Self::TableInNestedNS(v) = self { Some(v.as_mut()) } else { None } - } -} -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [ - EnumInNestedNS::A, - EnumInNestedNS::B, - EnumInNestedNS::C, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct EnumInNestedNS(pub i8); -#[allow(non_upper_case_globals)] -impl EnumInNestedNS { - pub const A: Self = Self(0); - pub const B: Self = Self(1); - pub const C: Self = Self(2); - - pub const ENUM_MIN: i8 = 0; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::A, - Self::B, - Self::C, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::A => Some("A"), - Self::B => Some("B"), - Self::C => Some("C"), - _ => None, - } - } -} -impl std::fmt::Debug for EnumInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for EnumInNestedNS { - type Output = EnumInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for EnumInNestedNS { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for EnumInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {} -// struct StructInNestedNS, aligned to 4 -#[repr(transparent)] -#[derive(Clone, Copy, PartialEq)] -pub struct StructInNestedNS(pub [u8; 8]); -impl Default for StructInNestedNS { - fn default() -> Self { - Self([0; 8]) - } -} -impl std::fmt::Debug for StructInNestedNS { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - f.debug_struct("StructInNestedNS") - .field("a", &self.a()) - .field("b", &self.b()) - .finish() - } -} - -impl flatbuffers::SimpleToVerifyInSlice for StructInNestedNS {} -impl flatbuffers::SafeSliceAccess for StructInNestedNS {} -impl<'a> flatbuffers::Follow<'a> for StructInNestedNS { - type Inner = &'a StructInNestedNS; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - <&'a StructInNestedNS>::follow(buf, loc) - } -} -impl<'a> flatbuffers::Follow<'a> for &'a StructInNestedNS { - type Inner = &'a StructInNestedNS; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - flatbuffers::follow_cast_ref::(buf, loc) - } -} -impl<'b> flatbuffers::Push for StructInNestedNS { - type Output = StructInNestedNS; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(self as *const StructInNestedNS as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} -impl<'b> flatbuffers::Push for &'b StructInNestedNS { - type Output = StructInNestedNS; - - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - let src = unsafe { - ::std::slice::from_raw_parts(*self as *const StructInNestedNS as *const u8, Self::size()) - }; - dst.copy_from_slice(src); - } -} - -impl<'a> flatbuffers::Verifiable for StructInNestedNS { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.in_buffer::(pos) - } -} -impl<'a> StructInNestedNS { - #[allow(clippy::too_many_arguments)] - pub fn new( - a: i32, - b: i32, - ) -> Self { - let mut s = Self([0; 8]); - s.set_a(a); - s.set_b(b); - s - } - - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.NamespaceB.StructInNestedNS" - } - - pub fn a(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[0..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_a(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[0..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn b(&self) -> i32 { - let mut mem = core::mem::MaybeUninit::::uninit(); - unsafe { - core::ptr::copy_nonoverlapping( - self.0[4..].as_ptr(), - mem.as_mut_ptr() as *mut u8, - core::mem::size_of::(), - ); - mem.assume_init() - }.from_little_endian() - } - - pub fn set_b(&mut self, x: i32) { - let x_le = x.to_little_endian(); - unsafe { - core::ptr::copy_nonoverlapping( - &x_le as *const i32 as *const u8, - self.0[4..].as_mut_ptr(), - core::mem::size_of::(), - ); - } - } - - pub fn unpack(&self) -> StructInNestedNST { - StructInNestedNST { - a: self.a(), - b: self.b(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Default)] -pub struct StructInNestedNST { - pub a: i32, - pub b: i32, -} -impl StructInNestedNST { - pub fn pack(&self) -> StructInNestedNS { - StructInNestedNS::new( - self.a, - self.b, - ) - } -} - -pub enum TableInNestedNSOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TableInNestedNS<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TableInNestedNS<'a> { - type Inner = TableInNestedNS<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TableInNestedNS<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.NamespaceB.TableInNestedNS" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TableInNestedNS { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TableInNestedNSArgs) -> flatbuffers::WIPOffset> { - let mut builder = TableInNestedNSBuilder::new(_fbb); - builder.add_foo(args.foo); - builder.finish() - } - - pub fn unpack(&self) -> TableInNestedNST { - let foo = self.foo(); - TableInNestedNST { - foo, - } - } - pub const VT_FOO: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn foo(&self) -> i32 { - self._tab.get::(TableInNestedNS::VT_FOO, Some(0)).unwrap() - } -} - -impl flatbuffers::Verifiable for TableInNestedNS<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::(&"foo", Self::VT_FOO, false)? - .finish(); - Ok(()) - } -} -pub struct TableInNestedNSArgs { - pub foo: i32, -} -impl<'a> Default for TableInNestedNSArgs { - #[inline] - fn default() -> Self { - TableInNestedNSArgs { - foo: 0, - } - } -} -pub struct TableInNestedNSBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TableInNestedNSBuilder<'a, 'b> { - #[inline] - pub fn add_foo(&mut self, foo: i32) { - self.fbb_.push_slot::(TableInNestedNS::VT_FOO, foo, 0); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInNestedNSBuilder<'a, 'b> { - let start = _fbb.start_table(); - TableInNestedNSBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TableInNestedNS<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TableInNestedNS"); - ds.field("foo", &self.foo()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TableInNestedNST { - pub foo: i32, -} -impl Default for TableInNestedNST { - fn default() -> Self { - Self { - foo: 0, - } - } -} -impl TableInNestedNST { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let foo = self.foo; - TableInNestedNS::create(_fbb, &TableInNestedNSArgs{ - foo, - }) - } -} -} // pub mod NamespaceB - -pub enum TableInFirstNSOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TableInFirstNS<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TableInFirstNS<'a> { - type Inner = TableInFirstNS<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TableInFirstNS<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.TableInFirstNS" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TableInFirstNS { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TableInFirstNSArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = TableInFirstNSBuilder::new(_fbb); - if let Some(x) = args.foo_struct { builder.add_foo_struct(x); } - if let Some(x) = args.foo_union { builder.add_foo_union(x); } - if let Some(x) = args.foo_table { builder.add_foo_table(x); } - builder.add_foo_union_type(args.foo_union_type); - builder.add_foo_enum(args.foo_enum); - builder.finish() - } - - pub fn unpack(&self) -> TableInFirstNST { - let foo_table = self.foo_table().map(|x| { - Box::new(x.unpack()) - }); - let foo_enum = self.foo_enum(); - let foo_union = match self.foo_union_type() { - namespace_b::UnionInNestedNS::NONE => namespace_b::UnionInNestedNST::NONE, - namespace_b::UnionInNestedNS::TableInNestedNS => namespace_b::UnionInNestedNST::TableInNestedNS(Box::new( - self.foo_union_as_table_in_nested_ns() - .expect("Invalid union table, expected `namespace_b::UnionInNestedNS::TableInNestedNS`.") - .unpack() - )), - _ => namespace_b::UnionInNestedNST::NONE, - }; - let foo_struct = self.foo_struct().map(|x| { - x.unpack() - }); - TableInFirstNST { - foo_table, - foo_enum, - foo_union, - foo_struct, - } - } - pub const VT_FOO_TABLE: flatbuffers::VOffsetT = 4; - pub const VT_FOO_ENUM: flatbuffers::VOffsetT = 6; - pub const VT_FOO_UNION_TYPE: flatbuffers::VOffsetT = 8; - pub const VT_FOO_UNION: flatbuffers::VOffsetT = 10; - pub const VT_FOO_STRUCT: flatbuffers::VOffsetT = 12; - - #[inline] - pub fn foo_table(&self) -> Option> { - self._tab.get::>(TableInFirstNS::VT_FOO_TABLE, None) - } - #[inline] - pub fn foo_enum(&self) -> namespace_b::EnumInNestedNS { - self._tab.get::(TableInFirstNS::VT_FOO_ENUM, Some(namespace_b::EnumInNestedNS::A)).unwrap() - } - #[inline] - pub fn foo_union_type(&self) -> namespace_b::UnionInNestedNS { - self._tab.get::(TableInFirstNS::VT_FOO_UNION_TYPE, Some(namespace_b::UnionInNestedNS::NONE)).unwrap() - } - #[inline] - pub fn foo_union(&self) -> Option> { - self._tab.get::>>(TableInFirstNS::VT_FOO_UNION, None) - } - #[inline] - pub fn foo_struct(&self) -> Option<&'a namespace_b::StructInNestedNS> { - self._tab.get::(TableInFirstNS::VT_FOO_STRUCT, None) - } - #[inline] - #[allow(non_snake_case)] - pub fn foo_union_as_table_in_nested_ns(&self) -> Option> { - if self.foo_union_type() == namespace_b::UnionInNestedNS::TableInNestedNS { - self.foo_union().map(namespace_b::TableInNestedNS::init_from_table) - } else { - None - } - } - -} - -impl flatbuffers::Verifiable for TableInFirstNS<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"foo_table", Self::VT_FOO_TABLE, false)? - .visit_field::(&"foo_enum", Self::VT_FOO_ENUM, false)? - .visit_union::(&"foo_union_type", Self::VT_FOO_UNION_TYPE, &"foo_union", Self::VT_FOO_UNION, false, |key, v, pos| { - match key { - namespace_b::UnionInNestedNS::TableInNestedNS => v.verify_union_variant::>("namespace_b::UnionInNestedNS::TableInNestedNS", pos), - _ => Ok(()), - } - })? - .visit_field::(&"foo_struct", Self::VT_FOO_STRUCT, false)? - .finish(); - Ok(()) - } -} -pub struct TableInFirstNSArgs<'a> { - pub foo_table: Option>>, - pub foo_enum: namespace_b::EnumInNestedNS, - pub foo_union_type: namespace_b::UnionInNestedNS, - pub foo_union: Option>, - pub foo_struct: Option<&'a namespace_b::StructInNestedNS>, -} -impl<'a> Default for TableInFirstNSArgs<'a> { - #[inline] - fn default() -> Self { - TableInFirstNSArgs { - foo_table: None, - foo_enum: namespace_b::EnumInNestedNS::A, - foo_union_type: namespace_b::UnionInNestedNS::NONE, - foo_union: None, - foo_struct: None, - } - } -} -pub struct TableInFirstNSBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TableInFirstNSBuilder<'a, 'b> { - #[inline] - pub fn add_foo_table(&mut self, foo_table: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TableInFirstNS::VT_FOO_TABLE, foo_table); - } - #[inline] - pub fn add_foo_enum(&mut self, foo_enum: namespace_b::EnumInNestedNS) { - self.fbb_.push_slot::(TableInFirstNS::VT_FOO_ENUM, foo_enum, namespace_b::EnumInNestedNS::A); - } - #[inline] - pub fn add_foo_union_type(&mut self, foo_union_type: namespace_b::UnionInNestedNS) { - self.fbb_.push_slot::(TableInFirstNS::VT_FOO_UNION_TYPE, foo_union_type, namespace_b::UnionInNestedNS::NONE); - } - #[inline] - pub fn add_foo_union(&mut self, foo_union: flatbuffers::WIPOffset) { - self.fbb_.push_slot_always::>(TableInFirstNS::VT_FOO_UNION, foo_union); - } - #[inline] - pub fn add_foo_struct(&mut self, foo_struct: &namespace_b::StructInNestedNS) { - self.fbb_.push_slot_always::<&namespace_b::StructInNestedNS>(TableInFirstNS::VT_FOO_STRUCT, foo_struct); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInFirstNSBuilder<'a, 'b> { - let start = _fbb.start_table(); - TableInFirstNSBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TableInFirstNS<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TableInFirstNS"); - ds.field("foo_table", &self.foo_table()); - ds.field("foo_enum", &self.foo_enum()); - ds.field("foo_union_type", &self.foo_union_type()); - match self.foo_union_type() { - namespace_b::UnionInNestedNS::TableInNestedNS => { - if let Some(x) = self.foo_union_as_table_in_nested_ns() { - ds.field("foo_union", &x) - } else { - ds.field("foo_union", &"InvalidFlatbuffer: Union discriminant does not match value.") - } - }, - _ => { - let x: Option<()> = None; - ds.field("foo_union", &x) - }, - }; - ds.field("foo_struct", &self.foo_struct()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TableInFirstNST { - pub foo_table: Option>, - pub foo_enum: namespace_b::EnumInNestedNS, - pub foo_union: namespace_b::UnionInNestedNST, - pub foo_struct: Option, -} -impl Default for TableInFirstNST { - fn default() -> Self { - Self { - foo_table: None, - foo_enum: namespace_b::EnumInNestedNS::A, - foo_union: namespace_b::UnionInNestedNST::NONE, - foo_struct: None, - } - } -} -impl TableInFirstNST { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let foo_table = self.foo_table.as_ref().map(|x|{ - x.pack(_fbb) - }); - let foo_enum = self.foo_enum; - let foo_union_type = self.foo_union.union_in_nested_ns_type(); - let foo_union = self.foo_union.pack(_fbb); - let foo_struct_tmp = self.foo_struct.as_ref().map(|x| x.pack()); - let foo_struct = foo_struct_tmp.as_ref(); - TableInFirstNS::create(_fbb, &TableInFirstNSArgs{ - foo_table, - foo_enum, - foo_union_type, - foo_union, - foo_struct, - }) - } -} -pub enum SecondTableInAOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct SecondTableInA<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for SecondTableInA<'a> { - type Inner = SecondTableInA<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> SecondTableInA<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceA.SecondTableInA" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - SecondTableInA { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args SecondTableInAArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = SecondTableInABuilder::new(_fbb); - if let Some(x) = args.refer_to_c { builder.add_refer_to_c(x); } - builder.finish() - } - - pub fn unpack(&self) -> SecondTableInAT { - let refer_to_c = self.refer_to_c().map(|x| { - Box::new(x.unpack()) - }); - SecondTableInAT { - refer_to_c, - } - } - pub const VT_REFER_TO_C: flatbuffers::VOffsetT = 4; - - #[inline] - pub fn refer_to_c(&self) -> Option> { - self._tab.get::>(SecondTableInA::VT_REFER_TO_C, None) - } -} - -impl flatbuffers::Verifiable for SecondTableInA<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"refer_to_c", Self::VT_REFER_TO_C, false)? - .finish(); - Ok(()) - } -} -pub struct SecondTableInAArgs<'a> { - pub refer_to_c: Option>>, -} -impl<'a> Default for SecondTableInAArgs<'a> { - #[inline] - fn default() -> Self { - SecondTableInAArgs { - refer_to_c: None, - } - } -} -pub struct SecondTableInABuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> SecondTableInABuilder<'a, 'b> { - #[inline] - pub fn add_refer_to_c(&mut self, refer_to_c: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(SecondTableInA::VT_REFER_TO_C, refer_to_c); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> SecondTableInABuilder<'a, 'b> { - let start = _fbb.start_table(); - SecondTableInABuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for SecondTableInA<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("SecondTableInA"); - ds.field("refer_to_c", &self.refer_to_c()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct SecondTableInAT { - pub refer_to_c: Option>, -} -impl Default for SecondTableInAT { - fn default() -> Self { - Self { - refer_to_c: None, - } - } -} -impl SecondTableInAT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let refer_to_c = self.refer_to_c.as_ref().map(|x|{ - x.pack(_fbb) - }); - SecondTableInA::create(_fbb, &SecondTableInAArgs{ - refer_to_c, - }) - } -} -} // pub mod NamespaceA - -#[allow(unused_imports, dead_code)] -pub mod namespace_c { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -pub enum TableInCOffset {} -#[derive(Copy, Clone, PartialEq)] - -pub struct TableInC<'a> { - pub _tab: flatbuffers::Table<'a>, -} - -impl<'a> flatbuffers::Follow<'a> for TableInC<'a> { - type Inner = TableInC<'a>; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { _tab: flatbuffers::Table { buf, loc } } - } -} - -impl<'a> TableInC<'a> { - pub const fn get_fully_qualified_name() -> &'static str { - "NamespaceC.TableInC" - } - - #[inline] - pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { - TableInC { _tab: table } - } - #[allow(unused_mut)] - pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( - _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, - args: &'args TableInCArgs<'args>) -> flatbuffers::WIPOffset> { - let mut builder = TableInCBuilder::new(_fbb); - if let Some(x) = args.refer_to_a2 { builder.add_refer_to_a2(x); } - if let Some(x) = args.refer_to_a1 { builder.add_refer_to_a1(x); } - builder.finish() - } - - pub fn unpack(&self) -> TableInCT { - let refer_to_a1 = self.refer_to_a1().map(|x| { - Box::new(x.unpack()) - }); - let refer_to_a2 = self.refer_to_a2().map(|x| { - Box::new(x.unpack()) - }); - TableInCT { - refer_to_a1, - refer_to_a2, - } - } - pub const VT_REFER_TO_A1: flatbuffers::VOffsetT = 4; - pub const VT_REFER_TO_A2: flatbuffers::VOffsetT = 6; - - #[inline] - pub fn refer_to_a1(&self) -> Option> { - self._tab.get::>(TableInC::VT_REFER_TO_A1, None) - } - #[inline] - pub fn refer_to_a2(&self) -> Option> { - self._tab.get::>(TableInC::VT_REFER_TO_A2, None) - } -} - -impl flatbuffers::Verifiable for TableInC<'_> { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - v.visit_table(pos)? - .visit_field::>(&"refer_to_a1", Self::VT_REFER_TO_A1, false)? - .visit_field::>(&"refer_to_a2", Self::VT_REFER_TO_A2, false)? - .finish(); - Ok(()) - } -} -pub struct TableInCArgs<'a> { - pub refer_to_a1: Option>>, - pub refer_to_a2: Option>>, -} -impl<'a> Default for TableInCArgs<'a> { - #[inline] - fn default() -> Self { - TableInCArgs { - refer_to_a1: None, - refer_to_a2: None, - } - } -} -pub struct TableInCBuilder<'a: 'b, 'b> { - fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, - start_: flatbuffers::WIPOffset, -} -impl<'a: 'b, 'b> TableInCBuilder<'a, 'b> { - #[inline] - pub fn add_refer_to_a1(&mut self, refer_to_a1: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TableInC::VT_REFER_TO_A1, refer_to_a1); - } - #[inline] - pub fn add_refer_to_a2(&mut self, refer_to_a2: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(TableInC::VT_REFER_TO_A2, refer_to_a2); - } - #[inline] - pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TableInCBuilder<'a, 'b> { - let start = _fbb.start_table(); - TableInCBuilder { - fbb_: _fbb, - start_: start, - } - } - #[inline] - pub fn finish(self) -> flatbuffers::WIPOffset> { - let o = self.fbb_.end_table(self.start_); - flatbuffers::WIPOffset::new(o.value()) - } -} - -impl std::fmt::Debug for TableInC<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut ds = f.debug_struct("TableInC"); - ds.field("refer_to_a1", &self.refer_to_a1()); - ds.field("refer_to_a2", &self.refer_to_a2()); - ds.finish() - } -} -#[non_exhaustive] -#[derive(Debug, Clone, PartialEq)] -pub struct TableInCT { - pub refer_to_a1: Option>, - pub refer_to_a2: Option>, -} -impl Default for TableInCT { - fn default() -> Self { - Self { - refer_to_a1: None, - refer_to_a2: None, - } - } -} -impl TableInCT { - pub fn pack<'b>( - &self, - _fbb: &mut flatbuffers::FlatBufferBuilder<'b> - ) -> flatbuffers::WIPOffset> { - let refer_to_a1 = self.refer_to_a1.as_ref().map(|x|{ - x.pack(_fbb) - }); - let refer_to_a2 = self.refer_to_a2.as_ref().map(|x|{ - x.pack(_fbb) - }); - TableInC::create(_fbb, &TableInCArgs{ - refer_to_a1, - refer_to_a2, - }) - } -} -} // pub mod NamespaceC - diff --git a/tests/optional_scalars.ts b/tests/optional_scalars.ts new file mode 100644 index 000000000..0892a2fc0 --- /dev/null +++ b/tests/optional_scalars.ts @@ -0,0 +1 @@ +export { OptionalByte } from './optional_scalars/optional-byte'; diff --git a/tests/optional_scalars/mod.rs b/tests/optional_scalars/mod.rs new file mode 100644 index 000000000..ef52c12b8 --- /dev/null +++ b/tests/optional_scalars/mod.rs @@ -0,0 +1,8 @@ +// Automatically generated by the Flatbuffers compiler. Do not modify. +pub mod optional_scalars { + use super::*; + mod optional_byte_generated; + pub use self::optional_byte_generated::*; + mod scalar_stuff_generated; + pub use self::scalar_stuff_generated::*; +} // optional_scalars diff --git a/tests/optional_scalars/optional_scalars/optional_byte_generated.rs b/tests/optional_scalars/optional_scalars/optional_byte_generated.rs new file mode 100644 index 000000000..226e9ecef --- /dev/null +++ b/tests/optional_scalars/optional_scalars/optional_byte_generated.rs @@ -0,0 +1,97 @@ +// automatically generated by the FlatBuffers compiler, do not modify +extern crate flatbuffers; +use std::mem; +use std::cmp::Ordering; +use self::flatbuffers::{EndianScalar, Follow}; +use super::*; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MIN_OPTIONAL_BYTE: i8 = 0; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +pub const ENUM_MAX_OPTIONAL_BYTE: i8 = 2; +#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] +#[allow(non_camel_case_types)] +pub const ENUM_VALUES_OPTIONAL_BYTE: [OptionalByte; 3] = [ + OptionalByte::None, + OptionalByte::One, + OptionalByte::Two, +]; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +#[repr(transparent)] +pub struct OptionalByte(pub i8); +#[allow(non_upper_case_globals)] +impl OptionalByte { + pub const None: Self = Self(0); + pub const One: Self = Self(1); + pub const Two: Self = Self(2); + + pub const ENUM_MIN: i8 = 0; + pub const ENUM_MAX: i8 = 2; + pub const ENUM_VALUES: &'static [Self] = &[ + Self::None, + Self::One, + Self::Two, + ]; + /// Returns the variant's name or "" if unknown. + pub fn variant_name(self) -> Option<&'static str> { + match self { + Self::None => Some("None"), + Self::One => Some("One"), + Self::Two => Some("Two"), + _ => None, + } + } +} +impl std::fmt::Debug for OptionalByte { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(name) = self.variant_name() { + f.write_str(name) + } else { + f.write_fmt(format_args!("", self.0)) + } + } +} +impl<'a> flatbuffers::Follow<'a> for OptionalByte { + type Inner = Self; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + let b = unsafe { + flatbuffers::read_scalar_at::(buf, loc) + }; + Self(b) + } +} + +impl flatbuffers::Push for OptionalByte { + type Output = OptionalByte; + #[inline] + fn push(&self, dst: &mut [u8], _rest: &[u8]) { + unsafe { flatbuffers::emplace_scalar::(dst, self.0); } + } +} + +impl flatbuffers::EndianScalar for OptionalByte { + #[inline] + fn to_little_endian(self) -> Self { + let b = i8::to_le(self.0); + Self(b) + } + #[inline] + #[allow(clippy::wrong_self_convention)] + fn from_little_endian(self) -> Self { + let b = i8::from_le(self.0); + Self(b) + } +} + +impl<'a> flatbuffers::Verifiable for OptionalByte { + #[inline] + fn run_verifier( + v: &mut flatbuffers::Verifier, pos: usize + ) -> Result<(), flatbuffers::InvalidFlatbuffer> { + use self::flatbuffers::Verifiable; + i8::run_verifier(v, pos) + } +} + +impl flatbuffers::SimpleToVerifyInSlice for OptionalByte {} diff --git a/tests/optional_scalars_generated.rs b/tests/optional_scalars/optional_scalars/scalar_stuff_generated.rs similarity index 85% rename from tests/optional_scalars_generated.rs rename to tests/optional_scalars/optional_scalars/scalar_stuff_generated.rs index 392fdf58a..16a3a5e84 100644 --- a/tests/optional_scalars_generated.rs +++ b/tests/optional_scalars/optional_scalars/scalar_stuff_generated.rs @@ -1,113 +1,9 @@ // automatically generated by the FlatBuffers compiler, do not modify - - - +extern crate flatbuffers; use std::mem; use std::cmp::Ordering; - -extern crate flatbuffers; use self::flatbuffers::{EndianScalar, Follow}; - -#[allow(unused_imports, dead_code)] -pub mod optional_scalars { - - use std::mem; - use std::cmp::Ordering; - - extern crate flatbuffers; - use self::flatbuffers::{EndianScalar, Follow}; - -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MIN_OPTIONAL_BYTE: i8 = 0; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -pub const ENUM_MAX_OPTIONAL_BYTE: i8 = 2; -#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")] -#[allow(non_camel_case_types)] -pub const ENUM_VALUES_OPTIONAL_BYTE: [OptionalByte; 3] = [ - OptionalByte::None, - OptionalByte::One, - OptionalByte::Two, -]; - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] -#[repr(transparent)] -pub struct OptionalByte(pub i8); -#[allow(non_upper_case_globals)] -impl OptionalByte { - pub const None: Self = Self(0); - pub const One: Self = Self(1); - pub const Two: Self = Self(2); - - pub const ENUM_MIN: i8 = 0; - pub const ENUM_MAX: i8 = 2; - pub const ENUM_VALUES: &'static [Self] = &[ - Self::None, - Self::One, - Self::Two, - ]; - /// Returns the variant's name or "" if unknown. - pub fn variant_name(self) -> Option<&'static str> { - match self { - Self::None => Some("None"), - Self::One => Some("One"), - Self::Two => Some("Two"), - _ => None, - } - } -} -impl std::fmt::Debug for OptionalByte { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if let Some(name) = self.variant_name() { - f.write_str(name) - } else { - f.write_fmt(format_args!("", self.0)) - } - } -} -impl<'a> flatbuffers::Follow<'a> for OptionalByte { - type Inner = Self; - #[inline] - fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - let b = unsafe { - flatbuffers::read_scalar_at::(buf, loc) - }; - Self(b) - } -} - -impl flatbuffers::Push for OptionalByte { - type Output = OptionalByte; - #[inline] - fn push(&self, dst: &mut [u8], _rest: &[u8]) { - unsafe { flatbuffers::emplace_scalar::(dst, self.0); } - } -} - -impl flatbuffers::EndianScalar for OptionalByte { - #[inline] - fn to_little_endian(self) -> Self { - let b = i8::to_le(self.0); - Self(b) - } - #[inline] - #[allow(clippy::wrong_self_convention)] - fn from_little_endian(self) -> Self { - let b = i8::from_le(self.0); - Self(b) - } -} - -impl<'a> flatbuffers::Verifiable for OptionalByte { - #[inline] - fn run_verifier( - v: &mut flatbuffers::Verifier, pos: usize - ) -> Result<(), flatbuffers::InvalidFlatbuffer> { - use self::flatbuffers::Verifiable; - i8::run_verifier(v, pos) - } -} - -impl flatbuffers::SimpleToVerifyInSlice for OptionalByte {} +use super::*; pub enum ScalarStuffOffset {} #[derive(Copy, Clone, PartialEq)] @@ -124,6 +20,10 @@ impl<'a> flatbuffers::Follow<'a> for ScalarStuff<'a> { } impl<'a> ScalarStuff<'a> { + pub const fn get_fully_qualified_name() -> &'static str { + "optional_scalars.ScalarStuff" + } + #[inline] pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { ScalarStuff { _tab: table } @@ -438,42 +338,42 @@ impl flatbuffers::Verifiable for ScalarStuff<'_> { ) -> Result<(), flatbuffers::InvalidFlatbuffer> { use self::flatbuffers::Verifiable; v.visit_table(pos)? - .visit_field::(&"just_i8", Self::VT_JUST_I8, false)? - .visit_field::(&"maybe_i8", Self::VT_MAYBE_I8, false)? - .visit_field::(&"default_i8", Self::VT_DEFAULT_I8, false)? - .visit_field::(&"just_u8", Self::VT_JUST_U8, false)? - .visit_field::(&"maybe_u8", Self::VT_MAYBE_U8, false)? - .visit_field::(&"default_u8", Self::VT_DEFAULT_U8, false)? - .visit_field::(&"just_i16", Self::VT_JUST_I16, false)? - .visit_field::(&"maybe_i16", Self::VT_MAYBE_I16, false)? - .visit_field::(&"default_i16", Self::VT_DEFAULT_I16, false)? - .visit_field::(&"just_u16", Self::VT_JUST_U16, false)? - .visit_field::(&"maybe_u16", Self::VT_MAYBE_U16, false)? - .visit_field::(&"default_u16", Self::VT_DEFAULT_U16, false)? - .visit_field::(&"just_i32", Self::VT_JUST_I32, false)? - .visit_field::(&"maybe_i32", Self::VT_MAYBE_I32, false)? - .visit_field::(&"default_i32", Self::VT_DEFAULT_I32, false)? - .visit_field::(&"just_u32", Self::VT_JUST_U32, false)? - .visit_field::(&"maybe_u32", Self::VT_MAYBE_U32, false)? - .visit_field::(&"default_u32", Self::VT_DEFAULT_U32, false)? - .visit_field::(&"just_i64", Self::VT_JUST_I64, false)? - .visit_field::(&"maybe_i64", Self::VT_MAYBE_I64, false)? - .visit_field::(&"default_i64", Self::VT_DEFAULT_I64, false)? - .visit_field::(&"just_u64", Self::VT_JUST_U64, false)? - .visit_field::(&"maybe_u64", Self::VT_MAYBE_U64, false)? - .visit_field::(&"default_u64", Self::VT_DEFAULT_U64, false)? - .visit_field::(&"just_f32", Self::VT_JUST_F32, false)? - .visit_field::(&"maybe_f32", Self::VT_MAYBE_F32, false)? - .visit_field::(&"default_f32", Self::VT_DEFAULT_F32, false)? - .visit_field::(&"just_f64", Self::VT_JUST_F64, false)? - .visit_field::(&"maybe_f64", Self::VT_MAYBE_F64, false)? - .visit_field::(&"default_f64", Self::VT_DEFAULT_F64, false)? - .visit_field::(&"just_bool", Self::VT_JUST_BOOL, false)? - .visit_field::(&"maybe_bool", Self::VT_MAYBE_BOOL, false)? - .visit_field::(&"default_bool", Self::VT_DEFAULT_BOOL, false)? - .visit_field::(&"just_enum", Self::VT_JUST_ENUM, false)? - .visit_field::(&"maybe_enum", Self::VT_MAYBE_ENUM, false)? - .visit_field::(&"default_enum", Self::VT_DEFAULT_ENUM, false)? + .visit_field::("just_i8", Self::VT_JUST_I8, false)? + .visit_field::("maybe_i8", Self::VT_MAYBE_I8, false)? + .visit_field::("default_i8", Self::VT_DEFAULT_I8, false)? + .visit_field::("just_u8", Self::VT_JUST_U8, false)? + .visit_field::("maybe_u8", Self::VT_MAYBE_U8, false)? + .visit_field::("default_u8", Self::VT_DEFAULT_U8, false)? + .visit_field::("just_i16", Self::VT_JUST_I16, false)? + .visit_field::("maybe_i16", Self::VT_MAYBE_I16, false)? + .visit_field::("default_i16", Self::VT_DEFAULT_I16, false)? + .visit_field::("just_u16", Self::VT_JUST_U16, false)? + .visit_field::("maybe_u16", Self::VT_MAYBE_U16, false)? + .visit_field::("default_u16", Self::VT_DEFAULT_U16, false)? + .visit_field::("just_i32", Self::VT_JUST_I32, false)? + .visit_field::("maybe_i32", Self::VT_MAYBE_I32, false)? + .visit_field::("default_i32", Self::VT_DEFAULT_I32, false)? + .visit_field::("just_u32", Self::VT_JUST_U32, false)? + .visit_field::("maybe_u32", Self::VT_MAYBE_U32, false)? + .visit_field::("default_u32", Self::VT_DEFAULT_U32, false)? + .visit_field::("just_i64", Self::VT_JUST_I64, false)? + .visit_field::("maybe_i64", Self::VT_MAYBE_I64, false)? + .visit_field::("default_i64", Self::VT_DEFAULT_I64, false)? + .visit_field::("just_u64", Self::VT_JUST_U64, false)? + .visit_field::("maybe_u64", Self::VT_MAYBE_U64, false)? + .visit_field::("default_u64", Self::VT_DEFAULT_U64, false)? + .visit_field::("just_f32", Self::VT_JUST_F32, false)? + .visit_field::("maybe_f32", Self::VT_MAYBE_F32, false)? + .visit_field::("default_f32", Self::VT_DEFAULT_F32, false)? + .visit_field::("just_f64", Self::VT_JUST_F64, false)? + .visit_field::("maybe_f64", Self::VT_MAYBE_F64, false)? + .visit_field::("default_f64", Self::VT_DEFAULT_F64, false)? + .visit_field::("just_bool", Self::VT_JUST_BOOL, false)? + .visit_field::("maybe_bool", Self::VT_MAYBE_BOOL, false)? + .visit_field::("default_bool", Self::VT_DEFAULT_BOOL, false)? + .visit_field::("just_enum", Self::VT_JUST_ENUM, false)? + .visit_field::("maybe_enum", Self::VT_MAYBE_ENUM, false)? + .visit_field::("default_enum", Self::VT_DEFAULT_ENUM, false)? .finish(); Ok(()) } @@ -1025,5 +925,3 @@ pub fn finish_scalar_stuff_buffer<'a, 'b>( pub fn finish_size_prefixed_scalar_stuff_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset>) { fbb.finish_size_prefixed(root, Some(SCALAR_STUFF_IDENTIFIER)); } -} // pub mod optional_scalars - diff --git a/tests/rust_usage_test/benches/benchmarks.rs b/tests/rust_usage_test/benches/benchmarks.rs index bfe63b6d1..51fae7572 100644 --- a/tests/rust_usage_test/benches/benchmarks.rs +++ b/tests/rust_usage_test/benches/benchmarks.rs @@ -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; diff --git a/tests/rust_usage_test/benches/flatbuffers_benchmarks.rs b/tests/rust_usage_test/benches/flatbuffers_benchmarks.rs index ee6d81db7..575f47a81 100644 --- a/tests/rust_usage_test/benches/flatbuffers_benchmarks.rs +++ b/tests/rust_usage_test/benches/flatbuffers_benchmarks.rs @@ -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; diff --git a/tests/rust_usage_test/bin/flatbuffers_alloc_check.rs b/tests/rust_usage_test/bin/flatbuffers_alloc_check.rs index 84abd1e0d..418783c65 100644 --- a/tests/rust_usage_test/bin/flatbuffers_alloc_check.rs +++ b/tests/rust_usage_test/bin/flatbuffers_alloc_check.rs @@ -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; diff --git a/tests/rust_usage_test/bin/monster_example.rs b/tests/rust_usage_test/bin/monster_example.rs index 2e3c7ac60..17ce0e0f8 100644 --- a/tests/rust_usage_test/bin/monster_example.rs +++ b/tests/rust_usage_test/bin/monster_example.rs @@ -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; diff --git a/tests/rust_usage_test/outdir/.gitignore b/tests/rust_usage_test/outdir/.gitignore new file mode 100644 index 000000000..c83f90a2b --- /dev/null +++ b/tests/rust_usage_test/outdir/.gitignore @@ -0,0 +1 @@ +src/generated diff --git a/tests/rust_usage_test/outdir/build.rs b/tests/rust_usage_test/outdir/build.rs index 7690192b7..046282887 100644 --- a/tests/rust_usage_test/outdir/build.rs +++ b/tests/rust_usage_test/outdir/build.rs @@ -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(); + } } diff --git a/tests/rust_usage_test/outdir/src/main.rs b/tests/rust_usage_test/outdir/src/main.rs index b5fcaf8a6..734e1375d 100644 --- a/tests/rust_usage_test/outdir/src/main.rs +++ b/tests/rust_usage_test/outdir/src/main.rs @@ -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::(fbb.finished_data()).unwrap(); assert_eq!(mon.hp(), 1); diff --git a/tests/rust_usage_test/tests/arrays_test.rs b/tests/rust_usage_test/tests/arrays_test.rs index 41e7590fa..faa0ac8d2 100644 --- a/tests/rust_usage_test/tests/arrays_test.rs +++ b/tests/rust_usage_test/tests/arrays_test.rs @@ -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; diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index 9855c02bf..71c2d86ef 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -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)] diff --git a/tests/rust_usage_test/tests/more_defaults_test.rs b/tests/rust_usage_test/tests/more_defaults_test.rs index c1ed81f6f..af664f258 100644 --- a/tests/rust_usage_test/tests/more_defaults_test.rs +++ b/tests/rust_usage_test/tests/more_defaults_test.rs @@ -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::*; diff --git a/tests/rust_usage_test/tests/optional_scalars_test.rs b/tests/rust_usage_test/tests/optional_scalars_test.rs index d6c8422dd..f8588c538 100644 --- a/tests/rust_usage_test/tests/optional_scalars_test.rs +++ b/tests/rust_usage_test/tests/optional_scalars_test.rs @@ -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::*;