xref: /aosp_15_r20/external/flatbuffers/samples/rust_generated/my_game/sample/monster_generated.rs (revision 890232f25432b36107d06881e0a25aaa6b473652)
1 // automatically generated by the FlatBuffers compiler, do not modify
2 // @generated
3 extern crate alloc;
4 extern crate flatbuffers;
5 use alloc::boxed::Box;
6 use alloc::string::{String, ToString};
7 use alloc::vec::Vec;
8 use core::mem;
9 use core::cmp::Ordering;
10 use self::flatbuffers::{EndianScalar, Follow};
11 use super::*;
12 pub enum MonsterOffset {}
13 #[derive(Copy, Clone, PartialEq)]
14 
15 pub struct Monster<'a> {
16   pub _tab: flatbuffers::Table<'a>,
17 }
18 
19 impl<'a> flatbuffers::Follow<'a> for Monster<'a> {
20   type Inner = Monster<'a>;
21   #[inline]
follow(buf: &'a [u8], loc: usize) -> Self::Inner22   fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
23     Self { _tab: flatbuffers::Table { buf, loc } }
24   }
25 }
26 
27 impl<'a> Monster<'a> {
28   pub const VT_POS: flatbuffers::VOffsetT = 4;
29   pub const VT_MANA: flatbuffers::VOffsetT = 6;
30   pub const VT_HP: flatbuffers::VOffsetT = 8;
31   pub const VT_NAME: flatbuffers::VOffsetT = 10;
32   pub const VT_INVENTORY: flatbuffers::VOffsetT = 14;
33   pub const VT_COLOR: flatbuffers::VOffsetT = 16;
34   pub const VT_WEAPONS: flatbuffers::VOffsetT = 18;
35   pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20;
36   pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22;
37   pub const VT_PATH: flatbuffers::VOffsetT = 24;
38 
get_fully_qualified_name() -> &'static str39   pub const fn get_fully_qualified_name() -> &'static str {
40     "MyGame.Sample.Monster"
41   }
42 
43   #[inline]
init_from_table(table: flatbuffers::Table<'a>) -> Self44   pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
45     Monster { _tab: table }
46   }
47   #[allow(unused_mut)]
create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, args: &'args MonsterArgs<'args> ) -> flatbuffers::WIPOffset<Monster<'bldr>>48   pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
49     _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
50     args: &'args MonsterArgs<'args>
51   ) -> flatbuffers::WIPOffset<Monster<'bldr>> {
52     let mut builder = MonsterBuilder::new(_fbb);
53     if let Some(x) = args.path { builder.add_path(x); }
54     if let Some(x) = args.equipped { builder.add_equipped(x); }
55     if let Some(x) = args.weapons { builder.add_weapons(x); }
56     if let Some(x) = args.inventory { builder.add_inventory(x); }
57     if let Some(x) = args.name { builder.add_name(x); }
58     if let Some(x) = args.pos { builder.add_pos(x); }
59     builder.add_hp(args.hp);
60     builder.add_mana(args.mana);
61     builder.add_equipped_type(args.equipped_type);
62     builder.add_color(args.color);
63     builder.finish()
64   }
65 
unpack(&self) -> MonsterT66   pub fn unpack(&self) -> MonsterT {
67     let pos = self.pos().map(|x| {
68       x.unpack()
69     });
70     let mana = self.mana();
71     let hp = self.hp();
72     let name = self.name().map(|x| {
73       x.to_string()
74     });
75     let inventory = self.inventory().map(|x| {
76       x.to_vec()
77     });
78     let color = self.color();
79     let weapons = self.weapons().map(|x| {
80       x.iter().map(|t| t.unpack()).collect()
81     });
82     let equipped = match self.equipped_type() {
83       Equipment::NONE => EquipmentT::NONE,
84       Equipment::Weapon => EquipmentT::Weapon(Box::new(
85         self.equipped_as_weapon()
86             .expect("Invalid union table, expected `Equipment::Weapon`.")
87             .unpack()
88       )),
89       _ => EquipmentT::NONE,
90     };
91     let path = self.path().map(|x| {
92       x.iter().map(|t| t.unpack()).collect()
93     });
94     MonsterT {
95       pos,
96       mana,
97       hp,
98       name,
99       inventory,
100       color,
101       weapons,
102       equipped,
103       path,
104     }
105   }
106 
107   #[inline]
pos(&self) -> Option<&'a Vec3>108   pub fn pos(&self) -> Option<&'a Vec3> {
109     self._tab.get::<Vec3>(Monster::VT_POS, None)
110   }
111   #[inline]
mana(&self) -> i16112   pub fn mana(&self) -> i16 {
113     self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap()
114   }
115   #[inline]
hp(&self) -> i16116   pub fn hp(&self) -> i16 {
117     self._tab.get::<i16>(Monster::VT_HP, Some(100)).unwrap()
118   }
119   #[inline]
name(&self) -> Option<&'a str>120   pub fn name(&self) -> Option<&'a str> {
121     self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None)
122   }
123   #[inline]
inventory(&self) -> Option<&'a [u8]>124   pub fn inventory(&self) -> Option<&'a [u8]> {
125     self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice())
126   }
127   #[inline]
color(&self) -> Color128   pub fn color(&self) -> Color {
129     self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap()
130   }
131   #[inline]
weapons(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>>132   pub fn weapons(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>> {
133     self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon>>>>(Monster::VT_WEAPONS, None)
134   }
135   #[inline]
equipped_type(&self) -> Equipment136   pub fn equipped_type(&self) -> Equipment {
137     self._tab.get::<Equipment>(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap()
138   }
139   #[inline]
equipped(&self) -> Option<flatbuffers::Table<'a>>140   pub fn equipped(&self) -> Option<flatbuffers::Table<'a>> {
141     self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Monster::VT_EQUIPPED, None)
142   }
143   #[inline]
path(&self) -> Option<&'a [Vec3]>144   pub fn path(&self) -> Option<&'a [Vec3]> {
145     self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, Vec3>>>(Monster::VT_PATH, None).map(|v| v.safe_slice())
146   }
147   #[inline]
148   #[allow(non_snake_case)]
equipped_as_weapon(&self) -> Option<Weapon<'a>>149   pub fn equipped_as_weapon(&self) -> Option<Weapon<'a>> {
150     if self.equipped_type() == Equipment::Weapon {
151       self.equipped().map(Weapon::init_from_table)
152     } else {
153       None
154     }
155   }
156 
157 }
158 
159 impl flatbuffers::Verifiable for Monster<'_> {
160   #[inline]
run_verifier( v: &mut flatbuffers::Verifier, pos: usize ) -> Result<(), flatbuffers::InvalidFlatbuffer>161   fn run_verifier(
162     v: &mut flatbuffers::Verifier, pos: usize
163   ) -> Result<(), flatbuffers::InvalidFlatbuffer> {
164     use self::flatbuffers::Verifiable;
165     v.visit_table(pos)?
166      .visit_field::<Vec3>("pos", Self::VT_POS, false)?
167      .visit_field::<i16>("mana", Self::VT_MANA, false)?
168      .visit_field::<i16>("hp", Self::VT_HP, false)?
169      .visit_field::<flatbuffers::ForwardsUOffset<&str>>("name", Self::VT_NAME, false)?
170      .visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, u8>>>("inventory", Self::VT_INVENTORY, false)?
171      .visit_field::<Color>("color", Self::VT_COLOR, false)?
172      .visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<Weapon>>>>("weapons", Self::VT_WEAPONS, false)?
173      .visit_union::<Equipment, _>("equipped_type", Self::VT_EQUIPPED_TYPE, "equipped", Self::VT_EQUIPPED, false, |key, v, pos| {
174         match key {
175           Equipment::Weapon => v.verify_union_variant::<flatbuffers::ForwardsUOffset<Weapon>>("Equipment::Weapon", pos),
176           _ => Ok(()),
177         }
178      })?
179      .visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, Vec3>>>("path", Self::VT_PATH, false)?
180      .finish();
181     Ok(())
182   }
183 }
184 pub struct MonsterArgs<'a> {
185     pub pos: Option<&'a Vec3>,
186     pub mana: i16,
187     pub hp: i16,
188     pub name: Option<flatbuffers::WIPOffset<&'a str>>,
189     pub inventory: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, u8>>>,
190     pub color: Color,
191     pub weapons: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>>>,
192     pub equipped_type: Equipment,
193     pub equipped: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
194     pub path: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, Vec3>>>,
195 }
196 impl<'a> Default for MonsterArgs<'a> {
197   #[inline]
default() -> Self198   fn default() -> Self {
199     MonsterArgs {
200       pos: None,
201       mana: 150,
202       hp: 100,
203       name: None,
204       inventory: None,
205       color: Color::Blue,
206       weapons: None,
207       equipped_type: Equipment::NONE,
208       equipped: None,
209       path: None,
210     }
211   }
212 }
213 
214 pub struct MonsterBuilder<'a: 'b, 'b> {
215   fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
216   start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
217 }
218 impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
219   #[inline]
add_pos(&mut self, pos: &Vec3)220   pub fn add_pos(&mut self, pos: &Vec3) {
221     self.fbb_.push_slot_always::<&Vec3>(Monster::VT_POS, pos);
222   }
223   #[inline]
add_mana(&mut self, mana: i16)224   pub fn add_mana(&mut self, mana: i16) {
225     self.fbb_.push_slot::<i16>(Monster::VT_MANA, mana, 150);
226   }
227   #[inline]
add_hp(&mut self, hp: i16)228   pub fn add_hp(&mut self, hp: i16) {
229     self.fbb_.push_slot::<i16>(Monster::VT_HP, hp, 100);
230   }
231   #[inline]
add_name(&mut self, name: flatbuffers::WIPOffset<&'b str>)232   pub fn add_name(&mut self, name: flatbuffers::WIPOffset<&'b  str>) {
233     self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_NAME, name);
234   }
235   #[inline]
add_inventory(&mut self, inventory: flatbuffers::WIPOffset<flatbuffers::Vector<'b , u8>>)236   pub fn add_inventory(&mut self, inventory: flatbuffers::WIPOffset<flatbuffers::Vector<'b , u8>>) {
237     self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_INVENTORY, inventory);
238   }
239   #[inline]
add_color(&mut self, color: Color)240   pub fn add_color(&mut self, color: Color) {
241     self.fbb_.push_slot::<Color>(Monster::VT_COLOR, color, Color::Blue);
242   }
243   #[inline]
add_weapons(&mut self, weapons: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<Weapon<'b >>>>)244   pub fn add_weapons(&mut self, weapons: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<Weapon<'b >>>>) {
245     self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_WEAPONS, weapons);
246   }
247   #[inline]
add_equipped_type(&mut self, equipped_type: Equipment)248   pub fn add_equipped_type(&mut self, equipped_type: Equipment) {
249     self.fbb_.push_slot::<Equipment>(Monster::VT_EQUIPPED_TYPE, equipped_type, Equipment::NONE);
250   }
251   #[inline]
add_equipped(&mut self, equipped: flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>)252   pub fn add_equipped(&mut self, equipped: flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>) {
253     self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped);
254   }
255   #[inline]
add_path(&mut self, path: flatbuffers::WIPOffset<flatbuffers::Vector<'b , Vec3>>)256   pub fn add_path(&mut self, path: flatbuffers::WIPOffset<flatbuffers::Vector<'b , Vec3>>) {
257     self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_PATH, path);
258   }
259   #[inline]
new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b>260   pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
261     let start = _fbb.start_table();
262     MonsterBuilder {
263       fbb_: _fbb,
264       start_: start,
265     }
266   }
267   #[inline]
finish(self) -> flatbuffers::WIPOffset<Monster<'a>>268   pub fn finish(self) -> flatbuffers::WIPOffset<Monster<'a>> {
269     let o = self.fbb_.end_table(self.start_);
270     flatbuffers::WIPOffset::new(o.value())
271   }
272 }
273 
274 impl core::fmt::Debug for Monster<'_> {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result275   fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
276     let mut ds = f.debug_struct("Monster");
277       ds.field("pos", &self.pos());
278       ds.field("mana", &self.mana());
279       ds.field("hp", &self.hp());
280       ds.field("name", &self.name());
281       ds.field("inventory", &self.inventory());
282       ds.field("color", &self.color());
283       ds.field("weapons", &self.weapons());
284       ds.field("equipped_type", &self.equipped_type());
285       match self.equipped_type() {
286         Equipment::Weapon => {
287           if let Some(x) = self.equipped_as_weapon() {
288             ds.field("equipped", &x)
289           } else {
290             ds.field("equipped", &"InvalidFlatbuffer: Union discriminant does not match value.")
291           }
292         },
293         _ => {
294           let x: Option<()> = None;
295           ds.field("equipped", &x)
296         },
297       };
298       ds.field("path", &self.path());
299       ds.finish()
300   }
301 }
302 #[non_exhaustive]
303 #[derive(Debug, Clone, PartialEq)]
304 pub struct MonsterT {
305   pub pos: Option<Vec3T>,
306   pub mana: i16,
307   pub hp: i16,
308   pub name: Option<String>,
309   pub inventory: Option<Vec<u8>>,
310   pub color: Color,
311   pub weapons: Option<Vec<WeaponT>>,
312   pub equipped: EquipmentT,
313   pub path: Option<Vec<Vec3T>>,
314 }
315 impl Default for MonsterT {
default() -> Self316   fn default() -> Self {
317     Self {
318       pos: None,
319       mana: 150,
320       hp: 100,
321       name: None,
322       inventory: None,
323       color: Color::Blue,
324       weapons: None,
325       equipped: EquipmentT::NONE,
326       path: None,
327     }
328   }
329 }
330 impl MonsterT {
pack<'b>( &self, _fbb: &mut flatbuffers::FlatBufferBuilder<'b> ) -> flatbuffers::WIPOffset<Monster<'b>>331   pub fn pack<'b>(
332     &self,
333     _fbb: &mut flatbuffers::FlatBufferBuilder<'b>
334   ) -> flatbuffers::WIPOffset<Monster<'b>> {
335     let pos_tmp = self.pos.as_ref().map(|x| x.pack());
336     let pos = pos_tmp.as_ref();
337     let mana = self.mana;
338     let hp = self.hp;
339     let name = self.name.as_ref().map(|x|{
340       _fbb.create_string(x)
341     });
342     let inventory = self.inventory.as_ref().map(|x|{
343       _fbb.create_vector(x)
344     });
345     let color = self.color;
346     let weapons = self.weapons.as_ref().map(|x|{
347       let w: Vec<_> = x.iter().map(|t| t.pack(_fbb)).collect();_fbb.create_vector(&w)
348     });
349     let equipped_type = self.equipped.equipment_type();
350     let equipped = self.equipped.pack(_fbb);
351     let path = self.path.as_ref().map(|x|{
352       let w: Vec<_> = x.iter().map(|t| t.pack()).collect();_fbb.create_vector(&w)
353     });
354     Monster::create(_fbb, &MonsterArgs{
355       pos,
356       mana,
357       hp,
358       name,
359       inventory,
360       color,
361       weapons,
362       equipped_type,
363       equipped,
364       path,
365     })
366   }
367 }
368 #[inline]
369 #[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")]
get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a>370 pub fn get_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> {
371   unsafe { flatbuffers::root_unchecked::<Monster<'a>>(buf) }
372 }
373 
374 #[inline]
375 #[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")]
get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a>376 pub fn get_size_prefixed_root_as_monster<'a>(buf: &'a [u8]) -> Monster<'a> {
377   unsafe { flatbuffers::size_prefixed_root_unchecked::<Monster<'a>>(buf) }
378 }
379 
380 #[inline]
381 /// Verifies that a buffer of bytes contains a `Monster`
382 /// and returns it.
383 /// Note that verification is still experimental and may not
384 /// catch every error, or be maximally performant. For the
385 /// previous, unchecked, behavior use
386 /// `root_as_monster_unchecked`.
root_as_monster(buf: &[u8]) -> Result<Monster, flatbuffers::InvalidFlatbuffer>387 pub fn root_as_monster(buf: &[u8]) -> Result<Monster, flatbuffers::InvalidFlatbuffer> {
388   flatbuffers::root::<Monster>(buf)
389 }
390 #[inline]
391 /// Verifies that a buffer of bytes contains a size prefixed
392 /// `Monster` and returns it.
393 /// Note that verification is still experimental and may not
394 /// catch every error, or be maximally performant. For the
395 /// previous, unchecked, behavior use
396 /// `size_prefixed_root_as_monster_unchecked`.
size_prefixed_root_as_monster(buf: &[u8]) -> Result<Monster, flatbuffers::InvalidFlatbuffer>397 pub fn size_prefixed_root_as_monster(buf: &[u8]) -> Result<Monster, flatbuffers::InvalidFlatbuffer> {
398   flatbuffers::size_prefixed_root::<Monster>(buf)
399 }
400 #[inline]
401 /// Verifies, with the given options, that a buffer of bytes
402 /// contains a `Monster` and returns it.
403 /// Note that verification is still experimental and may not
404 /// catch every error, or be maximally performant. For the
405 /// previous, unchecked, behavior use
406 /// `root_as_monster_unchecked`.
root_as_monster_with_opts<'b, 'o>( opts: &'o flatbuffers::VerifierOptions, buf: &'b [u8], ) -> Result<Monster<'b>, flatbuffers::InvalidFlatbuffer>407 pub fn root_as_monster_with_opts<'b, 'o>(
408   opts: &'o flatbuffers::VerifierOptions,
409   buf: &'b [u8],
410 ) -> Result<Monster<'b>, flatbuffers::InvalidFlatbuffer> {
411   flatbuffers::root_with_opts::<Monster<'b>>(opts, buf)
412 }
413 #[inline]
414 /// Verifies, with the given verifier options, that a buffer of
415 /// bytes contains a size prefixed `Monster` and returns
416 /// it. Note that verification is still experimental and may not
417 /// catch every error, or be maximally performant. For the
418 /// previous, unchecked, behavior use
419 /// `root_as_monster_unchecked`.
size_prefixed_root_as_monster_with_opts<'b, 'o>( opts: &'o flatbuffers::VerifierOptions, buf: &'b [u8], ) -> Result<Monster<'b>, flatbuffers::InvalidFlatbuffer>420 pub fn size_prefixed_root_as_monster_with_opts<'b, 'o>(
421   opts: &'o flatbuffers::VerifierOptions,
422   buf: &'b [u8],
423 ) -> Result<Monster<'b>, flatbuffers::InvalidFlatbuffer> {
424   flatbuffers::size_prefixed_root_with_opts::<Monster<'b>>(opts, buf)
425 }
426 #[inline]
427 /// Assumes, without verification, that a buffer of bytes contains a Monster and returns it.
428 /// # Safety
429 /// Callers must trust the given bytes do indeed contain a valid `Monster`.
root_as_monster_unchecked(buf: &[u8]) -> Monster430 pub unsafe fn root_as_monster_unchecked(buf: &[u8]) -> Monster {
431   flatbuffers::root_unchecked::<Monster>(buf)
432 }
433 #[inline]
434 /// Assumes, without verification, that a buffer of bytes contains a size prefixed Monster and returns it.
435 /// # Safety
436 /// Callers must trust the given bytes do indeed contain a valid size prefixed `Monster`.
size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster437 pub unsafe fn size_prefixed_root_as_monster_unchecked(buf: &[u8]) -> Monster {
438   flatbuffers::size_prefixed_root_unchecked::<Monster>(buf)
439 }
440 #[inline]
finish_monster_buffer<'a, 'b>( fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<Monster<'a>>)441 pub fn finish_monster_buffer<'a, 'b>(
442     fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>,
443     root: flatbuffers::WIPOffset<Monster<'a>>) {
444   fbb.finish(root, None);
445 }
446 
447 #[inline]
finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<Monster<'a>>)448 pub fn finish_size_prefixed_monster_buffer<'a, 'b>(fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>, root: flatbuffers::WIPOffset<Monster<'a>>) {
449   fbb.finish_size_prefixed(root, None);
450 }
451