Skip to content

Commit

Permalink
Added support for Swift 6
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 19, 2024
1 parent 609e9e0 commit c0c6ca8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/mochidev/AsyncSequenceReader.git", .upToNextMinor(from: "0.2.1")),
.package(url: "https://github.com/mochidev/AsyncSequenceReader.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/mochidev/Bytes.git", .upToNextMinor(from: "0.3.0")),
],
targets: [
Expand All @@ -29,14 +29,14 @@ let package = Package(
"Bytes"
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
.enableExperimentalFeature("StrictConcurrency"),
]
),
.testTarget(
name: "CodableDatastoreTests",
dependencies: ["CodableDatastore"],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
.enableExperimentalFeature("StrictConcurrency"),
]
),
]
Expand Down
43 changes: 43 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "CodableDatastore",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
],
products: [
.library(
name: "CodableDatastore",
targets: ["CodableDatastore"]
),
],
dependencies: [
.package(url: "https://github.com/mochidev/AsyncSequenceReader.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/mochidev/Bytes.git", .upToNextMinor(from: "0.3.0")),
],
targets: [
.target(
name: "CodableDatastore",
dependencies: [
"AsyncSequenceReader",
"Bytes"
],
swiftSettings: [
.swiftLanguageVersion(.v6),
]
),
.testTarget(
name: "CodableDatastoreTests",
dependencies: ["CodableDatastore"],
swiftSettings: [
.swiftLanguageVersion(.v6),
]
),
]
)
4 changes: 4 additions & 0 deletions Sources/CodableDatastore/Indexes/IndexRepresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,8 @@ public struct AnyIndexRepresentation<Instance: Sendable>: Hashable, Sendable {
}

/// Forced KeyPath conformance since Swift 5.10 doesn't support it out of the box.
#if compiler(>=6)
extension KeyPath: @unchecked @retroactive Sendable where Root: Sendable, Value: Sendable {}
#else
extension KeyPath: @unchecked Sendable where Root: Sendable, Value: Sendable {}
#endif
9 changes: 9 additions & 0 deletions Sources/CodableDatastore/Indexes/Indexable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,21 @@ extension UInt16: DiscreteIndexable, RangedIndexable {}
extension UInt32: DiscreteIndexable, RangedIndexable {}
extension UInt64: DiscreteIndexable, RangedIndexable {}

#if compiler(>=6)
extension Optional: @retroactive Comparable where Wrapped: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
if let lhs, let rhs { return lhs < rhs }
return lhs == nil && rhs != nil
}
}
#else
extension Optional: Comparable where Wrapped: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
if let lhs, let rhs { return lhs < rhs }
return lhs == nil && rhs != nil
}
}
#endif
extension Optional: DiscreteIndexable where Wrapped: DiscreteIndexable {}
extension Optional: RangedIndexable where Wrapped: RangedIndexable {}

Expand Down
25 changes: 21 additions & 4 deletions Sources/CodableDatastore/Indexes/UUID+Comparable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ import Foundation

#if swift(<5.9) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux) || os(Windows)
/// Make UUIDs comparable, so that they can be used transparently as an index.
///
/// - SeeAlso: https://github.com/apple/swift-foundation/blob/5388acf1d929865d4df97d3c50e4d08bc4c6bdf0/Sources/FoundationEssentials/UUID.swift#L135-L156
#if compiler(>=6)
extension UUID: @retroactive Comparable {
@inlinable
public static func < (lhs: UUID, rhs: UUID) -> Bool {
lhs.isLessThan(rhs: rhs)
}
}
#else
extension UUID: Comparable {
@inlinable
public static func < (lhs: UUID, rhs: UUID) -> Bool {
var leftUUID = lhs.uuid
lhs.isLessThan(rhs: rhs)
}
}
#endif
#endif

extension UUID {
/// Make UUIDs comparable, so that they can be used transparently as an index.
///
/// - SeeAlso: https://github.com/apple/swift-foundation/blob/5388acf1d929865d4df97d3c50e4d08bc4c6bdf0/Sources/FoundationEssentials/UUID.swift#L135-L156
@usableFromInline
func isLessThan(rhs: UUID) -> Bool {
var leftUUID = self.uuid
var rightUUID = rhs.uuid
var result: Int = 0
var diff: Int = 0
Expand All @@ -36,4 +54,3 @@ extension UUID: Comparable {
return result < 0
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Foundation

extension ISO8601DateFormatter {
#if compiler(>=6)
nonisolated(unsafe)
static let withMilliseconds: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
Expand All @@ -21,6 +23,20 @@ extension ISO8601DateFormatter {
]
return formatter
}()
#else
static let withMilliseconds: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withTimeZone,
.withFractionalSeconds
]
return formatter
}()
#endif
}

extension JSONDecoder.DateDecodingStrategy {
Expand Down

0 comments on commit c0c6ca8

Please sign in to comment.