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:
Casper 2021-03-29 19:56:45 -04:00 committed by GitHub
parent 1c26d2a1a0
commit 4133a39df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 1681 additions and 564 deletions

View File

@ -247,8 +247,13 @@ impl EquipmentT {
}
// struct Vec3, aligned to 4
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default)]
#[derive(Clone, Copy, PartialEq)]
pub struct Vec3(pub [u8; 12]);
impl Default for Vec3 {
fn default() -> Self {
Self([0; 12])
}
}
impl std::fmt::Debug for Vec3 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Vec3")

View File

@ -2318,7 +2318,10 @@ class RustGenerator : public BaseGenerator {
code_.SetValue("FIELD_OFFSET", NumToString(offset_to_field));
code_.SetValue("REF", IsStruct(field.value.type) ? "&" : "");
cb(field);
offset_to_field += SizeOf(field.value.type.base_type) + field.padding;
const size_t size = IsStruct(field.value.type)
? field.value.type.struct_def->bytesize
: SizeOf(field.value.type.base_type);
offset_to_field += size + field.padding;
}
}
// Generate an accessor struct with constructor for a flatbuffers struct.
@ -2339,8 +2342,13 @@ class RustGenerator : public BaseGenerator {
// hold for PartialOrd/Ord.
code_ += "// struct {{STRUCT_NAME}}, aligned to {{ALIGN}}";
code_ += "#[repr(transparent)]";
code_ += "#[derive(Clone, Copy, PartialEq, Default)]";
code_ += "#[derive(Clone, Copy, PartialEq)]";
code_ += "pub struct {{STRUCT_NAME}}(pub [u8; {{STRUCT_SIZE}}]);";
code_ += "impl Default for {{STRUCT_NAME}} { ";
code_ += " fn default() -> Self { ";
code_ += " Self([0; {{STRUCT_SIZE}}])";
code_ += " }";
code_ += "}";
// Debug for structs.
code_ += "impl std::fmt::Debug for {{STRUCT_NAME}} {";

View File

@ -935,8 +935,8 @@ class SwiftGenerator : public BaseGenerator {
auto type = GenType(field.value.type);
code_.SetValue("VALUENAME", name);
if (IsStruct(field.value.type)) {
code_ += "var _v = _t.{{VALUENAME}}";
code_ += "_{{VALUENAME}} = _v.unpack()";
code_ += "var _v{{VALUENAME}} = _t.{{VALUENAME}}";
code_ += "_{{VALUENAME}} = _v{{VALUENAME}}.unpack()";
continue;
}
std::string is_enum = IsEnum(field.value.type) ? ".value" : "";

View File

@ -235,8 +235,8 @@ public struct MyGame_Example_Vec3: NativeStruct, NativeObject {
_z = _t.z
_test1 = _t.test1
_test2 = _t.test2.value
var _v = _t.test3
_test3 = _v.unpack()
var _vtest3 = _t.test3
_test3 = _vtest3.unpack()
}
public var x: Float32 { _x }
@ -334,6 +334,66 @@ public struct MyGame_Example_Ability_Mutable: FlatBufferObject {
}
}
public struct MyGame_Example_StructOfStructs: NativeStruct, NativeObject {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
private var _a: MyGame_Example_Ability
private var _b: MyGame_Example_Test
private var _c: MyGame_Example_Ability
public init(a: MyGame_Example_Ability, b: MyGame_Example_Test, c: MyGame_Example_Ability) {
_a = a
_b = b
_c = c
}
public init() {
_a = MyGame_Example_Ability()
_b = MyGame_Example_Test()
_c = MyGame_Example_Ability()
}
public init(_ _t: inout MyGame_Example_StructOfStructs_Mutable) {
var _va = _t.a
_a = _va.unpack()
var _vb = _t.b
_b = _vb.unpack()
var _vc = _t.c
_c = _vc.unpack()
}
public var a: MyGame_Example_Ability { _a }
public var b: MyGame_Example_Test { _b }
public var c: MyGame_Example_Ability { _c }
}
public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_1_12_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Struct
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 0) }
public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.postion + 8) }
public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.postion + 12) }
public mutating func unpack() -> MyGame_Example_StructOfStructs {
return MyGame_Example_StructOfStructs(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructs?) -> Offset<UOffset> {
guard var obj = obj else { return Offset<UOffset>() }
return pack(&builder, obj: &obj)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructs) -> Offset<UOffset> {
return builder.create(struct: obj)
}
}
public struct MyGame_InParentNamespace: FlatBufferObject, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_1_12_0() }

