Skip to content

Commit 9217854

Browse files
authored
Adopt NonisolatedNonsendingByDefault (#604)
1 parent 4db2fde commit 9217854

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import PackageDescription
33

44
#if compiler(>=6.1)
5-
let swiftSettings: [SwiftSetting] = []
5+
let swiftSettings: [SwiftSetting] = [
6+
.enableUpcomingFeature("NonisolatedNonsendingByDefault")
7+
]
68
#else
79
let swiftSettings: [SwiftSetting] = [
810
// Sadly the 6.0 compiler concurrency checker finds false positives.

Sources/ConnectionPoolModule/ConnectionPool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,20 +578,20 @@ protocol TaskGroupProtocol {
578578
// We need to call this `addTask_` because some Swift versions define this
579579
// under exactly this name and others have different attributes. So let's pick
580580
// a name that doesn't clash anywhere and implement it using the standard `addTask`.
581-
mutating func addTask_(operation: @escaping @Sendable () async -> Void)
581+
mutating func addTask_(operation: @isolated(any) @escaping @Sendable () async -> Void)
582582
}
583583

584584
@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
585585
extension DiscardingTaskGroup: TaskGroupProtocol {
586586
@inlinable
587-
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
587+
mutating func addTask_(operation: @isolated(any) @escaping @Sendable () async -> Void) {
588588
self.addTask(priority: nil, operation: operation)
589589
}
590590
}
591591

592592
extension TaskGroup<Void>: TaskGroupProtocol {
593593
@inlinable
594-
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
594+
mutating func addTask_(operation: @isolated(any) @escaping @Sendable () async -> Void) {
595595
self.addTask(priority: nil, operation: operation)
596596
}
597597
}

Sources/PostgresNIO/New/PostgresNotificationSequence.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@ public struct PostgresNotification: Sendable {
66
public struct PostgresNotificationSequence: AsyncSequence, Sendable {
77
public typealias Element = PostgresNotification
88

9-
let base: AsyncThrowingStream<PostgresNotification, Error>
9+
let base: AsyncThrowingStream<PostgresNotification, any Error>
1010

1111
public func makeAsyncIterator() -> AsyncIterator {
1212
AsyncIterator(base: self.base.makeAsyncIterator())
1313
}
1414

1515
public struct AsyncIterator: AsyncIteratorProtocol {
16-
var base: AsyncThrowingStream<PostgresNotification, Error>.AsyncIterator
16+
var base: AsyncThrowingStream<PostgresNotification, any Error>.AsyncIterator
1717

18+
#if compiler(>=6.2)
19+
@concurrent
1820
public mutating func next() async throws -> Element? {
1921
try await self.base.next()
2022
}
23+
#else
24+
public mutating func next() async throws -> Element? {
25+
try await self.base.next()
26+
}
27+
#endif
28+
29+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
30+
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Self.Failure) -> Element? {
31+
try await self.base.next(isolation: actor)
32+
}
2133
}
2234
}
2335

Sources/PostgresNIO/New/PostgresRowSequence.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public struct PostgresRowSequence: AsyncSequence, Sendable {
3131
extension PostgresRowSequence {
3232
public struct AsyncIterator: AsyncIteratorProtocol {
3333
public typealias Element = PostgresRow
34+
public typealias Failure = any Error
3435

3536
let backing: BackingSequence.AsyncIterator
3637

@@ -43,7 +44,9 @@ extension PostgresRowSequence {
4344
self.columns = columns
4445
}
4546

46-
public mutating func next() async throws -> PostgresRow? {
47+
#if compiler(>=6.2)
48+
@concurrent
49+
public mutating func next() async throws -> Element? {
4750
if let dataRow = try await self.backing.next() {
4851
return PostgresRow(
4952
data: dataRow,
@@ -53,6 +56,36 @@ extension PostgresRowSequence {
5356
}
5457
return nil
5558
}
59+
#else
60+
public mutating func next() async throws -> Element? {
61+
if let dataRow = try await self.backing.next() {
62+
return PostgresRow(
63+
data: dataRow,
64+
lookupTable: self.lookupTable,
65+
columns: self.columns
66+
)
67+
}
68+
return nil
69+
}
70+
#endif
71+
72+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
73+
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Self.Failure) -> PostgresRow? {
74+
// Since the underlying NIOThrowingAsyncSequenceProducer<DataRow, Error, AdaptiveRowBuffer, PSQLRowStream>.AsyncIterator
75+
// does not supported the next(isolation:) call yet, we will hop here back and forth.
76+
struct UnsafeTransfer: @unchecked Sendable {
77+
var backing: BackingSequence.AsyncIterator
78+
}
79+
let unsafeTransfer = UnsafeTransfer(backing: self.backing)
80+
if let dataRow = try await unsafeTransfer.backing.next() {
81+
return PostgresRow(
82+
data: dataRow,
83+
lookupTable: self.lookupTable,
84+
columns: self.columns
85+
)
86+
}
87+
return nil
88+
}
5689
}
5790
}
5891

0 commit comments

Comments
 (0)