mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 05:02:39 +00:00
Implement Debug trait for Rust flatbuffers. (#6207)
* Refactor idl_gen_rust to a ForAllX continuation pattern. * Removed unneeded SetValue and updated sample rust gencode * Make Rust flatbuffers print right * Generated code and removed unnecessary trait constraint * bumped rust version. Release required * removed an unwrap in Rust Debug-print unions * Tested formatting flatbuffers in rust. * Set float precision in flaky debug-print test * impl Debug for structs too Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -173,12 +173,22 @@ impl flatbuffers::EndianScalar for Equipment {
|
||||
pub struct EquipmentUnionTableOffset {}
|
||||
// struct Vec3, aligned to 4
|
||||
#[repr(C, align(4))]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct Vec3 {
|
||||
x_: f32,
|
||||
y_: f32,
|
||||
z_: f32,
|
||||
} // pub struct Vec3
|
||||
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::SafeSliceAccess for Vec3 {}
|
||||
impl<'a> flatbuffers::Follow<'a> for Vec3 {
|
||||
type Inner = &'a Vec3;
|
||||
@@ -238,7 +248,7 @@ impl Vec3 {
|
||||
}
|
||||
|
||||
pub enum MonsterOffset {}
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct Monster<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
@@ -429,8 +439,36 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
pub enum WeaponOffset {}
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
pub struct Weapon<'a> {
|
||||
pub _tab: flatbuffers::Table<'a>,
|
||||
@@ -515,6 +553,14 @@ impl<'a: 'b, 'b> WeaponBuilder<'a, 'b> {
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> {
|
||||
flatbuffers::get_root::<Monster<'a>>(buf)
|
||||
|
||||
@@ -152,6 +152,7 @@ fn main() {
|
||||
//assert_eq!(monster.path().unwrap()[1].x(), 4.0);
|
||||
|
||||
println!("The FlatBuffer was successfully created and accessed!");
|
||||
dbg!(monster);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user