[Swift] Implements FlatbuffersVector which confirms to RandomAccessCollection (#8752)

* Implements FlatbuffersVector in swift

Implements FlatbuffersVector which confirms to RandomAccessCollection,
this would give us semi-native sugary syntax to all the arrays in swift port.

This work will also be the foundation of using arrays in swift

* Fix failing tests for Swift
This commit is contained in:
mustiikhalil
2025-11-05 00:53:59 +01:00
committed by GitHub
parent 78a3d59a65
commit 5fe90a9160
63 changed files with 1554 additions and 1650 deletions

View File

@@ -8,7 +8,7 @@ import Common
import FlatBuffers
public enum MyGame_Sample_Color: Int8, Enum, Verifiable {
public enum MyGame_Sample_Color: Int8, FlatbuffersVectorInitializable, Enum, Verifiable {
public typealias T = Int8
public static var byteSize: Int { return MemoryLayout<Int8>.size }
public var value: Int8 { return self.rawValue }
@@ -31,7 +31,7 @@ extension MyGame_Sample_Color: Encodable {
}
}
public enum MyGame_Sample_Equipment: UInt8, UnionEnum {
public enum MyGame_Sample_Equipment: UInt8, FlatbuffersVectorInitializable, UnionEnum {
public typealias T = UInt8
public init?(value: T) {
@@ -73,7 +73,7 @@ public struct MyGame_Sample_EquipmentUnion {
}
}
}
public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
public struct MyGame_Sample_Vec3: NativeStruct, FlatbuffersVectorInitializable, Verifiable, FlatbuffersInitializable, NativeObject {
static func validateVersion() { FlatBuffersVersion_25_9_23() }
@@ -136,7 +136,7 @@ extension MyGame_Sample_Vec3: Encodable {
}
}
public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
public struct MyGame_Sample_Vec3_Mutable: FlatBufferStruct, FlatbuffersVectorInitializable {
static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -165,7 +165,7 @@ public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
}
}
public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable, ObjectAPIPacker {
public struct MyGame_Sample_Monster: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -197,24 +197,17 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable, ObjectAPIPack
@discardableResult public func mutate(hp: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.hp.v); return _accessor.mutate(hp, index: o) }
public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
public var hasInventory: Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? false : true }
public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
public var inventory: FlatbufferVector<UInt8> { return _accessor.vector(at: VTOFFSET.inventory.v, byteSize: 1) }
public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public var color: MyGame_Sample_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Sample_Color(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .blue }
@discardableResult public func mutate(color: MyGame_Sample_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
public var hasWeapons: Bool { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? false : true }
public var weaponsCount: Int32 { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func weapons(at index: Int32) -> MyGame_Sample_Weapon? { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? nil : MyGame_Sample_Weapon(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
public var weapons: FlatbufferVector<MyGame_Sample_Weapon> { return _accessor.vector(at: VTOFFSET.weapons.v, byteSize: 4) }
public var equippedType: MyGame_Sample_Equipment { let o = _accessor.offset(VTOFFSET.equippedType.v); return o == 0 ? .none_ : MyGame_Sample_Equipment(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
public func equipped<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.equipped.v); return o == 0 ? nil : _accessor.union(o) }
public var hasPath: Bool { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? false : true }
public var pathCount: Int32 { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func path(at index: Int32) -> MyGame_Sample_Vec3? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Sample_Vec3.self, offset: _accessor.vector(at: o) + index * 12) }
public func mutablePath(at index: Int32) -> MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : MyGame_Sample_Vec3_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 12) }
public func withUnsafePointerToPath<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.path.v, body: body) }
public var path: FlatbufferVector<MyGame_Sample_Vec3> { return _accessor.vector(at: VTOFFSET.path.v, byteSize: 12) }
public var mutablePath: FlatbufferVector<MyGame_Sample_Vec3_Mutable> { return _accessor.vector(at: VTOFFSET.path.v, byteSize: 12) }
public func withUnsafePointerToPath<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.path.v, body: body) }
public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 11) }
public static func add(pos: MyGame_Sample_Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) }
public static func add(mana: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: mana, def: 150, at: VTOFFSET.mana.p) }
@@ -282,9 +275,8 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable, ObjectAPIPack
let __weapons = builder.createVector(ofOffsets: __weapons__)
let __equipped = obj.equipped?.pack(builder: &builder) ?? Offset()
MyGame_Sample_Monster.startVectorOfPath(obj.path.count, in: &builder)
for i in obj.path {
guard let _o = i else { continue }
builder.create(struct: _o)
for val in obj.path {
builder.create(struct: val)
}
let __path = builder.endVector(len: obj.path.count)
let __root = MyGame_Sample_Monster.startMonster(&builder)
@@ -350,19 +342,11 @@ extension MyGame_Sample_Monster: Encodable {
try container.encodeIfPresent(hp, forKey: .hp)
}
try container.encodeIfPresent(name, forKey: .name)
if inventoryCount > 0 {
try container.encodeIfPresent(inventory, forKey: .inventory)
}
try container.encodeIfPresent(inventory, forKey: .inventory)
if color != .blue {
try container.encodeIfPresent(color, forKey: .color)
}
if weaponsCount > 0 {
var contentEncoder = container.nestedUnkeyedContainer(forKey: .weapons)
for index in 0..<weaponsCount {
guard let type = weapons(at: index) else { continue }
try contentEncoder.encode(type)
}
}
try container.encodeIfPresent(weapons, forKey: .weapons)
if equippedType != .none_ {
try container.encodeIfPresent(equippedType, forKey: .equippedType)
}
@@ -372,13 +356,7 @@ extension MyGame_Sample_Monster: Encodable {
try container.encodeIfPresent(_v, forKey: .equipped)
default: break;
}
if pathCount > 0 {
var contentEncoder = container.nestedUnkeyedContainer(forKey: .path)
for index in 0..<pathCount {
guard let type = path(at: index) else { continue }
try contentEncoder.encode(type)
}
}
try container.encodeIfPresent(path, forKey: .path)
}
}
@@ -392,7 +370,7 @@ public class MyGame_Sample_MonsterT: NativeObject {
public var color: MyGame_Sample_Color
public var weapons: [MyGame_Sample_WeaponT?]
public var equipped: MyGame_Sample_EquipmentUnion?
public var path: [MyGame_Sample_Vec3?]
public var path: [MyGame_Sample_Vec3]
public init(_ _t: inout MyGame_Sample_Monster) {
pos = _t.pos
@@ -400,14 +378,11 @@ public class MyGame_Sample_MonsterT: NativeObject {
hp = _t.hp
name = _t.name
inventory = []
for index in 0..<_t.inventoryCount {
inventory.append(_t.inventory(at: index))
}
inventory.append(contentsOf: _t.inventory)
color = _t.color
weapons = []
for index in 0..<_t.weaponsCount {
var __v_ = _t.weapons(at: index)
weapons.append(__v_?.unpack())
for var val in _t.weapons{
weapons.append(val.unpack())
}
switch _t.equippedType {
case .weapon:
@@ -416,9 +391,7 @@ public class MyGame_Sample_MonsterT: NativeObject {
default: break
}
path = []
for index in 0..<_t.pathCount {
path.append(_t.path(at: index))
}
path.append(contentsOf: _t.path)
}
public init() {
@@ -434,7 +407,7 @@ public class MyGame_Sample_MonsterT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Sample_Monster.self) }
}
public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable, ObjectAPIPacker {
public struct MyGame_Sample_Weapon: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }