Skip to content

Commit 40129da

Browse files
authored
Add deprecated Shared.read overload, swift-format (#146)
Just to avoid strange usage. In the process I noticed swift-format missing from our infrastructure, so sneaking it in now.
1 parent 5a2a704 commit 40129da

File tree

10 files changed

+71
-25
lines changed

10 files changed

+71
-25
lines changed

.github/workflows/format.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Format
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
concurrency:
9+
group: format-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
swift_format:
14+
name: swift-format
15+
runs-on: macos-15
16+
permissions:
17+
contents: write
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Select Xcode 16.2
21+
run: sudo xcode-select -s /Applications/Xcode_16.2.app
22+
- name: Format
23+
run: make format
24+
- uses: stefanzweifel/git-auto-commit-action@v5
25+
with:
26+
commit_message: Run swift-format
27+
branch: 'main'

Sources/Sharing/Shared.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,25 @@ public struct Shared<Value> {
223223
reference = newValue.reference
224224
}
225225
}
226-
226+
227227
/// Returns a read-only shared reference to the resulting value of a given closure.
228228
///
229229
/// - Returns: A new read-only shared reference.
230230
public func read<Member>(
231-
_ body: @escaping @Sendable(Value) -> Member
231+
_ body: @escaping @Sendable (Value) -> Member
232232
) -> SharedReader<Member> {
233233
SharedReader(self).read(body)
234234
}
235235

236+
@available(
237+
*,
238+
deprecated,
239+
message: "Use dynamic member lookup instead ('$shared.member', not '$shared.read(\\.member)')"
240+
)
241+
public func read<Member>(_ keyPath: KeyPath<Value, Member>) -> SharedReader<Member> {
242+
self[dynamicMember: keyPath]
243+
}
244+
236245
/// Returns a shared reference to the resulting value of a given key path.
237246
///
238247
/// You don't call this subscript directly. Instead, Swift calls it for you when you access a

Sources/Sharing/SharedKeys/AppStorageKey.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -559,20 +559,20 @@
559559
}
560560
}
561561

562-
extension UserDefaults {
563-
public static var inMemory: UserDefaults {
564-
let suiteName: String
565-
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
566-
// file-based suites. Go back to using temporary directory always when we drop iOS 16
567-
// support.
568-
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
569-
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
570-
} else {
571-
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
572-
}
573-
return UserDefaults(suiteName: suiteName)!
562+
extension UserDefaults {
563+
public static var inMemory: UserDefaults {
564+
let suiteName: String
565+
// NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using
566+
// file-based suites. Go back to using temporary directory always when we drop iOS 16
567+
// support.
568+
if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) {
569+
suiteName = "co.pointfree.Sharing.\(UUID().uuidString)"
570+
} else {
571+
suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)"
572+
}
573+
return UserDefaults(suiteName: suiteName)!
574+
}
574575
}
575-
}
576576

577577
private enum AppStorageKeyFormatWarningEnabledKey: DependencyKey {
578578
static let liveValue = true

Sources/Sharing/SharedReader.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public struct SharedReader<Value> {
155155
reference = newValue.reference
156156
}
157157
}
158-
158+
159159
/// Returns a read-only shared reference to the resulting value of a given closure.
160160
///
161161
/// - Returns: A new shared reader.
@@ -170,6 +170,15 @@ public struct SharedReader<Value> {
170170
return open(reference)
171171
}
172172

173+
@available(
174+
*,
175+
deprecated,
176+
message: "Use dynamic member lookup instead ('$shared.member', not '$shared.read(\\.member)')"
177+
)
178+
public func read<Member>(_ keyPath: KeyPath<Value, Member>) -> SharedReader<Member> {
179+
self[dynamicMember: keyPath]
180+
}
181+
173182
/// Returns a read-only shared reference to the resulting value of a given key path.
174183
///
175184
/// You don't call this subscript directly. Instead, Swift calls it for you when you access a

Tests/SharingTests/ContinuationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Testing
21
import Sharing
2+
import Testing
33

44
@Suite struct ContinuationTests {
55
@Test func dontResumeLoadContinuation() async throws {

Tests/SharingTests/EquatableTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct EquatableTests {
3232
#expect(lhs == rhs)
3333
#expect($lhs == $rhs)
3434
}
35-
35+
3636
@Test func mapReader() {
3737
@Shared(value: 0) var base: Int
3838
@SharedReader var lhs: Int
@@ -41,7 +41,7 @@ struct EquatableTests {
4141
_rhs = $base.read { $0 * 3 }
4242
#expect(lhs == rhs)
4343
#expect($lhs == $rhs)
44-
44+
4545
$base.withLock { $0 += 1 }
4646
#expect(lhs != rhs)
4747
#expect($lhs != $rhs)

Tests/SharingTests/ErrorThrowingTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Testing
4343
@SharedReader(Key()) var count = 0
4444
#expect($count.loadError != nil)
4545
}
46-
46+
4747
@Test func userInitiatedLoadError() async {
4848
struct LoadError: Error {}
4949
struct Key: Hashable, Sendable, SharedReaderKey {
@@ -62,9 +62,9 @@ import Testing
6262
SharedSubscription {}
6363
}
6464
}
65-
65+
6666
@SharedReader(Key()) var value = 0
67-
67+
6868
await #expect(throws: LoadError.self) {
6969
try await $value.load()
7070
}

Tests/SharingTests/InMemoryTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ import Testing
8686
@Shared(.inMemory("nestedOptionalCount")) var nestedOptionalCount: Int?? = .some(.none)
8787
#expect(nestedOptionalCount == .some(.none))
8888

89-
@SharedReader(.inMemory("nestedOptionalCountReader")) var nestedOptionalCountReader: Int?? = .some(.none)
89+
@SharedReader(.inMemory("nestedOptionalCountReader")) var nestedOptionalCountReader: Int?? =
90+
.some(.none)
9091
#expect(nestedOptionalCountReader == .some(.none))
9192
}
9293

Tests/SharingTests/SharedChangeTrackerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import Testing
9898
$count.withLock { $0 = 1 }
9999

100100
#expect($count == $count)
101-
#expect(counts.withLock(\.self) == [0 ,1])
101+
#expect(counts.withLock(\.self) == [0, 1])
102102
}
103103
}
104104

Tests/SharingTests/SharedTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import Testing
8787

8888
#expect(id == 42)
8989
}
90-
90+
9191
@Test func mapReader() {
9292
@Shared(value: 0) var count
9393
@SharedReader var isZero: Bool

0 commit comments

Comments
 (0)