View File

@ -0,0 +1,83 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::System.Collections.Generic;
using global::FlatBuffers;
public struct StructOfStructs : IFlatbufferObject
{
private Struct __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); }
public StructOfStructs __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.Ability A { get { return (new MyGame.Example.Ability()).__assign(__p.bb_pos + 0, __p.bb); } }
public MyGame.Example.Test B { get { return (new MyGame.Example.Test()).__assign(__p.bb_pos + 8, __p.bb); } }
public MyGame.Example.Ability C { get { return (new MyGame.Example.Ability()).__assign(__p.bb_pos + 12, __p.bb); } }
public static Offset<MyGame.Example.StructOfStructs> CreateStructOfStructs(FlatBufferBuilder builder, uint a_Id, uint a_Distance, short b_A, sbyte b_B, uint c_Id, uint c_Distance) {
builder.Prep(4, 20);
builder.Prep(4, 8);
builder.PutUint(c_Distance);
builder.PutUint(c_Id);
builder.Prep(2, 4);
builder.Pad(1);
builder.PutSbyte(b_B);
builder.PutShort(b_A);
builder.Prep(4, 8);
builder.PutUint(a_Distance);
builder.PutUint(a_Id);
return new Offset<MyGame.Example.StructOfStructs>(builder.Offset);
}
public StructOfStructsT UnPack() {
var _o = new StructOfStructsT();
this.UnPackTo(_o);
return _o;
}
public void UnPackTo(StructOfStructsT _o) {
_o.A = this.A.UnPack();
_o.B = this.B.UnPack();
_o.C = this.C.UnPack();
}
public static Offset<MyGame.Example.StructOfStructs> Pack(FlatBufferBuilder builder, StructOfStructsT _o) {
if (_o == null) return default(Offset<MyGame.Example.StructOfStructs>);
var _a_id = _o.A.Id;
var _a_distance = _o.A.Distance;
var _b_a = _o.B.A;
var _b_b = _o.B.B;
var _c_id = _o.C.Id;
var _c_distance = _o.C.Distance;
return CreateStructOfStructs(
builder,
_a_id,
_a_distance,
_b_a,
_b_b,
_c_id,
_c_distance);
}
};
public class StructOfStructsT
{
[Newtonsoft.Json.JsonProperty("a")]
public MyGame.Example.AbilityT A { get; set; }
[Newtonsoft.Json.JsonProperty("b")]
public MyGame.Example.TestT B { get; set; }
[Newtonsoft.Json.JsonProperty("c")]
public MyGame.Example.AbilityT C { get; set; }
public StructOfStructsT() {
this.A = new MyGame.Example.AbilityT();
this.B = new MyGame.Example.TestT();
this.C = new MyGame.Example.AbilityT();
}
}
}

View File

