Skip to content

Commit bce9970

Browse files
authored
Fix concurrency warnings (#7)
1 parent 5025815 commit bce9970

5 files changed

Lines changed: 36 additions & 30 deletions

File tree

Sources/CompressionDependency/Compressor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public struct Compressor: Sendable {
4646
/// Compresses the provided data synchronously, using the provided algorithm.
4747
public func callAsFunction(
4848
_ data: Data,
49-
using algorithm: Algorithm = .zlib
49+
using algorithm: @Sendable @autoclosure () -> Algorithm = .zlib
5050
) throws -> Data {
51-
try self.compress(data, algorithm)
51+
try self.compress(data, algorithm())
5252
}
5353
/// Compresses the provided data asynchronously, using the provided algorithm.
5454
public func callAsFunction(
5555
_ data: Data,
56-
using algorithm: Algorithm = .zlib
56+
using algorithm: @Sendable @autoclosure () -> Algorithm = .zlib
5757
) async throws -> Data {
58-
try await self.compressAsync(data, algorithm)
58+
try await self.compressAsync(data, algorithm())
5959
}
6060
}

Sources/CompressionDependency/Decompressor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ public struct Decompressor: Sendable {
4949
/// Decompresses the provided data synchronously, using the provided algorithm.
5050
public func callAsFunction(
5151
_ data: Data,
52-
using algorithm: Algorithm = .zlib
52+
using algorithm: @Sendable @autoclosure () -> Algorithm = .zlib
5353
) throws -> Data {
54-
try self.decompress(data, algorithm)
54+
try self.decompress(data, algorithm())
5555
}
5656

5757
/// Decompresses the provided data asynchronously, using the provided algorithm.
5858
public func callAsFunction(
5959
_ data: Data,
60-
using algorithm: Algorithm = .zlib
60+
using algorithm: @Sendable @autoclosure () -> Algorithm = .zlib
6161
) async throws -> Data {
62-
try await self.decompressAsync(data, algorithm)
62+
try await self.decompressAsync(data, algorithm())
6363
}
6464
}

Sources/DependenciesAdditionsBasics/Proxies.swift

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public struct ReadWriteBinding<Value>: Sendable {
121121
self.set = getSet.1
122122
}
123123
/// Initializes a ``ReadWriteProxy`` from a ``ProxyBindable`` value, like `LockIsolated`.
124-
public init<Bindable: ProxyBindable>(_ bindable: Bindable) where Bindable.Value == Value {
124+
public init<Bindable: ProxyBindable & Sendable>(_ bindable: Bindable) where Bindable.Value == Value {
125125
self.init(
126126
get: {
127127
bindable.getValue()
@@ -133,7 +133,7 @@ public struct ReadWriteBinding<Value>: Sendable {
133133
}
134134

135135
/// Initializes a ``ReadWriteProxy`` from a ``ProxyBindable`` value, like `LockIsolated`.
136-
public static func bind<Bindable: ProxyBindable>(bindable: Bindable) -> Self
136+
public static func bind<Bindable: ProxyBindable & Sendable>(bindable: Bindable) -> Self
137137
where Bindable.Value == Value {
138138
.init(bindable)
139139
}
@@ -299,7 +299,7 @@ public struct MainActorReadWriteBinding<Value>: Sendable {
299299
self.set = getSet.1
300300
}
301301

302-
public init<Bindable: ProxyBindable>(_ bindable: Bindable) where Bindable.Value == Value {
302+
public init<Bindable: ProxyBindable & Sendable>(_ bindable: Bindable) where Bindable.Value == Value {
303303
self.init(
304304
get: {
305305
bindable.getValue()
@@ -422,23 +422,24 @@ public struct MainActorReadOnlyProxy<Value: Sendable>: Sendable {
422422
///
423423
/// `LockIsolated` is conforming to this protocol, so you can use `LockIsolated` values
424424
/// to control writable dependencies properties during tests and SwiftUI previews.
425-
@dynamicMemberLookup
425+
//@dynamicMemberLookup
426426
public protocol ProxyBindable {
427427
associatedtype Value: Sendable
428428
var getValue: @Sendable () -> Value { get }
429429
var setValue: @Sendable (Value) -> Void { get }
430430
}
431431

432-
extension ProxyBindable {
433-
public subscript(dynamicMember keyPath: ReferenceWritableKeyPath<Self, Value>)
434-
-> AnyProxyBindable<Value>
435-
{
436-
return AnyProxyBindable<Value>(
437-
getValue: { self[keyPath: keyPath] },
438-
setValue: { self[keyPath: keyPath] = $0 }
439-
)
440-
}
441-
}
432+
// Removed for now until a proper way to handle the non-sendable capture is found
433+
//extension ProxyBindable {
434+
// public subscript(dynamicMember keyPath: ReferenceWritableKeyPath<Self, Value>)
435+
// -> AnyProxyBindable<Value>
436+
// {
437+
// return AnyProxyBindable<Value>(
438+
// getValue: { self[keyPath: keyPath] },
439+
// setValue: { self[keyPath: keyPath] = $0 }
440+
// )
441+
// }
442+
//}
442443

443444
/// An erased ``ProxyBindable`` property.
444445
///
@@ -447,12 +448,13 @@ extension ProxyBindable {
447448
///
448449
/// For example, `LockIsolated(["a", "b", "c"]).count` returns an ``AnyProxyBindable<Int>``
449450
/// that you can bind to a writable dependency.
450-
public struct AnyProxyBindable<Value>: ProxyBindable {
451+
public struct AnyProxyBindable<Value: Sendable>: ProxyBindable {
451452
public var getValue: @Sendable () -> Value
452453
public var setValue: @Sendable (Value) -> Void
453454
}
454455

455-
extension LockIsolated: ProxyBindable {
456+
public protocol _Sendable: Sendable {}
457+
extension LockIsolated: ProxyBindable where Value: _Sendable {
456458
public var getValue: @Sendable () -> Value {
457459
{ self.value }
458460
}

Sources/NotificationCenterDependency/NotificationCenterDependency.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ extension NotificationCenter {
5858
>
5959

6060
init(
61-
post: @escaping @Sendable (
61+
@_inheritActorContext post: @escaping @Sendable (
6262
NSNotification.Name, AnyObject?, [AnyHashable: Any]?, StaticString, UInt
6363
) -> Void,
64-
addObserver: @escaping @Sendable (
64+
@_inheritActorContext addObserver: @escaping @Sendable (
6565
AnyObject, Selector, NSNotification.Name?, AnyObject?, StaticString, UInt
6666
) -> Void,
67-
removeObserver: @escaping @Sendable (
67+
@_inheritActorContext removeObserver: @escaping @Sendable (
6868
AnyObject, NSNotification.Name?, AnyObject?, StaticString, UInt
6969
) -> Void,
70-
publisher: @escaping @Sendable (Notification.Name, AnyObject?, StaticString, UInt) ->
70+
@_inheritActorContext publisher: @escaping @Sendable (Notification.Name, AnyObject?, StaticString, UInt) ->
7171
AnyPublisher<Notification, Never>
7272
) {
7373
self._post = post

Tests/NotificationCenterDependencyTests/NotificationCenterDependencyTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import Dependencies
22
@_spi(Internals) import DependenciesAdditionsBasics
33
import NotificationCenterDependency
44
import XCTest
5-
65
@MainActor
76
final class NotificationCenterDependencyTests: XCTestCase {
7+
nonisolated
88
func notificationName1() -> Notification.Name { .init("NotificationCenterDependencyTests_1") }
9+
nonisolated
910
func notificationName2() -> Notification.Name { .init("NotificationCenterDependencyTests_2") }
1011

1112
// TODO: Strenghten this
@@ -20,7 +21,10 @@ final class NotificationCenterDependencyTests: XCTestCase {
2021
await withTimeout { group in
2122
group.addTask {
2223
var count = 0
23-
for await notification in notificationCenter.notifications(named: n1) {
24+
let notifications = await MainActor.run{
25+
UncheckedSendable(notificationCenter.notifications(named: n1))
26+
}.value
27+
for await notification in notifications {
2428
XCTAssertEqual(notification.name, n1)
2529
count += 1
2630
if count == 3 { break }

0 commit comments

Comments
 (0)