mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 12:28:06 +00:00
Rust structz (#6539)
* Rust structz * struct of structs test * swift tmp variables Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -967,8 +967,13 @@ impl AnyAmbiguousAliasesT {
|
||||
}
|
||||
// struct Test, aligned to 2
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, PartialEq, Default)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct Test(pub [u8; 4]);
|
||||
impl Default for Test {
|
||||
fn default() -> Self {
|
||||
Self([0; 4])
|
||||
}
|
||||
}
|
||||
impl std::fmt::Debug for Test {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("Test")
|
||||
@@ -1111,8 +1116,13 @@ impl TestT {
|
||||
|
||||
// struct Vec3, aligned to 8
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, PartialEq, Default)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct Vec3(pub [u8; 32]);
|
||||
impl Default for Vec3 {
|
||||
fn default() -> Self {
|
||||
Self([0; 32])
|
||||
}
|
||||
}
|
||||
impl std::fmt::Debug for Vec3 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("Vec3")
|
||||
@@ -1356,8 +1366,13 @@ impl Vec3T {
|
||||
|
||||
// struct Ability, aligned to 4
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, PartialEq, Default)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct Ability(pub [u8; 8]);
|
||||
impl Default for Ability {
|
||||
fn default() -> Self {
|
||||
Self([0; 8])
|
||||
}
|
||||
}
|
||||
impl std::fmt::Debug for Ability {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("Ability")
|
||||
@@ -1508,6 +1523,139 @@ impl AbilityT {
|
||||
}
|
||||
}
|
||||
|
||||
// struct StructOfStructs, aligned to 4
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct StructOfStructs(pub [u8; 20]);
|
||||
impl Default for StructOfStructs {
|
||||
fn default() -> Self {
|
||||
Self([0; 20])
|
||||
}
|
||||
}
|
||||
impl std::fmt::Debug for StructOfStructs {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("StructOfStructs")
|
||||
.field("a", &self.a())
|
||||
.field("b", &self.b())
|
||||
.field("c", &self.c())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::SimpleToVerifyInSlice for StructOfStructs {}
|
||||
impl flatbuffers::SafeSliceAccess for StructOfStructs {}
|
||||
impl<'a> flatbuffers::Follow<'a> for StructOfStructs {
|
||||
type Inner = &'a StructOfStructs;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
<&'a StructOfStructs>::follow(buf, loc)
|
||||
}
|
||||
}
|
||||
impl<'a> flatbuffers::Follow<'a> for &'a StructOfStructs {
|
||||
type Inner = &'a StructOfStructs;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
flatbuffers::follow_cast_ref::<StructOfStructs>(buf, loc)
|
||||
}
|
||||
}
|
||||
impl<'b> flatbuffers::Push for StructOfStructs {
|
||||
type Output = StructOfStructs;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
let src = unsafe {
|
||||
::std::slice::from_raw_parts(self as *const StructOfStructs as *const u8, Self::size())
|
||||
};
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
impl<'b> flatbuffers::Push for &'b StructOfStructs {
|
||||
type Output = StructOfStructs;
|
||||
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
let src = unsafe {
|
||||
::std::slice::from_raw_parts(*self as *const StructOfStructs as *const u8, Self::size())
|
||||
};
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> flatbuffers::Verifiable for StructOfStructs {
|
||||
#[inline]
|
||||
fn run_verifier(
|
||||
v: &mut flatbuffers::Verifier, pos: usize
|
||||
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
|
||||
use self::flatbuffers::Verifiable;
|
||||
v.in_buffer::<Self>(pos)
|
||||
}
|
||||
}
|
||||
impl StructOfStructs {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
a: &Ability,
|
||||
b: &Test,
|
||||
c: &Ability,
|
||||
) -> Self {
|
||||
let mut s = Self([0; 20]);
|
||||
s.set_a(&a);
|
||||
s.set_b(&b);
|
||||
s.set_c(&c);
|
||||
s
|
||||
}
|
||||
|
||||
pub const fn get_fully_qualified_name() -> &'static str {
|
||||
"MyGame.Example.StructOfStructs"
|
||||
}
|
||||
|
||||
pub fn a(&self) -> &Ability {
|
||||
unsafe { &*(self.0[0..].as_ptr() as *const Ability) }
|
||||
}
|
||||
|
||||
pub fn set_a(&mut self, x: &Ability) {
|
||||
self.0[0..0+8].copy_from_slice(&x.0)
|
||||
}
|
||||
|
||||
pub fn b(&self) -> &Test {
|
||||
unsafe { &*(self.0[8..].as_ptr() as *const Test) }
|
||||
}
|
||||
|
||||
pub fn set_b(&mut self, x: &Test) {
|
||||
self.0[8..8+4].copy_from_slice(&x.0)
|
||||
}
|
||||
|
||||
pub fn c(&self) -> &Ability {
|
||||
unsafe { &*(self.0[12..].as_ptr() as *const Ability) }
|
||||
}
|
||||
|
||||
pub fn set_c(&mut self, x: &Ability) {
|
||||
self.0[12..12+8].copy_from_slice(&x.0)
|
||||
}
|
||||
|
||||
pub fn unpack(&self) -> StructOfStructsT {
|
||||
StructOfStructsT {
|
||||
a: self.a().unpack(),
|
||||
b: self.b().unpack(),
|
||||
c: self.c().unpack(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct StructOfStructsT {
|
||||
pub a: AbilityT,
|
||||
pub b: TestT,
|
||||
pub c: AbilityT,
|
||||
}
|
||||
impl StructOfStructsT {
|
||||
pub fn pack(&self) -> StructOfStructs {
|
||||
StructOfStructs::new(
|
||||
&self.a.pack(),
|
||||
&self.b.pack(),
|
||||
&self.c.pack(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TestSimpleTableWithEnumOffset {}
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user