@ -0,0 +1,80 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package Example
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type StructOfStructsT struct {
A *AbilityT
B *TestT
C *AbilityT
}
func (t *StructOfStructsT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
return CreateStructOfStructs(builder, t.A.Id, t.A.Distance, t.B.A, t.B.B, t.C.Id, t.C.Distance)
}
func (rcv *StructOfStructs) UnPackTo(t *StructOfStructsT) {
t.A = rcv.A(nil).UnPack()
t.B = rcv.B(nil).UnPack()
t.C = rcv.C(nil).UnPack()
}
func (rcv *StructOfStructs) UnPack() *StructOfStructsT {
if rcv == nil { return nil }
t := &StructOfStructsT{}
rcv.UnPackTo(t)
return t
}
type StructOfStructs struct {
_tab flatbuffers.Struct
}
func (rcv *StructOfStructs) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *StructOfStructs) Table() flatbuffers.Table {
return rcv._tab.Table
}
func (rcv *StructOfStructs) A(obj *Ability) *Ability {
if obj == nil {
obj = new(Ability)
}
obj.Init(rcv._tab.Bytes, rcv._tab.Pos+0)
return obj
}
func (rcv *StructOfStructs) B(obj *Test) *Test {
if obj == nil {
obj = new(Test)
}
obj.Init(rcv._tab.Bytes, rcv._tab.Pos+8)
return obj
}
func (rcv *StructOfStructs) C(obj *Ability) *Ability {
if obj == nil {
obj = new(Ability)
}
obj.Init(rcv._tab.Bytes, rcv._tab.Pos+12)
return obj
}
func CreateStructOfStructs(builder *flatbuffers.Builder, a_id uint32, a_distance uint32, b_a int16, b_b int8, c_id uint32, c_distance uint32) flatbuffers.UOffsetT {
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(c_distance)
builder.PrependUint32(c_id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(b_b)
builder.PrependInt16(b_a)
builder.Prep(4, 8)
builder.PrependUint32(a_distance)
builder.PrependUint32(a_id)
return builder.Offset()
}

View File

@ -0,0 +1,44 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class StructOfStructs extends Struct {
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public StructOfStructs __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.Ability a() { return a(new MyGame.Example.Ability()); }
public MyGame.Example.Ability a(MyGame.Example.Ability obj) { return obj.__assign(bb_pos + 0, bb); }
public MyGame.Example.Test b() { return b(new MyGame.Example.Test()); }
public MyGame.Example.Test b(MyGame.Example.Test obj) { return obj.__assign(bb_pos + 8, bb); }
public MyGame.Example.Ability c() { return c(new MyGame.Example.Ability()); }
public MyGame.Example.Ability c(MyGame.Example.Ability obj) { return obj.__assign(bb_pos + 12, bb); }
public static int createStructOfStructs(FlatBufferBuilder builder, long a_id, long a_distance, short b_a, byte b_b, long c_id, long c_distance) {
builder.prep(4, 20);
builder.prep(4, 8);
builder.putInt((int)c_distance);
builder.putInt((int)c_id);
builder.prep(2, 4);
builder.pad(1);
builder.putByte(b_b);
builder.putShort(b_a);
builder.prep(4, 8);
builder.putInt((int)a_distance);
builder.putInt((int)a_id);
return builder.offset();
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public StructOfStructs get(int j) { return get(new StructOfStructs(), j); }
public StructOfStructs get(StructOfStructs obj, int j) { return obj.__assign(__element(j), bb); }
}
}

View File

@ -0,0 +1,42 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class StructOfStructs : Struct() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : StructOfStructs {
__init(_i, _bb)
return this
}
val a : MyGame.Example.Ability? get() = a(MyGame.Example.Ability())
fun a(obj: MyGame.Example.Ability) : MyGame.Example.Ability? = obj.__assign(bb_pos + 0, bb)
val b : MyGame.Example.Test? get() = b(MyGame.Example.Test())
fun b(obj: MyGame.Example.Test) : MyGame.Example.Test? = obj.__assign(bb_pos + 8, bb)
val c : MyGame.Example.Ability? get() = c(MyGame.Example.Ability())
fun c(obj: MyGame.Example.Ability) : MyGame.Example.Ability? = obj.__assign(bb_pos + 12, bb)
companion object {
fun createStructOfStructs(builder: FlatBufferBuilder, a_id: UInt, a_distance: UInt, b_a: Short, b_b: Byte, c_id: UInt, c_distance: UInt) : Int {
builder.prep(4, 20)
builder.prep(4, 8)
builder.putInt(c_distance.toInt())
builder.putInt(c_id.toInt())
builder.prep(2, 4)
builder.pad(1)
builder.putByte(b_b)
builder.putShort(b_a)
builder.prep(4, 8)
builder.putInt(a_distance.toInt())
builder.putInt(a_id.toInt())
return builder.offset()
}
}
}

View File

