From 543c1bbeba18fc4e29c7b6163f0d56ea1c209926 Mon Sep 17 00:00:00 2001 From: Casper Date: Sat, 10 Oct 2020 11:49:02 -0700 Subject: [PATCH] Fixed rust nested flatbuffers for tables other than self (#6062) * Fixed rust nested flatbuffers for tables other than self * replaced lifetimes * Use WrapInNameSpace and also update samples Co-authored-by: Casper Neo --- samples/monster_generated.rs | 116 ++++++++++++++++++-------------- src/idl_gen_rust.cpp | 20 ++---- tests/monster_test.fbs | 6 +- tests/monster_test_generated.rs | 13 ++-- 4 files changed, 79 insertions(+), 76 deletions(-) diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs index 9ec573c7c..f793f8524 100644 --- a/samples/monster_generated.rs +++ b/samples/monster_generated.rs @@ -1,22 +1,25 @@ // automatically generated by the FlatBuffers compiler, do not modify + +use std::mem; +use std::cmp::Ordering; + +extern crate flatbuffers; +use self::flatbuffers::EndianScalar; + +#[allow(unused_imports, dead_code)] pub mod my_game { - #![allow(dead_code)] - #![allow(unused_imports)] use std::mem; - use std::marker::PhantomData; use std::cmp::Ordering; extern crate flatbuffers; use self::flatbuffers::EndianScalar; +#[allow(unused_imports, dead_code)] pub mod sample { - #![allow(dead_code)] - #![allow(unused_imports)] use std::mem; - use std::marker::PhantomData; use std::cmp::Ordering; extern crate flatbuffers; @@ -24,15 +27,16 @@ pub mod sample { #[allow(non_camel_case_types)] #[repr(i8)] -#[derive(Clone, Copy, PartialEq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub enum Color { Red = 0, Green = 1, - Blue = 2 + Blue = 2, + } -const ENUM_MIN_COLOR: i8 = 0; -const ENUM_MAX_COLOR: i8 = 2; +pub const ENUM_MIN_COLOR: i8 = 0; +pub const ENUM_MAX_COLOR: i8 = 2; impl<'a> flatbuffers::Follow<'a> for Color { type Inner = Self; @@ -66,34 +70,35 @@ impl flatbuffers::Push for Color { } #[allow(non_camel_case_types)] -const ENUM_VALUES_COLOR:[Color; 3] = [ +pub const ENUM_VALUES_COLOR: [Color; 3] = [ Color::Red, Color::Green, Color::Blue ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_COLOR:[&'static str; 3] = [ +pub const ENUM_NAMES_COLOR: [&str; 3] = [ "Red", "Green", "Blue" ]; pub fn enum_name_color(e: Color) -> &'static str { - let index: usize = e as usize; - ENUM_NAMES_COLOR[index] + let index = e as i8; + ENUM_NAMES_COLOR[index as usize] } #[allow(non_camel_case_types)] #[repr(u8)] -#[derive(Clone, Copy, PartialEq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub enum Equipment { NONE = 0, - Weapon = 1 + Weapon = 1, + } -const ENUM_MIN_EQUIPMENT: u8 = 0; -const ENUM_MAX_EQUIPMENT: u8 = 1; +pub const ENUM_MIN_EQUIPMENT: u8 = 0; +pub const ENUM_MAX_EQUIPMENT: u8 = 1; impl<'a> flatbuffers::Follow<'a> for Equipment { type Inner = Self; @@ -127,20 +132,20 @@ impl flatbuffers::Push for Equipment { } #[allow(non_camel_case_types)] -const ENUM_VALUES_EQUIPMENT:[Equipment; 2] = [ +pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [ Equipment::NONE, Equipment::Weapon ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_EQUIPMENT:[&'static str; 2] = [ +pub const ENUM_NAMES_EQUIPMENT: [&str; 2] = [ "NONE", "Weapon" ]; pub fn enum_name_equipment(e: Equipment) -> &'static str { - let index: usize = e as usize; - ENUM_NAMES_EQUIPMENT[index] + let index = e as u8; + ENUM_NAMES_EQUIPMENT[index as usize] } pub struct EquipmentUnionTableOffset {} @@ -158,7 +163,6 @@ impl<'a> flatbuffers::Follow<'a> for Vec3 { #[inline] fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { <&'a Vec3>::follow(buf, loc) - //flatbuffers::follow_cast_ref::(buf, loc) } } impl<'a> flatbuffers::Follow<'a> for &'a Vec3 { @@ -192,7 +196,7 @@ impl<'b> flatbuffers::Push for &'b Vec3 { impl Vec3 { - pub fn new<'a>(_x: f32, _y: f32, _z: f32) -> Self { + pub fn new(_x: f32, _y: f32, _z: f32) -> Self { Vec3 { x_: _x.to_little_endian(), y_: _y.to_little_endian(), @@ -200,13 +204,13 @@ impl Vec3 { } } - pub fn x<'a>(&'a self) -> f32 { + pub fn x(&self) -> f32 { self.x_.from_little_endian() } - pub fn y<'a>(&'a self) -> f32 { + pub fn y(&self) -> f32 { self.y_.from_little_endian() } - pub fn z<'a>(&'a self) -> f32 { + pub fn z(&self) -> f32 { self.z_.from_little_endian() } } @@ -222,9 +226,7 @@ impl<'a> flatbuffers::Follow<'a> for Monster<'a> { type Inner = Monster<'a>; #[inline] fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } + Self { _tab: flatbuffers::Table { buf, loc } } } } @@ -240,6 +242,7 @@ impl<'a> Monster<'a> { _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); } @@ -261,48 +264,53 @@ impl<'a> Monster<'a> { 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(&'a self) -> Option<&'a Vec3> { + pub fn pos(&self) -> Option<&'a Vec3> { self._tab.get::(Monster::VT_POS, None) } #[inline] - pub fn mana(&'a self) -> i16 { + pub fn mana(&self) -> i16 { self._tab.get::(Monster::VT_MANA, Some(150)).unwrap() } #[inline] - pub fn hp(&'a self) -> i16 { + pub fn hp(&self) -> i16 { self._tab.get::(Monster::VT_HP, Some(100)).unwrap() } #[inline] - pub fn name(&'a self) -> Option<&'a str> { + pub fn name(&self) -> Option<&'a str> { self._tab.get::>(Monster::VT_NAME, None) } #[inline] - pub fn inventory(&'a self) -> Option<&'a [u8]> { + pub fn inventory(&self) -> Option<&'a [u8]> { self._tab.get::>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice()) } #[inline] - pub fn color(&'a self) -> Color { + pub fn color(&self) -> Color { self._tab.get::(Monster::VT_COLOR, Some(Color::Blue)).unwrap() } #[inline] - pub fn weapons(&'a self) -> Option>>> { + pub fn weapons(&self) -> Option>>> { self._tab.get::>>>>(Monster::VT_WEAPONS, None) } #[inline] - pub fn equipped_type(&'a self) -> Equipment { + pub fn equipped_type(&self) -> Equipment { self._tab.get::(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap() } #[inline] - pub fn equipped(&'a self) -> Option> { + 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(&'a self) -> Option { + pub fn equipped_as_weapon(&self) -> Option> { if self.equipped_type() == Equipment::Weapon { - self.equipped().map(|u| Weapon::init_from_table(u)) + self.equipped().map(Weapon::init_from_table) } else { None } @@ -311,15 +319,16 @@ impl<'a> Monster<'a> { } pub struct MonsterArgs<'a> { - pub pos: Option<&'a Vec3>, + pub pos: Option<&'a Vec3>, pub mana: i16, pub hp: i16, - pub name: Option>, - pub inventory: Option>>, + pub name: Option>, + pub inventory: Option>>, pub color: Color, - pub weapons: Option>>>>, + pub weapons: Option>>>>, pub equipped_type: Equipment, pub equipped: Option>, + pub path: Option>>, } impl<'a> Default for MonsterArgs<'a> { #[inline] @@ -334,6 +343,7 @@ impl<'a> Default for MonsterArgs<'a> { weapons: None, equipped_type: Equipment::NONE, equipped: None, + path: None, } } } @@ -343,7 +353,7 @@ pub struct MonsterBuilder<'a: 'b, 'b> { } impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> { #[inline] - pub fn add_pos(&mut self, pos: &'b Vec3) { + pub fn add_pos(&mut self, pos: &Vec3) { self.fbb_.push_slot_always::<&Vec3>(Monster::VT_POS, pos); } #[inline] @@ -379,6 +389,10 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> { 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 { @@ -404,9 +418,7 @@ impl<'a> flatbuffers::Follow<'a> for Weapon<'a> { type Inner = Weapon<'a>; #[inline] fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { - Self { - _tab: flatbuffers::Table { buf: buf, loc: loc }, - } + Self { _tab: flatbuffers::Table { buf, loc } } } } @@ -431,17 +443,17 @@ impl<'a> Weapon<'a> { pub const VT_DAMAGE: flatbuffers::VOffsetT = 6; #[inline] - pub fn name(&'a self) -> Option<&'a str> { + pub fn name(&self) -> Option<&'a str> { self._tab.get::>(Weapon::VT_NAME, None) } #[inline] - pub fn damage(&'a self) -> i16 { + pub fn damage(&self) -> i16 { self._tab.get::(Weapon::VT_DAMAGE, Some(0)).unwrap() } } pub struct WeaponArgs<'a> { - pub name: Option>, + pub name: Option>, pub damage: i16, } impl<'a> Default for WeaponArgs<'a> { diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 16c8dfa1e..4e157827e 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -1265,22 +1265,16 @@ class RustGenerator : public BaseGenerator { nested_root = parser_.LookupStruct(qualified_name); } FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser. - (void)nested_root; - code_.SetValue("OFFSET_NAME", - offset_prefix + "::" + GetFieldOffsetName(field)); + code_.SetValue("NESTED", WrapInNameSpace(*nested_root)); code_ += " pub fn {{FIELD_NAME}}_nested_flatbuffer(&'a self) -> " - " Option<{{STRUCT_NAME}}<'a>> {"; - code_ += " match self.{{FIELD_NAME}}() {"; - code_ += " None => { None }"; - code_ += " Some(data) => {"; - code_ += " use self::flatbuffers::Follow;"; - code_ += - " Some(>>::follow(data, 0))"; - code_ += " },"; - code_ += " }"; + "Option<{{NESTED}}<'a>> {"; + code_ += " self.{{FIELD_NAME}}().map(|data| {"; + code_ += " use flatbuffers::Follow;"; + code_ += " >>" + "::follow(data, 0)"; + code_ += " })"; code_ += " }"; } } diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index 094de8ca0..68ffbe080 100644 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -15,13 +15,13 @@ namespace MyGame.Example; attribute "priority"; /// Composite components of Monster color. -enum Color:ubyte (bit_flags) { +enum Color:ubyte (bit_flags) { Red = 0, // color Red = (1u << 0) /// \brief color Green /// Green is bit_flag with value (1u << 1) - Green, + Green, /// \brief color Blue (1u << 3) - Blue = 3, + Blue = 3, } enum Race:byte { diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index 4c5231e69..47afea1cf 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -1211,14 +1211,11 @@ impl<'a> Monster<'a> { 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> { - match self.testnestedflatbuffer() { - None => { None } - Some(data) => { - use self::flatbuffers::Follow; - Some(>>::follow(data, 0)) - }, - } + pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option> { + self.testnestedflatbuffer().map(|data| { + use flatbuffers::Follow; + >>::follow(data, 0) + }) } #[inline] pub fn testempty(&self) -> Option> {