Skip to content

Commit a42b72a

Browse files
authored
Audit pass on Sendable conformances and requirements (apple#272)
* Audit pass on Sendable conformances and requirements * A slightly cleaner mark for Sendability of OrderedSet * Remove flags for strict mode
1 parent 156bb4b commit a42b72a

7 files changed

+19
-10
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"]),
1717
.library(name: "AsyncAlgorithms_XCTest", targets: ["AsyncAlgorithms_XCTest"]),
1818
],
19-
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.3"))],
19+
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.4"))],
2020
targets: [
2121
.target(
2222
name: "AsyncAlgorithms",

Sources/AsyncAlgorithms/Buffer/BoundedBufferStateMachine.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
1616
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
1717
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>
1818

19-
private enum State {
19+
fileprivate enum State {
2020
case initial(base: Base)
2121
case buffering(
2222
task: Task<Void, Never>,
@@ -308,3 +308,6 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
308308
}
309309
}
310310
}
311+
312+
extension BoundedBufferStateMachine: Sendable where Base: Sendable { }
313+
extension BoundedBufferStateMachine.State: Sendable where Base: Sendable { }

Sources/AsyncAlgorithms/Buffer/UnboundedBufferStateMachine.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
2121
case bufferingOldest(Int)
2222
}
2323

24-
private enum State {
24+
fileprivate enum State {
2525
case initial(base: Base)
2626
case buffering(
2727
task: Task<Void, Never>,
@@ -248,3 +248,6 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
248248
}
249249
}
250250
}
251+
252+
extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
253+
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }

Sources/AsyncAlgorithms/Channels/AsyncChannel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// on the `Iterator` is made, or when `finish()` is called from another Task.
2020
/// As `finish()` induces a terminal state, there is no more need for a back pressure management.
2121
/// This function does not suspend and will finish all the pending iterations.
22-
public final class AsyncChannel<Element>: AsyncSequence, @unchecked Sendable {
22+
public final class AsyncChannel<Element: Sendable>: AsyncSequence, @unchecked Sendable {
2323
public typealias Element = Element
2424
public typealias AsyncIterator = Iterator
2525

Sources/AsyncAlgorithms/Channels/AsyncThrowingChannel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/// and is resumed when the next call to `next()` on the `Iterator` is made, or when `finish()`/`fail(_:)` is called
1919
/// from another Task. As `finish()` and `fail(_:)` induce a terminal state, there is no more need for a back pressure management.
2020
/// Those functions do not suspend and will finish all the pending iterations.
21-
public final class AsyncThrowingChannel<Element, Failure: Error>: AsyncSequence, @unchecked Sendable {
21+
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, @unchecked Sendable {
2222
public typealias Element = Element
2323
public typealias AsyncIterator = Iterator
2424

Sources/AsyncAlgorithms/Channels/ChannelStateMachine.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
//===----------------------------------------------------------------------===//
1111
@_implementationOnly import OrderedCollections
1212

13-
struct ChannelStateMachine<Element, Failure: Error>: Sendable {
14-
private struct SuspendedProducer: Hashable {
13+
// NOTE: this is only marked as unchecked since the swift-collections tag is before auditing for Sendable
14+
extension OrderedSet: @unchecked Sendable where Element: Sendable { }
15+
16+
struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
17+
private struct SuspendedProducer: Hashable, Sendable {
1518
let id: UInt64
1619
let continuation: UnsafeContinuation<Void, Never>?
1720
let element: Element?
@@ -29,7 +32,7 @@ struct ChannelStateMachine<Element, Failure: Error>: Sendable {
2932
}
3033
}
3134

32-
private struct SuspendedConsumer: Hashable {
35+
private struct SuspendedConsumer: Hashable, Sendable {
3336
let id: UInt64
3437
let continuation: UnsafeContinuation<Element?, any Error>?
3538

@@ -51,7 +54,7 @@ struct ChannelStateMachine<Element, Failure: Error>: Sendable {
5154
case failed(Error)
5255
}
5356

54-
private enum State {
57+
private enum State: Sendable {
5558
case channeling(
5659
suspendedProducers: OrderedSet<SuspendedProducer>,
5760
cancelledProducers: Set<SuspendedProducer>,

Sources/AsyncAlgorithms/Channels/ChannelStorage.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// See https://swift.org/LICENSE.txt for license information
99
//
1010
//===----------------------------------------------------------------------===//
11-
struct ChannelStorage<Element, Failure: Error>: Sendable {
11+
struct ChannelStorage<Element: Sendable, Failure: Error>: Sendable {
1212
private let stateMachine: ManagedCriticalState<ChannelStateMachine<Element, Failure>>
1313
private let ids = ManagedCriticalState<UInt64>(0)
1414

0 commit comments

Comments
 (0)