@ -0,0 +1,45 @@
-- automatically generated by the FlatBuffers compiler, do not modify
-- namespace: Example
local flatbuffers = require('flatbuffers')
local StructOfStructs = {} -- the module
local StructOfStructs_mt = {} -- the class metatable
function StructOfStructs.New()
local o = {}
setmetatable(o, {__index = StructOfStructs_mt})
return o
end
function StructOfStructs_mt:Init(buf, pos)
self.view = flatbuffers.view.New(buf, pos)
end
function StructOfStructs_mt:A(obj)
obj:Init(self.view.bytes, self.view.pos + 0)
return obj
end
function StructOfStructs_mt:B(obj)
obj:Init(self.view.bytes, self.view.pos + 8)
return obj
end
function StructOfStructs_mt:C(obj)
obj:Init(self.view.bytes, self.view.pos + 12)
return obj
end
function StructOfStructs.CreateStructOfStructs(builder, a_id, a_distance, b_a, b_b, c_id, c_distance)
builder:Prep(4, 20)
builder:Prep(4, 8)
builder:PrependUint32(c_distance)
builder:PrependUint32(c_id)
builder:Prep(2, 4)
builder:Pad(1)
builder:PrependInt8(b_b)
builder:PrependInt16(b_a)
builder:Prep(4, 8)
builder:PrependUint32(a_distance)
builder:PrependUint32(a_id)
return builder:Offset()
end
return StructOfStructs -- return the module

View File

@ -0,0 +1,74 @@
<?php
// automatically generated by the FlatBuffers compiler, do not modify
namespace MyGame\Example;
use \Google\FlatBuffers\Struct;
use \Google\FlatBuffers\Table;
use \Google\FlatBuffers\ByteBuffer;
use \Google\FlatBuffers\FlatBufferBuilder;
class StructOfStructs extends Struct
{
/**
* @param int $_i offset
* @param ByteBuffer $_bb
* @return StructOfStructs
**/
public function init($_i, ByteBuffer $_bb)
{
$this->bb_pos = $_i;
$this->bb = $_bb;
return $this;
}
/**
* @return Ability
*/
public function getA()
{
$obj = new Ability();
$obj->init($this->bb_pos + 0, $this->bb);
return $obj;
}
/**
* @return Test
*/
public function getB()
{
$obj = new Test();
$obj->init($this->bb_pos + 8, $this->bb);
return $obj;
}
/**
* @return Ability
*/
public function getC()
{
$obj = new Ability();
$obj->init($this->bb_pos + 12, $this->bb);
return $obj;
}
/**
* @return int offset
*/
public static function createStructOfStructs(FlatBufferBuilder $builder, $a_id, $a_distance, $b_a, $b_b, $c_id, $c_distance)
{
$builder->prep(4, 20);
$builder->prep(4, 8);
$builder->putUint($c_distance);
$builder->putUint($c_id);
$builder->prep(2, 4);
$builder->pad(1);
$builder->putSbyte($b_b);
$builder->putShort($b_a);
$builder->prep(4, 8);
$builder->putUint($a_distance);
$builder->putUint($a_id);
return $builder->offset();
}
}

View File

@ -0,0 +1,90 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class StructOfStructs(object):
__slots__ = ['_tab']
@classmethod
def SizeOf(cls):
return 20
# StructOfStructs
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# StructOfStructs
def A(self, obj):
obj.Init(self._tab.Bytes, self._tab.Pos + 0)
return obj
# StructOfStructs
def B(self, obj):
obj.Init(self._tab.Bytes, self._tab.Pos + 8)
return obj
# StructOfStructs
def C(self, obj):
obj.Init(self._tab.Bytes, self._tab.Pos + 12)
return obj
def CreateStructOfStructs(builder, a_id, a_distance, b_a, b_b, c_id, c_distance):
builder.Prep(4, 20)
builder.Prep(4, 8)
builder.PrependUint32(c_distance)
builder.PrependUint32(c_id)
builder.Prep(2, 4)
builder.Pad(1)
builder.PrependInt8(b_b)
builder.PrependInt16(b_a)
builder.Prep(4, 8)
builder.PrependUint32(a_distance)
builder.PrependUint32(a_id)
return builder.Offset()
import MyGame.Example.Ability
import MyGame.Example.Test
try:
from typing import Optional
except:
pass
class StructOfStructsT(object):
# StructOfStructsT
def __init__(self):
self.a = None # type: Optional[MyGame.Example.Ability.AbilityT]
self.b = None # type: Optional[MyGame.Example.Test.TestT]
self.c = None # type: Optional[MyGame.Example.Ability.AbilityT]
@classmethod
def InitFromBuf(cls, buf, pos):
structOfStructs = StructOfStructs()
structOfStructs.Init(buf, pos)
return cls.InitFromObj(structOfStructs)
@classmethod
def InitFromObj(cls, structOfStructs):
x = StructOfStructsT()
x._UnPack(structOfStructs)
return x
# StructOfStructsT
def _UnPack(self, structOfStructs):
if structOfStructs is None:
return
if structOfStructs.A(MyGame.Example.Ability.Ability()) is not None:
self.a = MyGame.Example.Ability.AbilityT.InitFromObj(structOfStructs.A(MyGame.Example.Ability.Ability()))
if structOfStructs.B(MyGame.Example.Test.Test()) is not None:
self.b = MyGame.Example.Test.TestT.InitFromObj(structOfStructs.B(MyGame.Example.Test.Test()))
if structOfStructs.C(MyGame.Example.Ability.Ability()) is not None:
self.c = MyGame.Example.Ability.AbilityT.InitFromObj(structOfStructs.C(MyGame.Example.Ability.Ability()))
# StructOfStructsT
def Pack(self, builder):
return CreateStructOfStructs(builder, self.a.id, self.a.distance, self.b.a, self.b.b, self.c.id, self.c.distance)

