mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 13:53:38 +00:00
Rust Object API (#6070)
* inital commit of rust object api * Required fields support. * clang fallthrough * Fix unused variables * just don't fall through * remove comment * s/panic/unreachable * Tests for object API * Added defaults * deleted unintentionally added files and updated .bat file * fix bat file * clang format * Cargo clippy checks * remove commented out code * clippy allows * Remove matches! macro since we're not yet at Rust v1.42 * install clippy in RustTest.sh * move line Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -38,7 +38,7 @@ pub const ENUM_VALUES_COLOR: [Color; 3] = [
|
||||
Color::Blue,
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
||||
#[repr(transparent)]
|
||||
pub struct Color(pub i8);
|
||||
#[allow(non_upper_case_globals)]
|
||||
@@ -125,7 +125,7 @@ pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [
|
||||
Equipment::Weapon,
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
||||
#[repr(transparent)]
|
||||
pub struct Equipment(pub u8);
|
||||
#[allow(non_upper_case_globals)]
|
||||
@@ -157,7 +157,6 @@ impl std::fmt::Debug for Equipment {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct EquipmentUnionTableOffset {}
|
||||
impl<'a> flatbuffers::Follow<'a> for Equipment {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
@@ -199,9 +198,57 @@ impl<'a> flatbuffers::Verifiable for Equipment {
|
||||
}
|
||||
|
||||
impl flatbuffers::SimpleToVerifyInSlice for Equipment {}
|
||||
pub struct EquipmentUnionTableOffset {}
|
||||
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum EquipmentT {
|
||||
NONE,
|
||||
Weapon(Box<WeaponT>),
|
||||
}
|
||||
impl Default for EquipmentT {
|
||||
fn default() -> Self {
|
||||
Self::NONE
|
||||
}
|
||||
}
|
||||
impl EquipmentT {
|
||||
fn equipment_type(&self) -> Equipment {
|
||||
match self {
|
||||
Self::NONE => Equipment::NONE,
|
||||
Self::Weapon(_) => Equipment::Weapon,
|
||||
}
|
||||
}
|
||||
pub fn pack(&self, fbb: &mut flatbuffers::FlatBufferBuilder) -> Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>> {
|
||||
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<Box<WeaponT>> {
|
||||
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)]
|
||||
#[derive(Clone, Copy, PartialEq, Default)]
|
||||
pub struct Vec3(pub [u8; 12]);
|
||||
impl std::fmt::Debug for Vec3 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
@@ -343,6 +390,29 @@ impl Vec3 {
|
||||
}
|
||||
}
|
||||
|
||||
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 {}
|
||||
@@ -363,9 +433,7 @@ impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
|
||||
impl<'a> Monster<'a> {
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
Monster {
|
||||
_tab: table,
|
||||
}
|
||||
Monster { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
@@ -385,6 +453,46 @@ impl<'a> Monster<'a> {
|
||||
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;
|
||||
@@ -590,6 +698,57 @@ impl std::fmt::Debug for Monster<'_> {
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct MonsterT {
|
||||
pub pos: Option<Vec3T>,
|
||||
pub mana: i16,
|
||||
pub hp: i16,
|
||||
pub name: Option<String>,
|
||||
pub inventory: Option<Vec<u8>>,
|
||||
pub color: Color,
|
||||
pub weapons: Option<Vec<WeaponT>>,
|
||||
pub equipped: EquipmentT,
|
||||
pub path: Option<Vec<Vec3T>>,
|
||||
}
|
||||
impl MonsterT {
|
||||
pub fn pack<'b>(
|
||||
&self,
|
||||
_fbb: &mut flatbuffers::FlatBufferBuilder<'b>
|
||||
) -> flatbuffers::WIPOffset<Monster<'b>> {
|
||||
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)]
|
||||
|
||||
@@ -608,9 +767,7 @@ impl<'a> flatbuffers::Follow<'a> for Weapon<'a> {
|
||||
impl<'a> Weapon<'a> {
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
Weapon {
|
||||
_tab: table,
|
||||
}
|
||||
Weapon { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
|
||||
@@ -622,6 +779,16 @@ impl<'a> Weapon<'a> {
|
||||
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;
|
||||
|
||||
@@ -697,6 +864,27 @@ impl std::fmt::Debug for Weapon<'_> {
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct WeaponT {
|
||||
pub name: Option<String>,
|
||||
pub damage: i16,
|
||||
}
|
||||
impl WeaponT {
|
||||
pub fn pack<'b>(
|
||||
&self,
|
||||
_fbb: &mut flatbuffers::FlatBufferBuilder<'b>
|
||||
) -> flatbuffers::WIPOffset<Weapon<'b>> {
|
||||
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> {
|
||||
|
||||
@@ -19,6 +19,7 @@ extern crate flatbuffers;
|
||||
|
||||
// import the generated code
|
||||
#[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,
|
||||
Monster, MonsterArgs,
|
||||
@@ -27,7 +28,7 @@ pub use monster_generated::my_game::sample::{Color, Equipment,
|
||||
|
||||
|
||||
// Example how to use FlatBuffers to create and read binary buffers.
|
||||
|
||||
#[allow(clippy::float_cmp)]
|
||||
fn main() {
|
||||
// Build up a serialized buffer algorithmically.
|
||||
// Initialize it with a capacity of 1024 bytes.
|
||||
|
||||
@@ -31,6 +31,7 @@ use flexbuffers::{BitWidth, Builder, Reader, ReaderError};
|
||||
// {"damage": 15, "name": "great axe"},
|
||||
// {"damage": 5, "name": "hammer"}]
|
||||
// }
|
||||
#[allow(clippy::float_cmp)]
|
||||
fn main() {
|
||||
// Create a new Flexbuffer builder.
|
||||
let mut builder = Builder::default();
|
||||
|
||||
Reference in New Issue
Block a user