mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
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:
@@ -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::<Vec3>(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<Monster<'bldr>> {
|
||||
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::<Vec3>(Monster::VT_POS, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn mana(&'a self) -> i16 {
|
||||
pub fn mana(&self) -> i16 {
|
||||
self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap()
|
||||
}
|
||||
#[inline]
|
||||
pub fn hp(&'a self) -> i16 {
|
||||
pub fn hp(&self) -> i16 {
|
||||
self._tab.get::<i16>(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::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None)
|
||||
}
|
||||
#[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())
|
||||
}
|
||||
#[inline]
|
||||
pub fn color(&'a self) -> Color {
|
||||
pub fn color(&self) -> Color {
|
||||
self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap()
|
||||
}
|
||||
#[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)
|
||||
}
|
||||
#[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()
|
||||
}
|
||||
#[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)
|
||||
}
|
||||
#[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)]
|
||||
pub fn equipped_as_weapon(&'a self) -> Option<Weapon> {
|
||||
pub fn equipped_as_weapon(&self) -> Option<Weapon<'a>> {
|
||||
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<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , u8>>>,
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, u8>>>,
|
||||
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: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
|
||||
pub path: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, Vec3>>>,
|
||||
}
|
||||
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::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped);
|
||||
}
|
||||
#[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> {
|
||||
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::<flatbuffers::ForwardsUOffset<&str>>(Weapon::VT_NAME, None)
|
||||
}
|
||||
#[inline]
|
||||
pub fn damage(&'a self) -> i16 {
|
||||
pub fn damage(&self) -> i16 {
|
||||
self._tab.get::<i16>(Weapon::VT_DAMAGE, Some(0)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WeaponArgs<'a> {
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub name: Option<flatbuffers::WIPOffset<&'a str>>,
|
||||
pub damage: i16,
|
||||
}
|
||||
impl<'a> Default for WeaponArgs<'a> {
|
||||
|
||||
@@ -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(<flatbuffers::ForwardsUOffset"
|
||||
"<{{STRUCT_NAME}}<'a>>>::follow(data, 0))";
|
||||
code_ += " },";
|
||||
code_ += " }";
|
||||
"Option<{{NESTED}}<'a>> {";
|
||||
code_ += " self.{{FIELD_NAME}}().map(|data| {";
|
||||
code_ += " use flatbuffers::Follow;";
|
||||
code_ += " <flatbuffers::ForwardsUOffset<{{NESTED}}<'a>>>"
|
||||
"::follow(data, 0)";
|
||||
code_ += " })";
|
||||
code_ += " }";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1211,14 +1211,11 @@ impl<'a> Monster<'a> {
|
||||
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())
|
||||
}
|
||||
pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option<Monster<'a>> {
|
||||
match self.testnestedflatbuffer() {
|
||||
None => { None }
|
||||
Some(data) => {
|
||||
use self::flatbuffers::Follow;
|
||||
Some(<flatbuffers::ForwardsUOffset<Monster<'a>>>::follow(data, 0))
|
||||
},
|
||||
}
|
||||
pub fn testnestedflatbuffer_nested_flatbuffer(&'a self) -> Option<Monster<'a>> {
|
||||
self.testnestedflatbuffer().map(|data| {
|
||||
use flatbuffers::Follow;
|
||||
<flatbuffers::ForwardsUOffset<Monster<'a>>>::follow(data, 0)
|
||||
})
|
||||
}
|
||||
#[inline]
|
||||
pub fn testempty(&self) -> Option<Stat<'a>> {
|
||||
|
||||
Reference in New Issue
Block a user