View File

@ -33,6 +33,8 @@ struct Vec3;
struct Ability;
struct StructOfStructs;
struct Stat;
struct StatBuilder;
struct StatT;
@ -69,6 +71,8 @@ inline const flatbuffers::TypeTable *Vec3TypeTable();
inline const flatbuffers::TypeTable *AbilityTypeTable();
inline const flatbuffers::TypeTable *StructOfStructsTypeTable();
inline const flatbuffers::TypeTable *StatTypeTable();
inline const flatbuffers::TypeTable *ReferrableTypeTable();
@ -693,6 +697,69 @@ struct Ability::Traits {
static constexpr size_t fields_number = 2;
};
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructOfStructs FLATBUFFERS_FINAL_CLASS {
private:
MyGame::Example::Ability a_;
MyGame::Example::Test b_;
MyGame::Example::Ability c_;
public:
struct Traits;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return StructOfStructsTypeTable();
}
StructOfStructs()
: a_(),
b_(),
c_() {
}
StructOfStructs(const MyGame::Example::Ability &_a, const MyGame::Example::Test &_b, const MyGame::Example::Ability &_c)
: a_(_a),
b_(_b),
c_(_c) {
}
const MyGame::Example::Ability &a() const {
return a_;
}
MyGame::Example::Ability &mutable_a() {
return a_;
}
const MyGame::Example::Test &b() const {
return b_;
}
MyGame::Example::Test &mutable_b() {
return b_;
}
const MyGame::Example::Ability &c() const {
return c_;
}
MyGame::Example::Ability &mutable_c() {
return c_;
}
template<size_t Index>
auto get_field() const {
if constexpr (Index == 0) return a();
else if constexpr (Index == 1) return b();
else if constexpr (Index == 2) return c();
else static_assert(Index != Index, "Invalid Field Index");
}
};
FLATBUFFERS_STRUCT_END(StructOfStructs, 20);
struct StructOfStructs::Traits {
using type = StructOfStructs;
static constexpr auto name = "StructOfStructs";
static constexpr auto fully_qualified_name = "MyGame.Example.StructOfStructs";
static constexpr std::array<const char *, 3> field_names = {
"a",
"b",
"c"
};
template<size_t Index>
using FieldType = decltype(std::declval<type>().get_field<Index>());
static constexpr size_t fields_number = 3;
};
} // namespace Example
struct InParentNamespaceT : public flatbuffers::NativeTable {
@ -3384,6 +3451,28 @@ inline const flatbuffers::TypeTable *AbilityTypeTable() {
return &tt;
}
inline const flatbuffers::TypeTable *StructOfStructsTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, 0 },
{ flatbuffers::ET_SEQUENCE, 0, 1 },
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::AbilityTypeTable,
MyGame::Example::TestTypeTable
};
static const int64_t values[] = { 0, 8, 12, 20 };
static const char * const names[] = {
"a",
"b",
"c"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_STRUCT, 3, type_codes, type_refs, nullptr, values, names
};
return &tt;
}
inline const flatbuffers::TypeTable *StatTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_STRING, 0, -1 },

View File

