Skip to content

Audit pass on Sendable conformances and requirements #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
.library(name: "_CAsyncSequenceValidationSupport", type: .static, targets: ["AsyncSequenceValidation"]),
.library(name: "AsyncAlgorithms_XCTest", targets: ["AsyncAlgorithms_XCTest"]),
],
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.3"))],
dependencies: [.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.4"))],
targets: [
.target(
name: "AsyncAlgorithms",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>

private enum State {
fileprivate enum State {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
Expand Down Expand Up @@ -308,3 +308,6 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
}
}
}

extension BoundedBufferStateMachine: Sendable where Base: Sendable { }
extension BoundedBufferStateMachine.State: Sendable where Base: Sendable { }
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
case bufferingOldest(Int)
}

private enum State {
fileprivate enum State {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
Expand Down Expand Up @@ -248,3 +248,6 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
}
}
}

extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }
2 changes: 1 addition & 1 deletion Sources/AsyncAlgorithms/Channels/AsyncChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// on the `Iterator` is made, or when `finish()` is called from another Task.
/// As `finish()` induces a terminal state, there is no more need for a back pressure management.
/// This function does not suspend and will finish all the pending iterations.
public final class AsyncChannel<Element>: AsyncSequence, @unchecked Sendable {
public final class AsyncChannel<Element: Sendable>: AsyncSequence, @unchecked Sendable {
public typealias Element = Element
public typealias AsyncIterator = Iterator

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

Expand Down
11 changes: 7 additions & 4 deletions Sources/AsyncAlgorithms/Channels/ChannelStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
//===----------------------------------------------------------------------===//
@_implementationOnly import OrderedCollections

struct ChannelStateMachine<Element, Failure: Error>: Sendable {
private struct SuspendedProducer: Hashable {
// NOTE: this is only marked as unchecked since the swift-collections tag is before auditing for Sendable
extension OrderedSet: @unchecked Sendable where Element: Sendable { }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lorentey This is the use in question

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is alright to do -- the type is already conditionally sendable, it just isn't marked as such in a shipping release yet.

My only worry is what happens when swift-collection ships the official conformance. Is the duplicate conformance going to become a compiler error?

Copy link
Member Author

@phausler phausler Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are limiting usage to 1.0.4 so as soon as we upgrade we can remove this

plus iirc the duplicate conformance should be a no-op


struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
private struct SuspendedProducer: Hashable, Sendable {
let id: UInt64
let continuation: UnsafeContinuation<Void, Never>?
let element: Element?
Expand All @@ -29,7 +32,7 @@ struct ChannelStateMachine<Element, Failure: Error>: Sendable {
}
}

private struct SuspendedConsumer: Hashable {
private struct SuspendedConsumer: Hashable, Sendable {
let id: UInt64
let continuation: UnsafeContinuation<Element?, any Error>?

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

private enum State {
private enum State: Sendable {
case channeling(
suspendedProducers: OrderedSet<SuspendedProducer>,
cancelledProducers: Set<SuspendedProducer>,
Expand Down
2 changes: 1 addition & 1 deletion Sources/AsyncAlgorithms/Channels/ChannelStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
struct ChannelStorage<Element, Failure: Error>: Sendable {
struct ChannelStorage<Element: Sendable, Failure: Error>: Sendable {
private let stateMachine: ManagedCriticalState<ChannelStateMachine<Element, Failure>>
private let ids = ManagedCriticalState<UInt64>(0)

Expand Down