[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

@@ -31,8 +31,8 @@ public struct FixedTypedVector: FlexBufferVector {
offset: Int,
byteWidth: UInt8,
type: FlexBufferType,
count: Int
) {
count: Int)
{
self.byteBuffer = byteBuffer
self.offset = offset
self.byteWidth = byteWidth

View File

@@ -39,14 +39,13 @@ public protocol FlexBufferContiguousBytes {
var count: Int { get }
func withUnsafeRawBufferPointer<Result>(
_ body: (UnsafeRawBufferPointer) throws -> Result
) rethrows -> Result
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
}
extension FlexBufferContiguousBytes {
public func withUnsafeRawBufferPointer<Result>(
_ body: (UnsafeRawBufferPointer) throws -> Result
) rethrows -> Result {
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
{
try byteBuffer.withUnsafePointerToSlice(
index: offset,
count: count,

View File

@@ -58,8 +58,8 @@ public struct Reference {
byteBuffer: ByteBuffer,
offset: Int,
parentWidth: UInt8,
packedType: UInt8
) {
packedType: UInt8)
{
guard let type = FlexBufferType(rawValue: UInt64(packedType >> 2)) else {
return nil
}
@@ -76,8 +76,8 @@ public struct Reference {
offset: Int,
parentWidth: UInt8,
byteWidth: UInt8,
type: FlexBufferType
) {
type: FlexBufferType)
{
self.byteBuffer = byteBuffer
self.offset = offset
self.parentWidth = parentWidth
@@ -241,8 +241,7 @@ public struct Reference {
@inline(__always)
public func withUnsafeRawPointer<Result>(
_ completion: (UnsafeRawPointer) throws
-> Result
)
-> Result)
rethrows -> Result?
{
return try byteBuffer.readWithUnsafeRawPointer(

View File

@@ -30,8 +30,8 @@ public struct TypedVector: FlexBufferVector {
byteBuffer: ByteBuffer,
offset: Int,
byteWidth: UInt8,
type: FlexBufferType
) {
type: FlexBufferType)
{
self.byteBuffer = byteBuffer
self.offset = offset
self.byteWidth = byteWidth
@@ -54,8 +54,8 @@ public struct TypedVector: FlexBufferVector {
static func mapKeys(
byteBuffer: ByteBuffer,
offset: Int,
byteWidth: UInt8
) -> TypedVector {
byteWidth: UInt8) -> TypedVector
{
let prefixedFields = 3
let keysOffset = offset &- (numericCast(byteWidth) &* prefixedFields)
@@ -88,8 +88,8 @@ extension TypedVector {
byteWidth)
return byteBuffer.readWithUnsafeRawPointer(
position: indirectoffset
) { bufPointer in
position: indirectoffset)
{ bufPointer in
target.withCString { strPointer in
Int(strcmp(bufPointer, strPointer))
}