@ -110,8 +110,13 @@ impl<'a> flatbuffers::Verifiable for FromInclude {
impl flatbuffers::SimpleToVerifyInSlice for FromInclude {}
// struct Unused, aligned to 4
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default)]
#[derive(Clone, Copy, PartialEq)]
pub struct Unused(pub [u8; 4]);
impl Default for Unused {
fn default() -> Self {
Self([0; 4])
}
}
impl std::fmt::Debug for Unused {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Unused")

Binary file not shown.

View File

@ -56,6 +56,12 @@ struct Ability {
distance:uint;
}
struct StructOfStructs {
a: Ability;
b: Test;
c: Ability;
}
table Stat {
id:string;
val:long;

View File

@ -121,6 +121,21 @@
},
"additionalProperties" : false
},
"MyGame_Example_StructOfStructs" : {
"type" : "object",
"properties" : {
"a" : {
"$ref" : "#/definitions/MyGame_Example_Ability"
},
"b" : {
"$ref" : "#/definitions/MyGame_Example_Test"
},
"c" : {
"$ref" : "#/definitions/MyGame_Example_Ability"
}
},
"additionalProperties" : false
},
"MyGame_Example_Stat" : {
"type" : "object",
"properties" : {

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,8 @@ struct Vec3;
struct Ability;
struct StructOfStructs;
struct Stat;
struct StatBuilder;
struct StatT;
@ -69,6 +71,8 @@ bool operator==(const Vec3 &lhs, const Vec3 &rhs);
bool operator!=(const Vec3 &lhs, const Vec3 &rhs);
bool operator==(const Ability &lhs, const Ability &rhs);
bool operator!=(const Ability &lhs, const Ability &rhs);
bool operator==(const StructOfStructs &lhs, const StructOfStructs &rhs);
bool operator!=(const StructOfStructs &lhs, const StructOfStructs &rhs);
bool operator==(const StatT &lhs, const StatT &rhs);
bool operator!=(const StatT &lhs, const StatT &rhs);
bool operator==(const ReferrableT &lhs, const ReferrableT &rhs);
@ -98,6 +102,8 @@ inline const flatbuffers::TypeTable *Vec3TypeTable();
inline const flatbuffers::TypeTable *AbilityTypeTable();
inline const flatbuffers::TypeTable *StructOfStructsTypeTable();
inline const flatbuffers::TypeTable *StatTypeTable();
inline const flatbuffers::TypeTable *ReferrableTypeTable();
@ -777,6 +783,59 @@ inline bool operator!=(const Ability &lhs, const Ability &rhs) {
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructOfStructs FLATBUFFERS_FINAL_CLASS {
private:
MyGame::Example::Ability a_;
MyGame::Example::Test b_;
MyGame::Example::Ability c_;
public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return StructOfStructsTypeTable();
}
StructOfStructs()
: a_(),
b_(),
c_() {
}
StructOfStructs(const MyGame::Example::Ability &_a, const MyGame::Example::Test &_b, const MyGame::Example::Ability &_c)
: a_(_a),
b_(_b),
c_(_c) {
}
const MyGame::Example::Ability &a() const {
return a_;
}
MyGame::Example::Ability &mutable_a() {
return a_;
}
const MyGame::Example::Test &b() const {
return b_;
}
MyGame::Example::Test &mutable_b() {
return b_;
}
const MyGame::Example::Ability &c() const {
return c_;
}
MyGame::Example::Ability &mutable_c() {
return c_;
}
};
FLATBUFFERS_STRUCT_END(StructOfStructs, 20);
inline bool operator==(const StructOfStructs &lhs, const StructOfStructs &rhs) {
return
(lhs.a() == rhs.a()) &&
(lhs.b() == rhs.b()) &&
(lhs.c() == rhs.c());
}
inline bool operator!=(const StructOfStructs &lhs, const StructOfStructs &rhs) {
return !(lhs == rhs);
}
} // namespace Example
struct InParentNamespaceT : public flatbuffers::NativeTable {
@ -3356,6 +3415,28 @@ inline const flatbuffers::TypeTable *AbilityTypeTable() {
return &tt;
}
inline const flatbuffers::TypeTable *StructOfStructsTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, 0 },
{ flatbuffers::ET_SEQUENCE, 0, 1 },
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::AbilityTypeTable,
MyGame::Example::TestTypeTable
};
static const int64_t values[] = { 0, 8, 12, 20 };
static const char * const names[] = {
"a",
"b",
"c"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_STRUCT, 3, type_codes, type_refs, nullptr, values, names
};
return &tt;
}
inline const flatbuffers::TypeTable *StatTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_STRING, 0, -1 },

