Skip to content

Commit

Permalink
Make models Sendable
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins committed Sep 21, 2024
1 parent 60a44e7 commit 9561ade
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
22 changes: 19 additions & 3 deletions Sources/ClientRuntime/PrimitiveTypeExtensions/Indirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@
// SPDX-License-Identifier: Apache-2.0
//

import class Foundation.NSRecursiveLock

@propertyWrapper
public class Indirect<T> {
public var wrappedValue: Optional<T>
public final class Indirect<T: Sendable>: @unchecked Sendable {
private let lock = NSRecursiveLock()
private var _wrappedValue: Optional<T>

public var wrappedValue: Optional<T> {
get {
lock.lock()
defer { lock.unlock() }
return _wrappedValue
}
set {
lock.lock()
defer { lock.unlock() }
_wrappedValue = newValue
}
}

public init(wrappedValue: Optional<T>) {
self.wrappedValue = wrappedValue
self._wrappedValue = wrappedValue
}
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/SmithyEventStreamsAPI/EventStreamError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public enum EventStreamError: Error {
case invalidMessage(String)
}

extension AsyncThrowingStream: Equatable where Element: Equatable {

public static func == (lhs: AsyncThrowingStream, rhs: AsyncThrowingStream) -> Bool {
// TODO: Remove as part of https://github.com/awslabs/aws-sdk-swift/issues/898
return false
}
}
//extension AsyncThrowingStream: Equatable where Element: Equatable {

Check warning on line 19 in Sources/SmithyEventStreamsAPI/EventStreamError.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Prefer at least one space after slashes for comments (comment_spacing)
//
// public static func == (lhs: AsyncThrowingStream, rhs: AsyncThrowingStream) -> Bool {
// // TODO: Remove as part of https://github.com/awslabs/aws-sdk-swift/issues/898
// return false
// }
//}

Check warning on line 25 in Sources/SmithyEventStreamsAPI/EventStreamError.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Prefer at least one space after slashes for comments (comment_spacing)
4 changes: 2 additions & 2 deletions Sources/SmithyHTTPAPI/Headers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
//

public struct Headers {
public struct Headers: Sendable {
public var headers: [Header] = []

/// Creates an empty instance.
Expand Down Expand Up @@ -197,7 +197,7 @@ extension Array where Element == Header {
}
}

public struct Header {
public struct Header: Sendable {
public var name: String
public var value: [String]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ class EnumGenerator(
writer.writeShapeDocs(shape)
writer.writeAvailableAttribute(null, shape)
writer.openBlock(
"public enum \$enum.name:L: \$N, \$N, \$N, \$N {",
"public enum \$enum.name:L: \$N, \$N, \$N, \$N, \$N {",
"}",
SwiftTypes.Protocols.Sendable,
SwiftTypes.Protocols.Equatable,
SwiftTypes.Protocols.RawRepresentable,
SwiftTypes.Protocols.CaseIterable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class IntEnumGenerator(
writer.writeShapeDocs(shape)
writer.writeAvailableAttribute(null, shape)
writer.openBlock(
"public enum \$enum.name:L: \$N, \$N, \$N, \$N {",
"public enum \$enum.name:L: \$N, \$N, \$N, \$N, \$N {",
"}",
SwiftTypes.Protocols.Sendable,
SwiftTypes.Protocols.Equatable,
SwiftTypes.Protocols.RawRepresentable,
SwiftTypes.Protocols.CaseIterable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class StructureGenerator(
private fun generateStruct() {
writer.writeShapeDocs(shape)
writer.writeAvailableAttribute(model, shape)
val equatableConformance = writer.format(": \$N ", SwiftTypes.Protocols.Equatable).takeIf { shape.hasTrait<EquatableConformanceTrait>() } ?: ""
writer.openBlock("public struct \$struct.name:L $equatableConformance{")
val equatableConformance = writer.format(", \$N", SwiftTypes.Protocols.Equatable).takeIf { shape.hasTrait<EquatableConformanceTrait>() } ?: ""
writer.openBlock("public struct \$struct.name:L: \$N$equatableConformance {", SwiftTypes.Protocols.Sendable)
.call { generateStructMembers() }
.write("")
.call { generateInitializerForStructure(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class UnionGenerator(
writer.writeShapeDocs(shape)
writer.writeAvailableAttribute(model, shape)
val indirectOrNot = "indirect ".takeIf { shape.hasTrait<RecursiveUnionTrait>() } ?: ""
val equatableConformance = (": " + SwiftTypes.Protocols.Equatable + " ").takeIf { shape.hasTrait<EquatableConformanceTrait>() } ?: ""
writer.openBlock("public ${indirectOrNot}enum \$union.name:L $equatableConformance{", "}\n") {
val equatableConformance = writer.format(", \$N", SwiftTypes.Protocols.Equatable).takeIf { shape.hasTrait<EquatableConformanceTrait>() } ?: ""
writer.openBlock("public ${indirectOrNot}enum \$union.name:L: \$N$equatableConformance {", "}\n", SwiftTypes.Protocols.Sendable) {
// event streams (@streaming union) MAY have variants that target errors.
// These errors if encountered on the stream will be thrown as an exception rather
// than showing up as one of the possible events the consumer will see on the stream (AsyncThrowingStream<T>).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object SwiftTypes {
val RawRepresentable = builtInSymbol("RawRepresentable", SwiftDeclaration.PROTOCOL)
val CaseIterable = builtInSymbol("CaseIterable", SwiftDeclaration.PROTOCOL)
val CustomDebugStringConvertible = builtInSymbol("CustomDebugStringConvertible", SwiftDeclaration.PROTOCOL)
val Sendable = builtInSymbol("Sendable", SwiftDeclaration.PROTOCOL)
}
}

Expand Down

0 comments on commit 9561ade

Please sign in to comment.