[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 struct Swift_Tests_Vectors: FlatBufferObject, Verifiable {
public struct Swift_Tests_Vectors: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable {
static func validateVersion() { FlatBuffersVersion_25_9_23() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -25,21 +25,12 @@ public struct Swift_Tests_Vectors: FlatBufferObject, Verifiable {
var p: VOffset { self.rawValue }
}
public var hasNone: Bool { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? false : true }
public var none_Count: Int32 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func none_(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var none_: [UInt64] { return _accessor.getVector(at: VTOFFSET.none_.v) ?? [] }
public func withUnsafePointerToNone<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.none_.v, body: body) }
public var hasEmpty: Bool { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? false : true }
public var emptyCount: Int32 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func empty(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var empty: [UInt64] { return _accessor.getVector(at: VTOFFSET.empty.v) ?? [] }
public func withUnsafePointerToEmpty<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.empty.v, body: body) }
public var hasArray: Bool { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? false : true }
public var arrayCount: Int32 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func array(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var array: [UInt64] { return _accessor.getVector(at: VTOFFSET.array.v) ?? [] }
public func withUnsafePointerToArray<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.array.v, body: body) }
public var none_: FlatbufferVector<UInt64> { return _accessor.vector(at: VTOFFSET.none_.v, byteSize: 8) }
public func withUnsafePointerToNone<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.none_.v, body: body) }
public var empty: FlatbufferVector<UInt64> { return _accessor.vector(at: VTOFFSET.empty.v, byteSize: 8) }
public func withUnsafePointerToEmpty<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.empty.v, body: body) }
public var array: FlatbufferVector<UInt64> { return _accessor.vector(at: VTOFFSET.array.v, byteSize: 8) }
public func withUnsafePointerToArray<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.array.v, body: body) }
public static func startVectors(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 3) }
public static func addVectorOf(none_: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: none_, at: VTOFFSET.none_.p) }
public static func addVectorOf(empty: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: empty, at: VTOFFSET.empty.p) }
@@ -76,15 +67,9 @@ extension Swift_Tests_Vectors: Encodable {
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if none_Count > 0 {
try container.encodeIfPresent(none_, forKey: .none_)
}
if emptyCount > 0 {
try container.encodeIfPresent(empty, forKey: .empty)
}
if arrayCount > 0 {
try container.encodeIfPresent(array, forKey: .array)
}
try container.encodeIfPresent(none_, forKey: .none_)
try container.encodeIfPresent(empty, forKey: .empty)
try container.encodeIfPresent(array, forKey: .array)
}
}