Skip to content

Commit

Permalink
Disables redendent return because of buildkite throwing errors for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mustiikhalil committed Jan 15, 2025
1 parent 523e4f4 commit f171d78
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
2 changes: 1 addition & 1 deletion swift.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 32 additions & 23 deletions swift/Sources/FlatBuffers/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public struct ByteBuffer {

var isOwned: Bool {
switch self {
case .writePointer: true
default: false
case .writePointer: return true
default: return false
}
}
}
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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)
}
}

Expand All @@ -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))
}
}
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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) }
"""
}
}
96 changes: 48 additions & 48 deletions swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down Expand Up @@ -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<UOffset>.size
preAlign(
len: size &+ (prefix ? size : 0) &+ FileIdLength,
Expand Down Expand Up @@ -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<UOffset>.size
preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment)
Expand Down Expand Up @@ -351,8 +351,8 @@ public struct FlatBufferBuilder {
@usableFromInline
mutating internal func padding(
bufSize: UInt32,
elementSize: UInt32
) -> UInt32 {
elementSize: UInt32) -> UInt32
{
((~bufSize) &+ 1) & (elementSize &- 1)
}

Expand Down Expand Up @@ -479,29 +479,29 @@ public struct FlatBufferBuilder {
@inline(__always)
mutating public func createVector<T: Scalar>(
_ elements: [T],
size: Int
) -> Offset {
size: Int) -> Offset
{
let size = size
startVector(size, elementSize: MemoryLayout<T>.size)
_bb.push(elements: elements)
return endVector(len: size)
}

#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<UInt8>.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<UInt8>.size)
_bb.push(bytes: $0)
return endVector(len: $0.count)
}
}
#endif

/// Creates a vector of type ``Enum`` into the ``ByteBuffer``
Expand Down Expand Up @@ -539,8 +539,8 @@ public struct FlatBufferBuilder {
@inline(__always)
mutating public func createVector<T: Enum>(
_ 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) {
Expand Down Expand Up @@ -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<Offset>.size)
for index in stride(from: offsets.count, to: 0, by: -1) {
push(element: offsets[index &- 1])
Expand Down Expand Up @@ -662,8 +662,8 @@ public struct FlatBufferBuilder {
@inline(__always)
@discardableResult
mutating public func create<T: NativeStruct>(
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)))
Expand All @@ -687,8 +687,8 @@ public struct FlatBufferBuilder {
@inline(__always)
@discardableResult
mutating public func create<T: NativeStruct>(
struct s: T
) -> Offset {
struct s: T) -> Offset
{
let size = MemoryLayout<T>.size
preAlign(len: size, alignment: MemoryLayout<T>.alignment)
_bb.push(struct: s, size: size)
Expand Down Expand Up @@ -803,8 +803,8 @@ public struct FlatBufferBuilder {
mutating public func add<T: Scalar>(
element: T,
def: T,
at position: VOffset
) {
at position: VOffset)
{
if element == def && !serializeDefaults { return }
track(offset: push(element: element), at: position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt8>? { return _accessor.getBufferPointer(at: VTOFFSET.inventory.v) }
public func withUnsafePointerToInventory<T>(_ 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) }
Expand Down Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f171d78

Please sign in to comment.