Skip to content

Commit 3f29225

Browse files
committed
Sendable
1 parent afe942a commit 3f29225

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Sources/KeyValueDecoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Foundation
3636
public final class KeyValueDecoder {
3737

3838
/// Contextual user-provided information for use during encoding.
39-
public var userInfo: [CodingUserInfoKey: Any]
39+
public var userInfo: [CodingUserInfoKey: any Sendable]
4040

4141
/// The strategy to use for decoding `nil`. Defaults to `Optional<Any>.none` which can be decoded to any optional type.
4242
public var nilDecodingStrategy: NilDecodingStrategy = .default
@@ -71,7 +71,7 @@ public final class KeyValueDecoder {
7171
/// Strategy used to decode nil values.
7272
public typealias NilDecodingStrategy = NilCodingStrategy
7373

74-
public enum IntDecodingStrategy {
74+
public enum IntDecodingStrategy: Sendable {
7575
/// Decodes all number types with lossless conversion or throws error.
7676
case exact
7777

Sources/KeyValueEncoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation
3535
public final class KeyValueEncoder {
3636

3737
/// Contextual user-provided information for use during encoding.
38-
public var userInfo: [CodingUserInfoKey: Any]
38+
public var userInfo: [CodingUserInfoKey: any Sendable]
3939

4040
/// The strategy to use for encoding `nil`. Defaults to `Optional<Any>.none` which can be cast to any optional type.
4141
public var nilEncodingStrategy: NilEncodingStrategy = .default
@@ -58,15 +58,15 @@ public final class KeyValueEncoder {
5858
}
5959

6060
/// Strategy used to encode and decode nil values.
61-
public enum NilCodingStrategy {
61+
public enum NilCodingStrategy: Sendable {
6262
/// `nil` values are removed
6363
case removed
6464

6565
/// `nil` values are substituted with a placeholder value
66-
case placeholder(Any, isNull: (Any) -> Bool)
66+
case placeholder(any Sendable, isNull: @Sendable (Any) -> Bool)
6767

6868
/// `nil` values are substituted with `Optional<Any>.none`. Can be cast to any optional type.
69-
public static var `default`: NilCodingStrategy { .placeholder(Optional<Any>.none as Any, isNull: isOptionalNone) }
69+
public static var `default`: NilCodingStrategy { .placeholder(Optional<any Sendable>.none as any Sendable, isNull: isOptionalNone) }
7070

7171
/// `nil` values are substituted with `"$null"` placeholder string. Compatible with `PropertyListEncoder`.
7272
public static var stringNull: NilCodingStrategy { .placeholder("$null", isNull: { ($0 as? String == "$null") }) }

Tests/KeyValueDecoderTests.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ private extension KeyValueDecoder {
10051005
}
10061006

10071007
let decoder = KeyValueDecoder()
1008-
decoder.userInfo[.decoder] = proxy.decode(from:)
1008+
decoder.userInfo[.decoder] = proxy as any DecodingProxy
10091009
_ = try decoder.decode(StubDecoder.self, from: value)
10101010
return proxy.result!
10111011
}
@@ -1020,7 +1020,7 @@ private extension KeyValueDecoder {
10201020
}
10211021

10221022
let decoder = KeyValueDecoder()
1023-
decoder.userInfo[.decoder] = proxy.decode(from:)
1023+
decoder.userInfo[.decoder] = proxy as any DecodingProxy
10241024
_ = try decoder.decode(StubDecoder.self, from: value)
10251025
return proxy.result!
10261026
}
@@ -1036,9 +1036,13 @@ private extension CodingUserInfoKey {
10361036
static let decoder = CodingUserInfoKey(rawValue: "decoder")!
10371037
}
10381038

1039+
private protocol DecodingProxy: Sendable {
1040+
func decode(from decoder: any Decoder) throws
1041+
}
1042+
10391043
private struct StubDecoder: Decodable {
10401044

1041-
final class Proxy<T> {
1045+
final class Proxy<T>: @unchecked Sendable, DecodingProxy {
10421046
private let closure: (any Decoder) throws -> T
10431047
private(set) var result: T?
10441048

@@ -1052,8 +1056,8 @@ private struct StubDecoder: Decodable {
10521056
}
10531057

10541058
init(from decoder: any Decoder) throws {
1055-
let closure = decoder.userInfo[.decoder] as! (any Decoder) throws -> Void
1056-
try closure(decoder)
1059+
let proxy = decoder.userInfo[.decoder] as! any DecodingProxy
1060+
try proxy.decode(from: decoder)
10571061
}
10581062
}
10591063

0 commit comments

Comments
 (0)