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 <cneo@google.com>
This commit is contained in:
Casper
2020-10-10 11:49:02 -07:00
committed by GitHub
parent cb971eece8
commit 543c1bbeba
4 changed files with 79 additions and 76 deletions

View File

@@ -1,22 +1,25 @@
// automatically generated by the FlatBuffers compiler, do not modify // 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 { pub mod my_game {
#![allow(dead_code)]
#![allow(unused_imports)]
use std::mem; use std::mem;
use std::marker::PhantomData;
use std::cmp::Ordering; use std::cmp::Ordering;
extern crate flatbuffers; extern crate flatbuffers;
use self::flatbuffers::EndianScalar; use self::flatbuffers::EndianScalar;
#[allow(unused_imports, dead_code)]
pub mod sample { pub mod sample {
#![allow(dead_code)]
#![allow(unused_imports)]
use std::mem; use std::mem;
use std::marker::PhantomData;
use std::cmp::Ordering; use std::cmp::Ordering;
extern crate flatbuffers; extern crate flatbuffers;
@@ -24,15 +27,16 @@ pub mod sample {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[repr(i8)] #[repr(i8)]
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Color { pub enum Color {
Red = 0, Red = 0,
Green = 1, Green = 1,
Blue = 2 Blue = 2,
} }
const ENUM_MIN_COLOR: i8 = 0; pub const ENUM_MIN_COLOR: i8 = 0;
const ENUM_MAX_COLOR: i8 = 2; pub const ENUM_MAX_COLOR: i8 = 2;
impl<'a> flatbuffers::Follow<'a> for Color { impl<'a> flatbuffers::Follow<'a> for Color {
type Inner = Self; type Inner = Self;
@@ -66,34 +70,35 @@ impl flatbuffers::Push for Color {
} }
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
const ENUM_VALUES_COLOR:[Color; 3] = [ pub const ENUM_VALUES_COLOR: [Color; 3] = [
Color::Red, Color::Red,
Color::Green, Color::Green,
Color::Blue Color::Blue
]; ];
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
const ENUM_NAMES_COLOR:[&'static str; 3] = [ pub const ENUM_NAMES_COLOR: [&str; 3] = [
"Red", "Red",
"Green", "Green",
"Blue" "Blue"
]; ];
pub fn enum_name_color(e: Color) -> &'static str { pub fn enum_name_color(e: Color) -> &'static str {
let index: usize = e as usize; let index = e as i8;
ENUM_NAMES_COLOR[index] ENUM_NAMES_COLOR[index as usize]
} }
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[repr(u8)] #[repr(u8)]
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Equipment { pub enum Equipment {
NONE = 0, NONE = 0,
Weapon = 1 Weapon = 1,
} }
const ENUM_MIN_EQUIPMENT: u8 = 0; pub const ENUM_MIN_EQUIPMENT: u8 = 0;
const ENUM_MAX_EQUIPMENT: u8 = 1; pub const ENUM_MAX_EQUIPMENT: u8 = 1;
impl<'a> flatbuffers::Follow<'a> for Equipment { impl<'a> flatbuffers::Follow<'a> for Equipment {
type Inner = Self; type Inner = Self;
@@ -127,20 +132,20 @@ impl flatbuffers::Push for Equipment {
} }
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
const ENUM_VALUES_EQUIPMENT:[Equipment; 2] = [ pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [
Equipment::NONE, Equipment::NONE,
Equipment::Weapon Equipment::Weapon
]; ];
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
const ENUM_NAMES_EQUIPMENT:[&'static str; 2] = [ pub const ENUM_NAMES_EQUIPMENT: [&str; 2] = [
"NONE", "NONE",
"Weapon" "Weapon"
]; ];
pub fn enum_name_equipment(e: Equipment) -> &'static str { pub fn enum_name_equipment(e: Equipment) -> &'static str {
let index: usize = e as usize; let index = e as u8;
ENUM_NAMES_EQUIPMENT[index] ENUM_NAMES_EQUIPMENT[index as usize]
} }
pub struct EquipmentUnionTableOffset {} pub struct EquipmentUnionTableOffset {}
@@ -158,7 +163,6 @@ impl<'a> flatbuffers::Follow<'a> for Vec3 {
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
<&'a Vec3>::follow(buf, loc) <&'a Vec3>::follow(buf, loc)
//flatbuffers::follow_cast_ref::<Vec3>(buf, loc)
} }
} }
impl<'a> flatbuffers::Follow<'a> for &'a Vec3 { impl<'a> flatbuffers::Follow<'a> for &'a Vec3 {
@@ -192,7 +196,7 @@ impl<'b> flatbuffers::Push for &'b Vec3 {
impl 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 { Vec3 {
x_: _x.to_little_endian(), x_: _x.to_little_endian(),
y_: _y.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() self.x_.from_little_endian()
} }
pub fn y<'a>(&'a self) -> f32 { pub fn y(&self) -> f32 {
self.y_.from_little_endian() self.y_.from_little_endian()
} }
pub fn z<'a>(&'a self) -> f32 { pub fn z(&self) -> f32 {
self.z_.from_little_endian() self.z_.from_little_endian()
} }
} }
@@ -222,9 +226,7 @@ impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
type Inner = Monster<'a>; type Inner = Monster<'a>;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { Self { _tab: flatbuffers::Table { buf, loc } }
_tab: flatbuffers::Table { buf: buf, loc: loc },
}
} }
} }
@@ -240,6 +242,7 @@ impl<'a> Monster<'a> {
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
args: &'args MonsterArgs<'args>) -> flatbuffers::WIPOffset<Monster<'bldr>> { args: &'args MonsterArgs<'args>) -> flatbuffers::WIPOffset<Monster<'bldr>> {
let mut builder = MonsterBuilder::new(_fbb); 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.equipped { builder.add_equipped(x); }
if let Some(x) = args.weapons { builder.add_weapons(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.inventory { builder.add_inventory(x); }
@@ -261,48 +264,53 @@ impl<'a> Monster<'a> {
pub const VT_WEAPONS: flatbuffers::VOffsetT = 18; pub const VT_WEAPONS: flatbuffers::VOffsetT = 18;
pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20; pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20;
pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22; pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22;
pub const VT_PATH: flatbuffers::VOffsetT = 24;
#[inline] #[inline]
pub fn pos(&'a self) -> Option<&'a Vec3> { pub fn pos(&self) -> Option<&'a Vec3> {
self._tab.get::<Vec3>(Monster::VT_POS, None) self._tab.get::<Vec3>(Monster::VT_POS, None)
} }
#[inline] #[inline]
pub fn mana(&'a self) -> i16 { pub fn mana(&self) -> i16 {
self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap() self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap()
} }
#[inline] #[inline]
pub fn hp(&'a self) -> i16 { pub fn hp(&self) -> i16 {
self._tab.get::<i16>(Monster::VT_HP, Some(100)).unwrap() self._tab.get::<i16>(Monster::VT_HP, Some(100)).unwrap()
} }
#[inline] #[inline]
pub fn name(&'a self) -> Option<&'a str> { pub fn name(&self) -> Option<&'a str> {
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None) self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None)
} }
#[inline] #[inline]
pub fn inventory(&'a self) -> Option<&'a [u8]> { pub fn inventory(&self) -> Option<&'a [u8]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice()) self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice())
} }
#[inline] #[inline]
pub fn color(&'a self) -> Color { pub fn color(&self) -> Color {
self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap() self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap()
} }
#[inline] #[inline]
pub fn weapons(&'a self) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>> { pub fn weapons(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>>>(Monster::VT_WEAPONS, None) self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>>>(Monster::VT_WEAPONS, None)
} }
#[inline] #[inline]
pub fn equipped_type(&'a self) -> Equipment { pub fn equipped_type(&self) -> Equipment {
self._tab.get::<Equipment>(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap() self._tab.get::<Equipment>(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap()
} }
#[inline] #[inline]
pub fn equipped(&'a self) -> Option<flatbuffers::Table<'a>> { pub fn equipped(&self) -> Option<flatbuffers::Table<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Monster::VT_EQUIPPED, None) self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Monster::VT_EQUIPPED, None)
} }
#[inline] #[inline]
pub fn path(&self) -> Option<&'a [Vec3]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<Vec3>>>(Monster::VT_PATH, None).map(|v| v.safe_slice() )
}
#[inline]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn equipped_as_weapon(&'a self) -> Option<Weapon> { pub fn equipped_as_weapon(&self) -> Option<Weapon<'a>> {
if self.equipped_type() == Equipment::Weapon { if self.equipped_type() == Equipment::Weapon {
self.equipped().map(|u| Weapon::init_from_table(u)) self.equipped().map(Weapon::init_from_table)
} else { } else {
None None
} }
@@ -311,15 +319,16 @@ impl<'a> Monster<'a> {
} }
pub struct MonsterArgs<'a> { pub struct MonsterArgs<'a> {
pub pos: Option<&'a Vec3>, pub pos: Option<&'a Vec3>,
pub mana: i16, pub mana: i16,
pub hp: i16, pub hp: i16,
pub name: Option<flatbuffers::WIPOffset<&'a str>>, pub name: Option<flatbuffers::WIPOffset<&'a str>>,
pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , u8>>>, pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, u8>>>,
pub color: Color, pub color: Color,
pub weapons: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<Weapon<'a >>>>>, pub weapons: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>>>,
pub equipped_type: Equipment, pub equipped_type: Equipment,
pub equipped: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>, pub equipped: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
pub path: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, Vec3>>>,
} }
impl<'a> Default for MonsterArgs<'a> { impl<'a> Default for MonsterArgs<'a> {
#[inline] #[inline]
@@ -334,6 +343,7 @@ impl<'a> Default for MonsterArgs<'a> {
weapons: None, weapons: None,
equipped_type: Equipment::NONE, equipped_type: Equipment::NONE,
equipped: None, equipped: None,
path: None,
} }
} }
} }
@@ -343,7 +353,7 @@ pub struct MonsterBuilder<'a: 'b, 'b> {
} }
impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> { impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
#[inline] #[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); self.fbb_.push_slot_always::<&Vec3>(Monster::VT_POS, pos);
} }
#[inline] #[inline]
@@ -379,6 +389,10 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped); self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped);
} }
#[inline] #[inline]
pub fn add_path(&mut self, path: flatbuffers::WIPOffset<flatbuffers::Vector<'b , Vec3>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_PATH, path);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> { pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
let start = _fbb.start_table(); let start = _fbb.start_table();
MonsterBuilder { MonsterBuilder {
@@ -404,9 +418,7 @@ impl<'a> flatbuffers::Follow<'a> for Weapon<'a> {
type Inner = Weapon<'a>; type Inner = Weapon<'a>;
#[inline] #[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
Self { Self { _tab: flatbuffers::Table { buf, loc } }
_tab: flatbuffers::Table { buf: buf, loc: loc },
}
} }
} }
@@ -431,17 +443,17 @@ impl<'a> Weapon<'a> {
pub const VT_DAMAGE: flatbuffers::VOffsetT = 6; pub const VT_DAMAGE: flatbuffers::VOffsetT = 6;
#[inline] #[inline]
pub fn name(&'a self) -> Option<&'a str> { pub fn name(&self) -> Option<&'a str> {
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Weapon::VT_NAME, None) self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Weapon::VT_NAME, None)
} }
#[inline] #[inline]
pub fn damage(&'a self) -> i16 { pub fn damage(&self) -> i16 {
self._tab.get::<i16>(Weapon::VT_DAMAGE, Some(0)).unwrap() self._tab.get::<i16>(Weapon::VT_DAMAGE, Some(0)).unwrap()
} }
} }
pub struct WeaponArgs<'a> { pub struct WeaponArgs<'a> {
pub name: Option<flatbuffers::WIPOffset<&'a str>>, pub name: Option<flatbuffers::WIPOffset<&'a str>>,
pub damage: i16, pub damage: i16,
} }
impl<'a> Default for WeaponArgs<'a> { impl<'a> Default for WeaponArgs<'a> {

View File

@@ -1265,22 +1265,16 @@ class RustGenerator : public BaseGenerator {
nested_root = parser_.LookupStruct(qualified_name); nested_root = parser_.LookupStruct(qualified_name);
} }
FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser. FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.
(void)nested_root;
code_.SetValue("OFFSET_NAME", code_.SetValue("NESTED", WrapInNameSpace(*nested_root));
offset_prefix + "::" + GetFieldOffsetName(field));
code_ += code_ +=
" pub fn {{FIELD_NAME}}_nested_flatbuffer(&'a self) -> " " pub fn {{FIELD_NAME}}_nested_flatbuffer(&'a self) -> "
" Option<{{STRUCT_NAME}}<'a>> {"; "Option<{{NESTED}}<'a>> {";
code_ += " match self.{{FIELD_NAME}}() {"; code_ += " self.{{FIELD_NAME}}().map(|data| {";
code_ += " None => { None }"; code_ += " use flatbuffers::Follow;";
code_ += " Some(data) => {"; code_ += " <flatbuffers::ForwardsUOffset<{{NESTED}}<'a>>>"
code_ += " use self::flatbuffers::Follow;"; "::follow(data, 0)";
code_ += code_ += " })";
" Some(<flatbuffers::ForwardsUOffset"
"<{{STRUCT_NAME}}<'a>>>::follow(data, 0))";
code_ += " },";
code_ += " }";
code_ += " }"; code_ += " }";
} }
} }

View File

@@ -1211,14 +1211,11 @@ impl<'a> Monster<'a> {
pub fn testnestedflatbuffer(&self) -> Option<&'a [u8]> { pub fn testnestedflatbuffer(&self) -> Option<&'a [u8]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_TESTNESTEDFLATBUFFER, None).map(|v| v.safe_slice()) self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_TESTNESTEDFLATBUFFER, None).map(|v| v.safe_slice())
} }
pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option<Monster<'a>> { pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option<Monster<'a>> {
match self.testnestedflatbuffer() { self.testnestedflatbuffer().map(|data| {
None => { None } use flatbuffers::Follow;
Some(data) => { <flatbuffers::ForwardsUOffset<Monster<'a>>>::follow(data, 0)
use self::flatbuffers::Follow; })
Some(<flatbuffers::ForwardsUOffset<Monster<'a>>>::follow(data, 0))
},
}
} }
#[inline] #[inline]
pub fn testempty(&self) -> Option<Stat<'a>> { pub fn testempty(&self) -> Option<Stat<'a>> {