Skip to content

Commit 447278e

Browse files
authored
Use DefaultKey.Base as lookup (#122)
Otherwise it is possible for 2 keys to point to the same underlying storage but get out of sync. Fixes #121.
1 parent 6060619 commit 447278e

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ jobs:
5151
run: |
5252
PREFIX=/opt/swift
5353
set -ex
54-
curl -f -o /tmp/swift.tar.gz "https://download.swift.org/swift-6.0.2-release/ubuntu2204/swift-6.0.2-RELEASE/swift-6.0.2-RELEASE-ubuntu22.04.tar.gz"
54+
curl -f -o /tmp/swift.tar.gz "https://download.swift.org/swift-6.0.3-release/ubuntu2204/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu22.04.tar.gz"
5555
sudo mkdir -p $PREFIX; sudo tar -xzf /tmp/swift.tar.gz -C $PREFIX --strip-component 1
56-
$PREFIX/usr/bin/swift sdk install https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.2-RELEASE/swift-wasm-6.0.2-RELEASE-wasm32-unknown-wasi.artifactbundle.zip --checksum 6ffedb055cb9956395d9f435d03d53ebe9f6a8d45106b979d1b7f53358e1dcb4
56+
$PREFIX/usr/bin/swift sdk install https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.3-RELEASE/swift-wasm-6.0.3-RELEASE-wasm32-unknown-wasi.artifactbundle.zip --checksum 31d3585b06dd92de390bacc18527801480163188cd7473f492956b5e213a8618
5757
echo "$PREFIX/usr/bin" >> $GITHUB_PATH
5858
5959
- name: Build

Sources/Sharing/SharedKey.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension Shared {
6666
/// - Parameter key: A shared key associated with the shared reference. It is responsible for
6767
/// loading and saving the shared reference's value from some external source.
6868
public init(_ key: (some SharedKey<Value>).Default) {
69-
self.init(wrappedValue: key.initialValue, key)
69+
self.init(wrappedValue: key.initialValue, key.base)
7070
}
7171

7272
/// Creates a shared reference to a value using a shared key by overriding its default value.
@@ -76,12 +76,11 @@ extension Shared {
7676
/// shared key.
7777
/// - key: A shared key associated with the shared reference. It is responsible for loading
7878
/// and saving the shared reference's value from some external source.
79-
@_disfavoredOverload
8079
public init(
8180
wrappedValue: @autoclosure () -> Value,
8281
_ key: (some SharedKey<Value>).Default
8382
) {
84-
self.init(wrappedValue: wrappedValue(), key)
83+
self.init(wrappedValue: wrappedValue(), key.base)
8584
}
8685

8786
/// Replaces a shared reference's key and attempts to load its value.

Sources/Sharing/SharedReaderKey.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extension SharedReader {
117117
/// - Parameter key: A shared key associated with the shared reference. It is responsible for
118118
/// loading the shared reference's value from some external source.
119119
public init(_ key: (some SharedReaderKey<Value>).Default) {
120-
self.init(wrappedValue: key.initialValue, key)
120+
self.init(wrappedValue: key.initialValue, key.base)
121121
}
122122

123123
@_disfavoredOverload
@@ -134,21 +134,19 @@ extension SharedReader {
134134
/// shared key.
135135
/// - key: A shared key associated with the shared reference. It is responsible for loading the
136136
/// shared reference's value from some external source.
137-
@_disfavoredOverload
138137
public init(
139138
wrappedValue: @autoclosure () -> Value,
140139
_ key: (some SharedReaderKey<Value>).Default
141140
) {
142-
self.init(wrappedValue: wrappedValue(), key)
141+
self.init(wrappedValue: wrappedValue(), key.base)
143142
}
144143

145-
@_disfavoredOverload
146144
@_documentation(visibility: private)
147145
public init(
148146
wrappedValue: @autoclosure () -> Value,
149147
_ key: (some SharedKey<Value>).Default
150148
) {
151-
self.init(wrappedValue: wrappedValue(), key)
149+
self.init(wrappedValue: wrappedValue(), key.base)
152150
}
153151

154152
/// Replaces a shared reference's key and attempts to load its value.

Tests/SharingTests/DefaultTests.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,84 @@ import Testing
8888
@Test func initialValue() {
8989
#expect(InMemoryKey<Bool>.Default.isOn.initialValue == false)
9090
}
91+
92+
@Suite struct DefaultNotDefaultInterplay {
93+
@Test func writerDefault_readerDefault() {
94+
@Shared(.count1) var count
95+
@SharedReader(.count2) var countReader
96+
97+
$count.withLock { $0 += 1 }
98+
99+
#expect(count == 2)
100+
#expect(countReader == 2)
101+
}
102+
103+
@Test func readerDefault_writerDefault() {
104+
@SharedReader(.count2) var countReader
105+
@Shared(.count1) var count
106+
107+
$count.withLock { $0 += 1 }
108+
109+
#expect(count == 3)
110+
#expect(countReader == 3)
111+
}
112+
113+
@Test func writer_readerDefault() {
114+
@Shared(.count1) var count = 3
115+
@SharedReader(.count2) var countReader
116+
117+
$count.withLock { $0 += 1 }
118+
119+
#expect(count == 4)
120+
#expect(countReader == 4)
121+
}
122+
123+
@Test func writerDefault_reader() {
124+
@Shared(.count1) var count
125+
@SharedReader(.count2) var countReader = 3
126+
127+
$count.withLock { $0 += 1 }
128+
129+
#expect(count == 2)
130+
#expect(countReader == 2)
131+
}
132+
133+
@Test func reader_writerDefault() {
134+
@SharedReader(.count2) var countReader = 3
135+
@Shared(.count1) var count
136+
137+
$count.withLock { $0 += 1 }
138+
139+
#expect(count == 4)
140+
#expect(countReader == 4)
141+
}
142+
143+
@Test func readerDefault_writer() {
144+
@SharedReader(.count2) var countReader
145+
@Shared(.count1) var count = 3
146+
147+
$count.withLock { $0 += 1 }
148+
149+
#expect(count == 3)
150+
#expect(countReader == 3)
151+
}
152+
}
91153
}
92154

93155
extension SharedReaderKey where Self == InMemoryKey<Bool>.Default {
94156
fileprivate static var isOn: Self {
95157
Self[.inMemory("isOn"), default: false]
96158
}
97159
}
160+
161+
extension SharedKey where Self == InMemoryKey<Int>.Default {
162+
fileprivate static var count1: Self {
163+
Self[.inMemory("count"), default: 1]
164+
}
165+
}
166+
167+
extension SharedReaderKey where Self == InMemoryKey<Int>.Default {
168+
fileprivate static var count2: Self {
169+
Self[.inMemory("count"), default: 2]
170+
}
171+
}

Tests/SharingTests/SharedTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ import Testing
185185

186186
#expect(
187187
$count.description == """
188-
Shared<Int>(AppStorageKey<Int>.Default[.appStorage("count"), default: 0])
188+
Shared<Int>(.appStorage("count"))
189189
"""
190190
)
191191

192192
#expect(
193193
SharedReader($count).description == """
194-
SharedReader<Int>(AppStorageKey<Int>.Default[.appStorage("count"), default: 0])
194+
SharedReader<Int>(.appStorage("count"))
195195
"""
196196
)
197197
}
@@ -200,13 +200,13 @@ import Testing
200200

201201
#expect(
202202
$count.description == """
203-
Shared<Int>(AppStorageKey<Int>.Default[.appStorage("count"), default: 0])
203+
Shared<Int>(.appStorage("count"))
204204
"""
205205
)
206206

207207
#expect(
208208
SharedReader($count).description == """
209-
SharedReader<Int>(AppStorageKey<Int>.Default[.appStorage("count"), default: 0])
209+
SharedReader<Int>(.appStorage("count"))
210210
"""
211211
)
212212
}

0 commit comments

Comments
 (0)