Skip to content

Commit 1370f01

Browse files
authored
More support for Swift 6. (#31)
1 parent 39bd400 commit 1370f01

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

[email protected]

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ let package = Package(
2626
"ConcurrencyExtras"
2727
]
2828
),
29-
]
29+
],
30+
swiftLanguageVersions: [.v6]
3031
)
3132

3233
#if !os(Windows)

Sources/ConcurrencyExtras/AsyncStream.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ extension AsyncStream {
5252
/// ```
5353
///
5454
/// - Parameter sequence: An async sequence.
55-
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
55+
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element, S: Sendable {
5656
let lock = NSLock()
57-
var iterator: S.AsyncIterator?
57+
let iterator = UncheckedBox<S.AsyncIterator?>(wrappedValue: nil)
5858
self.init {
5959
lock.withLock {
60-
if iterator == nil {
61-
iterator = sequence.makeAsyncIterator()
60+
if iterator.wrappedValue == nil {
61+
iterator.wrappedValue = sequence.makeAsyncIterator()
6262
}
6363
}
64-
return try? await iterator?.next()
64+
return try? await iterator.wrappedValue?.next()
6565
}
6666
}
6767

@@ -79,7 +79,7 @@ extension AsyncStream {
7979
extension AsyncSequence {
8080
/// Erases this async sequence to an async stream that produces elements till this sequence
8181
/// terminates (or fails).
82-
public func eraseToStream() -> AsyncStream<Element> {
82+
public func eraseToStream() -> AsyncStream<Element> where Self: Sendable {
8383
AsyncStream(self)
8484
}
8585
}

Sources/ConcurrencyExtras/AsyncThrowingStream.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ extension AsyncThrowingStream where Failure == Error {
55
/// terminates, rethrowing any failure.
66
///
77
/// - Parameter sequence: An async sequence.
8-
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element {
8+
public init<S: AsyncSequence>(_ sequence: S) where S.Element == Element, S: Sendable {
99
let lock = NSLock()
10-
var iterator: S.AsyncIterator?
10+
let iterator = UncheckedBox<S.AsyncIterator?>(wrappedValue: nil)
1111
self.init {
1212
lock.withLock {
13-
if iterator == nil {
14-
iterator = sequence.makeAsyncIterator()
13+
if iterator.wrappedValue == nil {
14+
iterator.wrappedValue = sequence.makeAsyncIterator()
1515
}
1616
}
17-
return try await iterator?.next()
17+
return try await iterator.wrappedValue?.next()
1818
}
1919
}
2020

@@ -34,7 +34,7 @@ extension AsyncThrowingStream where Failure == Error {
3434
extension AsyncSequence {
3535
/// Erases this async sequence to an async throwing stream that produces elements till this
3636
/// sequence terminates, rethrowing any error on failure.
37-
public func eraseToThrowingStream() -> AsyncThrowingStream<Element, Error> {
37+
public func eraseToThrowingStream() -> AsyncThrowingStream<Element, Error> where Self: Sendable {
3838
AsyncThrowingStream(self)
3939
}
4040
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
final class UncheckedBox<Value>: @unchecked Sendable {
2+
var wrappedValue: Value
3+
init(wrappedValue: Value) {
4+
self.wrappedValue = wrappedValue
5+
}
6+
}

Sources/ConcurrencyExtras/LockIsolated.swift

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ extension LockIsolated where Value: Sendable {
9393
}
9494
}
9595

96+
#if swift(<6)
9697
@available(*, deprecated, message: "Lock isolated values should not be equatable")
9798
extension LockIsolated: Equatable where Value: Equatable {
9899
public static func == (lhs: LockIsolated, rhs: LockIsolated) -> Bool {
@@ -106,6 +107,7 @@ extension LockIsolated: Hashable where Value: Hashable {
106107
hasher.combine(self.value)
107108
}
108109
}
110+
#endif
109111

110112
extension NSRecursiveLock {
111113
@inlinable @discardableResult

Sources/ConcurrencyExtras/MainSerialExecutor.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@
8181
private typealias Hook = @convention(thin) (UnownedJob, Original) -> Void
8282

8383
private var swift_task_enqueueGlobal_hook: Hook? {
84-
get { _swift_task_enqueueGlobal_hook.pointee }
85-
set { _swift_task_enqueueGlobal_hook.pointee = newValue }
84+
get { _swift_task_enqueueGlobal_hook.wrappedValue.pointee }
85+
set { _swift_task_enqueueGlobal_hook.wrappedValue.pointee = newValue }
8686
}
87-
private let _swift_task_enqueueGlobal_hook: UnsafeMutablePointer<Hook?> =
87+
private let _swift_task_enqueueGlobal_hook = UncheckedSendable(
8888
dlsym(dlopen(nil, 0), "swift_task_enqueueGlobal_hook").assumingMemoryBound(to: Hook?.self)
89+
)
8990
#endif

0 commit comments

Comments
 (0)