Skip to content

Commit

Permalink
Solve some Sendable warnings from the Swift 6 compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed Apr 28, 2024
1 parent 0e7df7b commit cda0492
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/MySQLKit/MySQLConnectionSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public struct MySQLConnectionSource: ConnectionPoolSource {
}
}

extension MySQLConnection: ConnectionPoolItem {}
extension MySQLNIO.MySQLConnection: AsyncKit.ConnectionPoolItem {} // Fully qualifying the type names implies @retroactive
9 changes: 6 additions & 3 deletions Sources/MySQLKit/MySQLDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import NIOFoundationCompat
/// Types which conform to `MySQLDataConvertible` are converted directly to the requested type. For other types,
/// an attempt is made to interpret the database value as JSON and decode the type from it.
public struct MySQLDataDecoder: Sendable {
/// A wrapper to silence `Sendable` warnings for `JSONDecoder` when not on macOS.
struct FakeSendable<T>: @unchecked Sendable { let value: T }

/// The `JSONDecoder` used for decoding values that can't be directly converted.
let json: JSONDecoder
let json: FakeSendable<JSONDecoder>

/// Initialize a ``MySQLDataDecoder`` with a JSON decoder.
///
/// - Parameter json: A `JSONDecoder` to use for decoding types that can't be directly converted. Defaults
/// to an unconfigured decoder.
public init(json: JSONDecoder = .init()) {
self.json = json
self.json = .init(value: json)
}

/// Convert the given `MySQLData` into a value of type `T`, if possible.
Expand All @@ -43,7 +46,7 @@ public struct MySQLDataDecoder: Sendable {
return try T.init(from: NestedSingleValueUnwrappingDecoder(decoder: self, data: data))
} catch is SQLCodingError where [.blob, .json, .longBlob, .mediumBlob, .string, .tinyBlob, .varString, .varchar].contains(data.type) {
// Couldn't unwrap it, but it's textual. Try decoding as JSON as a last ditch effort.
return try self.json.decode(T.self, from: data.buffer ?? .init())
return try self.json.value.decode(T.self, from: data.buffer ?? .init())
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions Sources/MySQLKit/MySQLDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import NIOFoundationCompat
/// Types which conform to `MySQLDataConvertible` are converted directly to `MySQLData`. Other types are
/// encoded as JSON and sent to the database as text.
public struct MySQLDataEncoder: Sendable {
/// A wrapper to silence `Sendable` warnings for `JSONEncoder` when not on macOS.
struct FakeSendable<T>: @unchecked Sendable { let value: T }

/// The `JSONEncoder` used for encoding values that can't be directly converted.
let json: JSONEncoder
let json: FakeSendable<JSONEncoder>

/// Initialize a ``MySQLDataEncoder`` with a JSON encoder.
///
/// - Parameter json: A `JSONEncoder` to use for encoding types that can't be directly converted. Defaults
/// to an unconfigured encoder.
public init(json: JSONEncoder = .init()) {
self.json = json
self.json = .init(value: json)
}

/// Convert the given `Encodable` value to a `MySQLData` value, if possible.
Expand All @@ -42,7 +45,7 @@ public struct MySQLDataEncoder: Sendable {
return MySQLData(
type: .string,
format: .text,
buffer: try self.json.encodeAsByteBuffer(value, allocator: .init()),
buffer: try self.json.value.encodeAsByteBuffer(value, allocator: .init()),
isUnsigned: true
)
}
Expand Down Expand Up @@ -165,3 +168,6 @@ public struct MySQLDataEncoder: Sendable {
}
}
}

/// This conformance suppresses a slew of spurious warnings in some build environments.
extension MySQLNIO.MySQLProtocol.DataType: @unchecked Swift.Sendable {} // Fully qualifying the type names implies @retroactive
2 changes: 1 addition & 1 deletion Sources/MySQLKit/MySQLRow+SQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension MySQLRow {
}
}

extension MySQLNIO.MySQLRow: @unchecked Sendable {} // Fully qualifying the type name implies @retroactive
extension MySQLNIO.MySQLRow: @unchecked Swift.Sendable {} // Fully qualifying the type names implies @retroactive

/// An error used to signal that a column requested from a `MySQLRow` using the `SQLRow` interface is not present.
struct MissingColumn: Error {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MySQLKit/Optional+MySQLDataConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MySQLNIO

/// Conforms `Optional` to `MySQLDataConvertible` for efficiency.
extension Optional: MySQLDataConvertible where Wrapped: MySQLDataConvertible {
extension Swift.Optional: MySQLNIO.MySQLDataConvertible where Wrapped: MySQLNIO.MySQLDataConvertible {
// See `MySQLDataConvertible.init?(mysqlData:)`.
public init?(mysqlData: MySQLData) {
if mysqlData.buffer != nil {
Expand Down

0 comments on commit cda0492

Please sign in to comment.