[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

@@ -17,7 +17,7 @@
import Foundation
#if canImport(Common)
import Common
import Common
#endif
/// Verifiable is a protocol all swift flatbuffers object should conform to,
@@ -51,8 +51,8 @@ extension Verifiable {
@discardableResult
public static func verifyRange<T>(
_ verifier: inout Verifier,
at position: Int, of type: T.Type
) throws -> (start: Int, count: Int) {
at position: Int, of type: T.Type) throws -> (start: Int, count: Int)
{
let len: UOffset = try verifier.getValue(at: position)
let intLen = Int(len)
let start = Int(clamping: (position &+ MemoryLayout<Int32>.size).magnitude)
@@ -74,8 +74,8 @@ extension Verifiable where Self: Scalar {
public static func verify<T>(
_ verifier: inout Verifier,
at position: Int,
of type: T.Type
) throws where T: Verifiable {
of type: T.Type) throws where T: Verifiable
{
try verifier.inBuffer(position: position, of: type.self)
}
}
@@ -96,8 +96,8 @@ public enum ForwardOffset<U>: Verifiable where U: Verifiable {
public static func verify<T>(
_ verifier: inout Verifier,
at position: Int,
of type: T.Type
) throws where T: Verifiable {
of type: T.Type) throws where T: Verifiable
{
let offset: UOffset = try verifier.getValue(at: position)
let nextOffset = Int(clamping: (Int(offset) &+ position).magnitude)
try U.verify(&verifier, at: nextOffset, of: U.self)
@@ -120,8 +120,8 @@ public enum Vector<U, S>: Verifiable where U: Verifiable, S: Verifiable {
public static func verify<T>(
_ verifier: inout Verifier,
at position: Int,
of type: T.Type
) throws where T: Verifiable {
of type: T.Type) throws where T: Verifiable
{
/// checks if the next verification type S is equal to U of type forwardOffset
/// This had to be done since I couldnt find a solution for duplicate call functions
/// A fix will be appreciated
@@ -170,8 +170,8 @@ public enum UnionVector<S> where S: UnionEnum {
fieldPosition: Int,
unionKeyName: String,
fieldName: String,
completion: @escaping Completion
) throws {
completion: @escaping Completion) throws
{
/// Get offset for union key vectors and offset vectors
let keyOffset: UOffset = try verifier.getValue(at: keyPosition)
let fieldOffset: UOffset = try verifier.getValue(at: fieldPosition)