View File

@ -54,6 +54,8 @@ class Vec3
class Ability
class StructOfStructs
class Stat
class Referrable
@ -164,6 +166,28 @@ def CreateAbility(b_:flatbuffers_builder, id:int, distance:int):
b_.PrependUint32(id)
return b_.Offset()
class StructOfStructs : flatbuffers_handle
def a():
return MyGame_Example_Ability{ buf_, pos_ + 0 }
def b():
return MyGame_Example_Test{ buf_, pos_ + 8 }
def c():
return MyGame_Example_Ability{ buf_, pos_ + 12 }
def CreateStructOfStructs(b_:flatbuffers_builder, a_id:int, a_distance:int, b_a:int, b_b:int, c_id:int, c_distance:int):
b_.Prep(4, 20)
b_.Prep(4, 8)
b_.PrependUint32(c_distance)
b_.PrependUint32(c_id)
b_.Prep(2, 4)
b_.Pad(1)
b_.PrependInt8(b_b)
b_.PrependInt16(b_a)
b_.Prep(4, 8)
b_.PrependUint32(a_distance)
b_.PrependUint32(a_id)
return b_.Offset()
class Stat : flatbuffers_handle
def id():
return buf_.flatbuffers_field_string(pos_, 4)

View File

@ -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)]

View File

