From f171d78bf02047fd9344cc7072cbb047b4a67cd3 Mon Sep 17 00:00:00 2001 From: mustiikhalil <26250654+mustiikhalil@users.noreply.github.com> Date: Tue, 14 Jan 2025 22:54:08 +0100 Subject: [PATCH] Disables redendent return because of buildkite throwing errors for now --- swift.swiftformat | 2 +- swift/Sources/FlatBuffers/ByteBuffer.swift | 55 ++++++----- .../FlatBuffers/FlatBufferBuilder.swift | 96 +++++++++---------- .../SwiftFlatBuffers/fuzzer_generated.swift | 4 +- .../FlatBuffersUnionTests.swift | 3 +- 5 files changed, 85 insertions(+), 75 deletions(-) diff --git a/swift.swiftformat b/swift.swiftformat index e8b2c324b30..be8210238d6 100644 --- a/swift.swiftformat +++ b/swift.swiftformat @@ -17,7 +17,7 @@ --typeattributes prev-line # wrapAttributes # rules ---rules wrap,todos,anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader +--rules wrap,todos,anyObjectProtocol,redundantParens,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader --disable trailingclosures --exclude **/*_generated.swift diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift index 9589f6614da..9bf5a13f9f8 100644 --- a/swift/Sources/FlatBuffers/ByteBuffer.swift +++ b/swift/Sources/FlatBuffers/ByteBuffer.swift @@ -40,8 +40,8 @@ public struct ByteBuffer { var isOwned: Bool { switch self { - case .writePointer: true - default: false + case .writePointer: return true + default: return false } } } @@ -102,17 +102,19 @@ public struct ByteBuffer { { switch retainedBlob { case .byteBuffer(let byteBuffer): - try byteBuffer.withUnsafeBytes(body) + return try byteBuffer.withUnsafeBytes(body) + #if !os(WASI) case .data(let data): - try data.withUnsafeBytes(body) + return try data.withUnsafeBytes(body) case .bytes(let contiguousBytes): - try contiguousBytes.withUnsafeBytes(body) + return try contiguousBytes.withUnsafeBytes(body) + #endif case .array(let array): - try array.withUnsafeBytes(body) + return try array.withUnsafeBytes(body) case .pointer(let ptr): - try body(UnsafeRawBufferPointer(start: ptr, count: capacity)) + return try body(UnsafeRawBufferPointer(start: ptr, count: capacity)) case .writePointer(let ptr): - try body(UnsafeRawBufferPointer(start: ptr, count: capacity)) + return try body(UnsafeRawBufferPointer(start: ptr, count: capacity)) } } @@ -123,26 +125,28 @@ public struct ByteBuffer { { switch retainedBlob { case .byteBuffer(let byteBuffer): - try byteBuffer.withUnsafeRawPointer(body) + return try byteBuffer.withUnsafeRawPointer(body) + #if !os(WASI) case .data(let data): - try data + return try data .withUnsafeBytes { try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!)) } case .bytes(let contiguousBytes): - try contiguousBytes + return try contiguousBytes .withUnsafeBytes { try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!)) } + #endif case .array(let array): - try array + return try array .withUnsafeBytes { try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!)) } case .pointer(let ptr): - try body(ptr) + return try body(ptr) case .writePointer(let ptr): - try body(ptr) + return try body(ptr) } } @@ -153,23 +157,25 @@ public struct ByteBuffer { { switch retainedBlob { case .byteBuffer(let byteBuffer): - try byteBuffer.readWithUnsafeRawPointer(position: position, body) + return try byteBuffer.readWithUnsafeRawPointer(position: position, body) + #if !os(WASI) case .data(let data): - try data.withUnsafeBytes { + return try data.withUnsafeBytes { try body($0.baseAddress!.advanced(by: position)) } case .bytes(let contiguousBytes): - try contiguousBytes.withUnsafeBytes { + return try contiguousBytes.withUnsafeBytes { try body($0.baseAddress!.advanced(by: position)) } + #endif case .array(let array): - try array.withUnsafeBytes { + return try array.withUnsafeBytes { try body($0.baseAddress!.advanced(by: position)) } case .pointer(let ptr): - try body(ptr.advanced(by: position)) + return try body(ptr.advanced(by: position)) case .writePointer(let ptr): - try body(ptr.advanced(by: position)) + return try body(ptr.advanced(by: position)) } } } @@ -192,7 +198,9 @@ public struct ByteBuffer { /// - bytes: Array of UInt8 @inline(__always) init(byteBuffer: _InternalByteBuffer) { - _storage = Storage(blob: .byteBuffer(byteBuffer), capacity: byteBuffer.capacity) + _storage = Storage( + blob: .byteBuffer(byteBuffer), + capacity: byteBuffer.capacity) _readerIndex = Int(byteBuffer.size) } @@ -392,7 +400,7 @@ public struct ByteBuffer { assert( index + count <= _storage.capacity, "Reading out of bounds is illegal") - return _storage.retainedBlob.readWithUnsafeRawPointer(position: index) { + return _storage.readWithUnsafeRawPointer(position: index) { String(cString: $0.bindMemory(to: UInt8.self, capacity: count)) } } @@ -449,7 +457,8 @@ extension ByteBuffer: CustomDebugStringConvertible { """ buffer located at: \(_storage.retainedBlob), with capacity of \(_storage.capacity), - { writerSize: \(_readerIndex), readerSize: \(reader), writerIndex: \(writerIndex) } + { writerSize: \(_readerIndex), readerSize: \(reader), writerIndex: \( + writerIndex) } """ } } diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index bc571233bc6..c96f24cde90 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -59,21 +59,21 @@ public struct FlatBufferBuilder { public var capacity: Int { _bb.capacity } #if !os(WASI) - /// Data representation of the buffer - /// - /// Should only be used after ``finish(offset:addPrefix:)`` is called - public var data: Data { - assert(finished, "Data shouldn't be called before finish()") - return _bb.withUnsafeSlicedBytes { ptr in - var data = Data() - data.append( - ptr.baseAddress!.bindMemory( - to: UInt8.self, - capacity: _bb.capacity), - count: _bb.capacity) - return data - } + /// Data representation of the buffer + /// + /// Should only be used after ``finish(offset:addPrefix:)`` is called + public var data: Data { + assert(finished, "Data shouldn't be called before finish()") + return _bb.withUnsafeSlicedBytes { ptr in + var data = Data() + data.append( + ptr.baseAddress!.bindMemory( + to: UInt8.self, + capacity: _bb.capacity), + count: _bb.capacity) + return data } + } #endif /// Returns the underlying bytes in the ``ByteBuffer`` @@ -110,7 +110,7 @@ public struct FlatBufferBuilder { public var sizedBuffer: ByteBuffer { assert(finished, "Data shouldn't be called before finish()") return _bb.withUnsafeSlicedBytes { ptr in - return ByteBuffer( + ByteBuffer( copyingMemoryBound: ptr.baseAddress!, capacity: ptr.count) } @@ -128,8 +128,8 @@ public struct FlatBufferBuilder { /// however the builder can be force by passing true for `serializeDefaults` public init( initialSize: Int32 = 1024, - serializeDefaults force: Bool = false - ) { + serializeDefaults force: Bool = false) + { assert(initialSize > 0, "Size should be greater than zero!") guard isLitteEndian else { fatalError( @@ -196,8 +196,8 @@ public struct FlatBufferBuilder { mutating public func finish( offset: Offset, fileId: String, - addPrefix prefix: Bool = false - ) { + addPrefix prefix: Bool = false) + { let size = MemoryLayout.size preAlign( len: size &+ (prefix ? size : 0) &+ FileIdLength, @@ -226,8 +226,8 @@ public struct FlatBufferBuilder { /// include the size of the current buffer. mutating public func finish( offset: Offset, - addPrefix prefix: Bool = false - ) { + addPrefix prefix: Bool = false) + { notNested() let size = MemoryLayout.size preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment) @@ -351,8 +351,8 @@ public struct FlatBufferBuilder { @usableFromInline mutating internal func padding( bufSize: UInt32, - elementSize: UInt32 - ) -> UInt32 { + elementSize: UInt32) -> UInt32 + { ((~bufSize) &+ 1) & (elementSize &- 1) } @@ -479,8 +479,8 @@ public struct FlatBufferBuilder { @inline(__always) mutating public func createVector( _ elements: [T], - size: Int - ) -> Offset { + size: Int) -> Offset + { let size = size startVector(size, elementSize: MemoryLayout.size) _bb.push(elements: elements) @@ -488,20 +488,20 @@ public struct FlatBufferBuilder { } #if swift(>=5.0) && !os(WASI) - @inline(__always) - /// Creates a vector of bytes in the buffer. - /// - /// Allows creating a vector from `Data` without copying to a `[UInt8]` - /// - /// - Parameter bytes: bytes to be written into the buffer - /// - Returns: ``Offset`` of the vector - mutating public func createVector(bytes: ContiguousBytes) -> Offset { - bytes.withUnsafeBytes { - startVector($0.count, elementSize: MemoryLayout.size) - _bb.push(bytes: $0) - return endVector(len: $0.count) - } + @inline(__always) + /// Creates a vector of bytes in the buffer. + /// + /// Allows creating a vector from `Data` without copying to a `[UInt8]` + /// + /// - Parameter bytes: bytes to be written into the buffer + /// - Returns: ``Offset`` of the vector + mutating public func createVector(bytes: ContiguousBytes) -> Offset { + bytes.withUnsafeBytes { + startVector($0.count, elementSize: MemoryLayout.size) + _bb.push(bytes: $0) + return endVector(len: $0.count) } + } #endif /// Creates a vector of type ``Enum`` into the ``ByteBuffer`` @@ -539,8 +539,8 @@ public struct FlatBufferBuilder { @inline(__always) mutating public func createVector( _ elements: [T], - size: Int - ) -> Offset { + size: Int) -> Offset + { let size = size startVector(size, elementSize: T.byteSize) for index in stride(from: elements.count, to: 0, by: -1) { @@ -585,8 +585,8 @@ public struct FlatBufferBuilder { @inline(__always) mutating public func createVector( ofOffsets offsets: [Offset], - len: Int - ) -> Offset { + len: Int) -> Offset + { startVector(len, elementSize: MemoryLayout.size) for index in stride(from: offsets.count, to: 0, by: -1) { push(element: offsets[index &- 1]) @@ -662,8 +662,8 @@ public struct FlatBufferBuilder { @inline(__always) @discardableResult mutating public func create( - struct s: T, position: VOffset - ) -> Offset { + struct s: T, position: VOffset) -> Offset + { let offset = create(struct: s) _vtableStorage.add( loc: (offset: _bb.size, position: VOffset(position))) @@ -687,8 +687,8 @@ public struct FlatBufferBuilder { @inline(__always) @discardableResult mutating public func create( - struct s: T - ) -> Offset { + struct s: T) -> Offset + { let size = MemoryLayout.size preAlign(len: size, alignment: MemoryLayout.alignment) _bb.push(struct: s, size: size) @@ -803,8 +803,8 @@ public struct FlatBufferBuilder { mutating public func add( element: T, def: T, - at position: VOffset - ) { + at position: VOffset) + { if element == def && !serializeDefaults { return } track(offset: push(element: element), at: position) } diff --git a/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift b/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift index facb58cb17f..4af34f9e216 100644 --- a/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift +++ b/tests/swift/tests/Sources/SwiftFlatBuffers/fuzzer_generated.swift @@ -228,7 +228,7 @@ public struct Monster: FlatBufferObject, Verifiable { 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 inventoryPointer: UnsafeBufferPointer? { return _accessor.getBufferPointer(at: VTOFFSET.inventory.v) } + public func withUnsafePointerToInventory(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) } public var color: Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue } public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 7) } public static func add(pos: Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) } @@ -261,7 +261,7 @@ public struct Monster: FlatBufferObject, Verifiable { } public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { var off = offsets - off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 10, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } + off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 10, fbb: &fbb), fbb: &fbb) < 0 } return fbb.createVector(ofOffsets: off) } fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> Monster? { diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersUnionTests.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersUnionTests.swift index 575340b40fb..c3a6a52c7c1 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersUnionTests.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersUnionTests.swift @@ -93,7 +93,8 @@ final class FlatBuffersUnionTests: XCTestCase { // swiftformat:disable all XCTAssertEqual(builder.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0]) // swiftformat:enable all - let monster = ColorsNameSpace.Monster.getRootAsMonster(bb: builder.sizedBuffer) + let monster = ColorsNameSpace.Monster + .getRootAsMonster(bb: builder.sizedBuffer) XCTAssertEqual(monster.colorsCount, 2) XCTAssertEqual(monster.colors(at: 0), .blue) XCTAssertEqual(monster.colors(at: 1), .green)