Skip to content

Commit

Permalink
Upgrade Swift dependencies (#1334)
Browse files Browse the repository at this point in the history
### Changelog
Swift: updated dependencies to latest versions of swift-collections etc.

### Docs

None

### Description

Resolves #1332

Most code changes are auto fixes from linters/formatters, and there was
a small `.min()` -> `.min` change in the Heap API
  • Loading branch information
jtbandes authored Feb 12, 2025
1 parent c2ddb3c commit ccd1811
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ jobs:
lfs: false
- uses: swift-actions/setup-swift@v2
with:
swift-version: "5.7"
- run: docker run -t --rm -v $(pwd):/work -w /work ghcr.io/realm/swiftlint:0.49.1
- run: docker run -t --rm -v $(pwd):/work ghcr.io/nicklockwood/swiftformat:0.49.18 --lint /work
swift-version: "6.0"
- run: docker run -t --rm -v $(pwd):/work -w /work ghcr.io/realm/swiftlint:0.58.2
- run: docker run -t --rm -v $(pwd):/work ghcr.io/nicklockwood/swiftformat:0.55.5 --lint /work
- run: swift build
- run: swift test

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:

- uses: swift-actions/setup-swift@v2
with:
swift-version: "5.7"
swift-version: "6.0"

- run: mkdir __docs__

Expand Down
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ excluded:
- .build

disabled_rules:
- blanket_disable_command
- function_body_length
- type_body_length
- file_length
Expand Down
19 changes: 14 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,34 @@
"repositoryURL": "https://github.com/apple/swift-algorithms",
"state": {
"branch": null,
"revision": "b14b7f4c528c942f121c8b860b9410b2bf57825e",
"version": "1.0.0"
"revision": "87e50f483c54e6efd60e885f7f5aa946cee68023",
"version": "1.2.1"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections",
"state": {
"branch": null,
"revision": "53a8adc54374f620002a3b6401d39e0feb3c57ae",
"version": null
"revision": "671108c96644956dddcd89dd59c203dcdb36cec7",
"version": "1.1.4"
}
},
{
"package": "SwiftDocCPlugin",
"repositoryURL": "https://github.com/apple/swift-docc-plugin",
"state": {
"branch": null,
"revision": "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"revision": "85e4bb4e1cd62cec64a4b8e769dcefdf0c5b9d64",
"version": "1.4.3"
}
},
{
"package": "SymbolKit",
"repositoryURL": "https://github.com/swiftlang/swift-docc-symbolkit",
"state": {
"branch": null,
"revision": "b45d1f2ed151d057b54504d653e0da5552844e34",
"version": "1.0.0"
}
},
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ let package = Package(
.library(name: "MCAP", targets: ["MCAP"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.4.3"),
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.1"),

// Use pre-release version for Heap
.package(url: "https://github.com/apple/swift-collections", revision: "53a8adc54374f620002a3b6401d39e0feb3c57ae"),
.package(url: "https://github.com/apple/swift-collections", .upToNextMajor(from: "1.1.4")),
],
targets: [
.target(
Expand Down
16 changes: 9 additions & 7 deletions swift/conformance/ReadConformanceRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private func readStreamed(file: FileHandle) throws -> Data {
}

private func readIndexed(file: FileHandle) throws -> Data {
let reader = try MCAPRandomAccessReader(file)
let reader = try MCAPRandomAccessReader(FileHandleReadable(fileHandle: file))
let schemas: [Record] = reader.schemasById.values.map { $0 }.sorted { $0.id < $1.id }
let channels: [Record] = reader.channelsById.values.map { $0 }.sorted { $0.id < $1.id }
var messages: [Record] = []
Expand All @@ -103,23 +103,25 @@ private func readIndexed(file: FileHandle) throws -> Data {
return data
}

extension FileHandle: IRandomAccessReadable {
struct FileHandleReadable: IRandomAccessReadable {
let fileHandle: FileHandle

public func size() -> UInt64 {
if #available(macOS 10.15.4, *) {
return try! seekToEnd()
return try! fileHandle.seekToEnd()
} else {
return seekToEndOfFile()
return fileHandle.seekToEndOfFile()
}
}

public func read(offset: UInt64, length: UInt64) -> Data? {
do {
if #available(macOS 10.15.4, *) {
try seek(toOffset: offset)
try fileHandle.seek(toOffset: offset)
} else {
seek(toFileOffset: offset)
fileHandle.seek(toFileOffset: offset)
}
return readData(ofLength: Int(length))
return fileHandle.readData(ofLength: Int(length))
} catch {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions swift/crc/CRC32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct CRC32 {
public static let polynomial: UInt32 = 0xEDB8_8320

@usableFromInline
internal var state: UInt32 = 0xFFFF_FFFF
var state: UInt32 = 0xFFFF_FFFF

public init() {}

Expand All @@ -31,21 +31,21 @@ public struct CRC32 {

@inlinable
public mutating func update(_ slice: Slice<UnsafeRawBufferPointer>) {
self.update(UnsafeRawBufferPointer(rebasing: slice))
update(UnsafeRawBufferPointer(rebasing: slice))
}

@inlinable
public mutating func update(_ data: UnsafeRawBufferPointer) {
#if (arch(arm) || arch(arm64)) && !os(iOS)
self.updateARM(data)
updateARM(data)
#else
self.update16Byte(data)
update16Byte(data)
#endif
}

/// 16-byte tabular update algorithm from: https://github.com/komrad36/CRC#option-10-16-byte-tabular
@inlinable
internal mutating func update16Byte(_ data: UnsafeRawBufferPointer) {
mutating func update16Byte(_ data: UnsafeRawBufferPointer) {
let lowByte: UInt32 = 0xFF
let tableSize: UInt32 = 256

Expand Down Expand Up @@ -97,7 +97,7 @@ public struct CRC32 {

#if (arch(arm) || arch(arm64)) && !os(iOS)
@inlinable
internal mutating func updateARM(_ data: UnsafeRawBufferPointer) {
mutating func updateARM(_ data: UnsafeRawBufferPointer) {
var offset = 0
let basePtr = Int(bitPattern: data.baseAddress)
while offset < data.count, !(basePtr + offset).isMultiple(of: MemoryLayout<UInt64>.alignment) {
Expand All @@ -122,7 +122,7 @@ public struct CRC32 {

extension CRC32 {
@usableFromInline
internal static let table = generateTables(16)
static let table = generateTables(16)

private static func generateTables(_ numTables: Int) -> [UInt32] {
var table = [UInt32](repeating: 0, count: numTables * 256)
Expand Down
12 changes: 6 additions & 6 deletions swift/mcap/MCAPRandomAccessReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public protocol IRandomAccessReadable {

private extension IRandomAccessReadable {
func checkedRead(offset: UInt64, length: UInt64) throws -> Data {
guard let data = self.read(offset: offset, length: length) else {
guard let data = read(offset: offset, length: length) else {
throw MCAPReadError.readBeyondBounds(offset: offset, length: length)
}
if UInt64(data.count) != length {
Expand Down Expand Up @@ -77,7 +77,7 @@ class ChunkCursor: Comparable {

var sortTime: UInt64 {
if let messageIndexCursors = messageIndexCursors {
let cursor = messageIndexCursors.min()!
let cursor = messageIndexCursors.min!
return cursor.records[cursor.index].logTime
}
return chunkIndex.messageStartTime
Expand Down Expand Up @@ -213,7 +213,7 @@ public class MCAPRandomAccessReader {
}
magicAndHeaderLength = UInt64(offset) + headerLength
let headerData = try readable.checkedRead(offset: UInt64(offset), length: headerLength)
self.header = try Header.deserializingFields(from: headerData)
header = try Header.deserializingFields(from: headerData)
}

// Read trailing magic and footer
Expand All @@ -238,7 +238,7 @@ public class MCAPRandomAccessReader {
throw MCAPReadError.invalidMagic(actual: Array(magic))
}

self.footer = try Footer.deserializing(from: footerAndMagic)
footer = try Footer.deserializing(from: footerAndMagic)
}

if footer.summaryStart == 0 {
Expand Down Expand Up @@ -298,7 +298,7 @@ public class MCAPRandomAccessReader {
let decompressedData: Data
if chunk.compression.isEmpty {
decompressedData = chunk.records
} else if let decompress = self.decompressHandlers[chunk.compression] {
} else if let decompress = decompressHandlers[chunk.compression] {
decompressedData = try decompress(chunk.records, chunk.uncompressedSize)
} else {
throw MCAPReadError.unsupportedCompression(chunk.compression)
Expand Down Expand Up @@ -345,7 +345,7 @@ public class MCAPRandomAccessReader {
if let topics = topics.map(Set.init) {
relevantChannels = Set(reader.channelsById.lazy.filter { topics.contains($1.topic) }.map(\.key))
}
self.chunkCursors = Heap<ChunkCursor>(reader.chunkIndexes.compactMap {
chunkCursors = Heap<ChunkCursor>(reader.chunkIndexes.compactMap {
if let startTime = startTime, $0.messageEndTime < startTime {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions swift/mcap/MCAPStreamedReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class MCAPStreamedReader {
let record = try recordReader.nextRecord()
switch record {
case let chunk as Chunk:
chunkReader = RecordReader(try _decompress(chunk))
chunkReader = try RecordReader(_decompress(chunk))
default:
return record
}
Expand All @@ -91,7 +91,7 @@ public class MCAPStreamedReader {
let decompressedData: Data
if chunk.compression.isEmpty {
decompressedData = chunk.records
} else if let decompress = self.decompressHandlers[chunk.compression] {
} else if let decompress = decompressHandlers[chunk.compression] {
decompressedData = try decompress(chunk.records, chunk.uncompressedSize)
} else {
throw MCAPReadError.unsupportedCompression(chunk.compression)
Expand Down
19 changes: 10 additions & 9 deletions swift/mcap/Records.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public extension Record {
data.append(Self.opcode.rawValue)
data.append(littleEndian: UInt64(0)) // placeholder
let fieldsStartOffset = data.count
self.serializeFields(to: &data)
serializeFields(to: &data)
let fieldsLength = data.count - fieldsStartOffset
withUnsafeBytes(of: UInt64(fieldsLength).littleEndian) {
data.replaceSubrange(
Expand Down Expand Up @@ -129,14 +129,14 @@ private extension Data {
}

mutating func appendPrefixedMap(_ map: [String: String]) {
let sizeOffset = self.count
let sizeOffset = count
append(littleEndian: UInt32(0)) // placeholder
for (key, value) in map {
appendPrefixedString(key)
appendPrefixedString(value)
}
Swift.withUnsafeBytes(
of: UInt32(self.count - sizeOffset - MemoryLayout<UInt32>.size).littleEndian
of: UInt32(count - sizeOffset - MemoryLayout<UInt32>.size).littleEndian
) {
replaceSubrange(sizeOffset ..< sizeOffset + MemoryLayout<UInt32>.size, with: $0)
}
Expand Down Expand Up @@ -170,9 +170,9 @@ private extension Data {
}
}

internal extension UnsafeRawBufferPointer {
extension UnsafeRawBufferPointer {
func read<T: FixedWidthInteger & UnsignedInteger>(littleEndian _: T.Type, from offset: inout Int) throws -> T {
if offset + MemoryLayout<T>.size > self.count {
if offset + MemoryLayout<T>.size > count {
throw MCAPReadError.readBeyondBounds(offset: UInt64(offset), length: UInt64(MemoryLayout<T>.size))
}
defer { offset += MemoryLayout<T>.size }
Expand All @@ -185,16 +185,17 @@ internal extension UnsafeRawBufferPointer {

func readPrefixedString(from offset: inout Int) throws -> String {
let length = try read(littleEndian: UInt32.self, from: &offset)
if offset + Int(length) > self.count {
if offset + Int(length) > count {
throw MCAPReadError.stringLengthBeyondBounds
}
defer { offset += Int(length) }
// swiftlint:disable:next optional_data_string_conversion
return String(decoding: self[offset ..< offset + Int(length)], as: UTF8.self)
}

func readUInt32PrefixedData(from offset: inout Int) throws -> Data {
let length = try read(littleEndian: UInt32.self, from: &offset)
if offset + Int(length) > self.count {
if offset + Int(length) > count {
throw MCAPReadError.dataLengthBeyondBounds
}
defer { offset += Int(length) }
Expand All @@ -203,7 +204,7 @@ internal extension UnsafeRawBufferPointer {

func readUInt64PrefixedData(from offset: inout Int) throws -> Data {
let length = try read(littleEndian: UInt64.self, from: &offset)
if offset + Int(length) > self.count {
if offset + Int(length) > count {
throw MCAPReadError.dataLengthBeyondBounds
}
defer { offset += Int(length) }
Expand Down Expand Up @@ -452,7 +453,7 @@ public struct Chunk: Record {
data.append(littleEndian: uncompressedSize)
data.append(littleEndian: uncompressedCRC)
data.appendPrefixedString(compression)
data.appendUInt64PrefixedData(self.records)
data.appendUInt64PrefixedData(records)
}
}

Expand Down
2 changes: 1 addition & 1 deletion swift/test/MCAPRandomAccessReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension MCAPRandomAccessReader.MessageIterator {
}
}

extension Message: Equatable {
extension Message: @retroactive Equatable {
// swiftlint:disable:next large_tuple
var comparisonFields: (ChannelID, UInt32, Timestamp, Timestamp, Data) {
(channelID, sequence, logTime, publishTime, data)
Expand Down

0 comments on commit ccd1811

Please sign in to comment.