@ -544,6 +544,85 @@ class AbilityObjectBuilder extends fb.ObjectBuilder {
return fbBuilder.finish(offset, fileIdentifier);
}
}
class StructOfStructs {
StructOfStructs._(this._bc, this._bcOffset);
static const fb.Reader<StructOfStructs> reader = const _StructOfStructsReader();
final fb.BufferContext _bc;
final int _bcOffset;
Ability get a => Ability.reader.read(_bc, _bcOffset + 0);
Test get b => Test.reader.read(_bc, _bcOffset + 8);
Ability get c => Ability.reader.read(_bc, _bcOffset + 12);
@override
String toString() {
return 'StructOfStructs{a: $a, b: $b, c: $c}';
}
}
class _StructOfStructsReader extends fb.StructReader<StructOfStructs> {
const _StructOfStructsReader();
@override
int get size => 20;
@override
StructOfStructs createObject(fb.BufferContext bc, int offset) =>
new StructOfStructs._(bc, offset);
}
class StructOfStructsBuilder {
StructOfStructsBuilder(this.fbBuilder) {
assert(fbBuilder != null);
}
final fb.Builder fbBuilder;
int finish(fb.StructBuilder a, fb.StructBuilder b, fb.StructBuilder c) {
c();
b();
a();
return fbBuilder.offset;
}
}
class StructOfStructsObjectBuilder extends fb.ObjectBuilder {
final AbilityObjectBuilder _a;
final TestObjectBuilder _b;
final AbilityObjectBuilder _c;
StructOfStructsObjectBuilder({
AbilityObjectBuilder a,
TestObjectBuilder b,
AbilityObjectBuilder c,
})
: _a = a,
_b = b,
_c = c;
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
_c.finish(fbBuilder);
_b.finish(fbBuilder);
_a.finish(fbBuilder);
return fbBuilder.offset;
}
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
}
}
class Stat {
Stat._(this._bc, this._bcOffset);
factory Stat(List<int> bytes) {

View File

@ -0,0 +1,84 @@
// automatically generated by the FlatBuffers compiler, do not modify
import * as flatbuffers from 'flatbuffers';
import { Ability, AbilityT } from '../../my-game/example/ability';
import { Test, TestT } from '../../my-game/example/test';
export class StructOfStructs {
bb: flatbuffers.ByteBuffer|null = null;
bb_pos = 0;
__init(i:number, bb:flatbuffers.ByteBuffer):StructOfStructs {
this.bb_pos = i;
this.bb = bb;
return this;
}
a(obj?:Ability):Ability|null {
return (obj || new Ability()).__init(this.bb_pos, this.bb!);
}
b(obj?:Test):Test|null {
return (obj || new Test()).__init(this.bb_pos + 8, this.bb!);
}
c(obj?:Ability):Ability|null {
return (obj || new Ability()).__init(this.bb_pos + 12, this.bb!);
}
static sizeOf():number {
return 20;
}
static createStructOfStructs(builder:flatbuffers.Builder, a_id: number, a_distance: number, b_a: number, b_b: number, c_id: number, c_distance: number):flatbuffers.Offset {
builder.prep(4, 20);
builder.prep(4, 8);
builder.writeInt32(c_distance);
builder.writeInt32(c_id);
builder.prep(2, 4);
builder.pad(1);
builder.writeInt8(b_b);
builder.writeInt16(b_a);
builder.prep(4, 8);
builder.writeInt32(a_distance);
builder.writeInt32(a_id);
return builder.offset();
}
unpack(): StructOfStructsT {
return new StructOfStructsT(
(this.a() !== null ? this.a()!.unpack() : null),
(this.b() !== null ? this.b()!.unpack() : null),
(this.c() !== null ? this.c()!.unpack() : null)
);
}
unpackTo(_o: StructOfStructsT): void {
_o.a = (this.a() !== null ? this.a()!.unpack() : null);
_o.b = (this.b() !== null ? this.b()!.unpack() : null);
_o.c = (this.c() !== null ? this.c()!.unpack() : null);
}
}
export class StructOfStructsT {
constructor(
public a: AbilityT|null = null,
public b: TestT|null = null,
public c: AbilityT|null = null
){}
pack(builder:flatbuffers.Builder): flatbuffers.Offset {
return StructOfStructs.createStructOfStructs(builder,
(this.a === null ? 0 : this.a.id!),
(this.a === null ? 0 : this.a.distance!),
(this.b === null ? 0 : this.b.a!),
(this.b === null ? 0 : this.b.b!),
(this.c === null ? 0 : this.c.id!),
(this.c === null ? 0 : this.c.distance!)
);
}
}

View File

@ -247,8 +247,13 @@ impl<'a> flatbuffers::Verifiable for EnumInNestedNS {
impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {}
// struct StructInNestedNS, aligned to 4
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default)]
#[derive(Clone, Copy, PartialEq)]
pub struct StructInNestedNS(pub [u8; 8]);
impl Default for StructInNestedNS {
fn default() -> Self {
Self([0; 8])
}
}
impl std::fmt::Debug for StructInNestedNS {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("StructInNestedNS")

View File

@ -247,8 +247,13 @@ impl<'a> flatbuffers::Verifiable for EnumInNestedNS {
impl flatbuffers::SimpleToVerifyInSlice for EnumInNestedNS {}
// struct StructInNestedNS, aligned to 4
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default)]
#[derive(Clone, Copy, PartialEq)]
pub struct StructInNestedNS(pub [u8; 8]);
impl Default for StructInNestedNS {
fn default() -> Self {
Self([0; 8])
}
}
impl std::fmt::Debug for StructInNestedNS {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("StructInNestedNS")

View File

@ -1111,6 +1111,39 @@ mod roundtrip_byteswap {
// fn fuzz_f64() { quickcheck::QuickCheck::new().max_tests(N).quickcheck(prop_f64 as fn(f64)); }
}
quickcheck! {
fn struct_of_structs(
a_id: u32,
a_distance: u32,
b_a: i16,
b_b: i8,
c_id: u32,
c_distance: u32
) -> bool {
use my_game::example::*;
let mut sos = StructOfStructs::default();
let mut a = Ability::default();
a.set_id(a_id);
a.set_distance(a_distance);
let mut b = Test::default();
b.set_a(b_a);
b.set_b(b_b);
let mut c = Ability::default();
c.set_id(c_id);
c.set_distance(c_distance);
sos.set_a(&a);
sos.set_b(&b);
sos.set_c(&c);
sos.a().id() == a_id &&
sos.a().distance() == a_distance &&
sos.b().a() == b_a &&
sos.b().b() == b_b &&
sos.c().id() == c_id &&
sos.c().distance() == c_distance
}
}
#[cfg(not(miri))] // slow.
#[cfg(test)]
mod roundtrip_vectors {