Skip to content

Commit 55f631f

Browse files
committed
rename generic parameters
1 parent e2bf1f7 commit 55f631f

File tree

1 file changed

+72
-11
lines changed

1 file changed

+72
-11
lines changed

Sources/CacheContainer.swift

+72-11
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,35 @@ public final class CacheContainer: Sendable {
4747
}
4848
}
4949

50-
final class ThreadSafeDictionary<V: Hashable & Sendable, T: Sendable>: Collection, @unchecked Sendable {
51-
private var dictionary: [V : T]
50+
final class ThreadSafeDictionary<Key: Hashable & Sendable, Value: Sendable>: Collection, @unchecked Sendable {
51+
private var dictionary: [Key : Value]
5252
private let concurrentQueue = DispatchQueue(label: "Dictionary Barrier Queue", attributes: .concurrent)
5353

54-
var keys: Dictionary<V, T>.Keys {
54+
var keys: Dictionary<Key, Value>.Keys {
5555
concurrentQueue.sync { dictionary.keys }
5656
}
5757

58-
var values: Dictionary<V, T>.Values {
58+
var values: Dictionary<Key, Value>.Values {
5959
concurrentQueue.sync { dictionary.values }
6060
}
6161

62-
var startIndex: Dictionary<V, T>.Index {
62+
var startIndex: Dictionary<Key, Value>.Index {
6363
concurrentQueue.sync { dictionary.startIndex }
6464
}
6565

66-
var endIndex: Dictionary<V, T>.Index {
66+
var endIndex: Dictionary<Key, Value>.Index {
6767
concurrentQueue.sync { dictionary.endIndex }
6868
}
6969

70-
init(dictionary: [V : T] = [:]) {
70+
init(dictionary: [Key : Value] = [:]) {
7171
self.dictionary = dictionary
7272
}
7373

74-
func index(after i: Dictionary<V, T>.Index) -> Dictionary<V, T>.Index {
74+
func index(after i: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Index {
7575
concurrentQueue.sync { dictionary.index(after: i) }
7676
}
7777

78-
subscript(key: V) -> T? {
78+
subscript(key: Key) -> Value? {
7979
set(newValue) {
8080
concurrentQueue.async(flags: .barrier) { [weak self] in
8181
self?.dictionary[key] = newValue
@@ -86,13 +86,74 @@ final class ThreadSafeDictionary<V: Hashable & Sendable, T: Sendable>: Collectio
8686
}
8787
}
8888

89-
subscript(index: Dictionary<V, T>.Index) -> Dictionary<V, T>.Element {
89+
subscript(index: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Element {
9090
concurrentQueue.sync { dictionary[index] }
9191
}
9292

93-
func removeValue(forKey key: V) {
93+
func removeValue(forKey key: Key) {
9494
concurrentQueue.async(flags: .barrier) { [weak self] in
9595
self?.dictionary.removeValue(forKey: key)
9696
}
9797
}
9898
}
99+
100+
101+
102+
struct ThreadSafeDictionary2<Key: Hashable & Sendable, Value: Sendable> {
103+
private let dictionary: Mutex<[Key: Value]>
104+
105+
init(dictionary: [Key : Value] = [:]) {
106+
self.dictionary = Mutex(dictionary)
107+
}
108+
109+
var keys: Dictionary<Key, Value>.Keys {
110+
dictionary.withLock(\.keys)
111+
}
112+
113+
var values: Dictionary<Key, Value>.Values {
114+
dictionary.withLock(\.values)
115+
}
116+
117+
subscript(key: Key) -> Value? {
118+
get { dictionary.withLock(\.[key]) }
119+
set { dictionary.withLock { $0[key] = newValue } }
120+
}
121+
122+
@discardableResult
123+
func removeValue(forKey key: Key) -> Value? {
124+
dictionary.withLock { $0.removeValue(forKey: key) }
125+
}
126+
}
127+
128+
extension ThreadSafeDictionary2: Collection {
129+
var startIndex: Dictionary<Key, Value>.Index {
130+
dictionary.withLock(\.startIndex)
131+
}
132+
133+
var endIndex: Dictionary<Key, Value>.Index {
134+
dictionary.withLock(\.endIndex)
135+
}
136+
137+
subscript(index: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Element {
138+
dictionary.withLock(\.[index])
139+
}
140+
141+
func index(after i: Dictionary<Key, Value>.Index) -> Dictionary<Key, Value>.Index {
142+
dictionary.withLock { $0.index(after: i) }
143+
}
144+
}
145+
146+
final class Mutex<Value: ~Copyable>: @unchecked Sendable {
147+
private let lock = NSLock()
148+
private var value: Value
149+
150+
init(_ initialValue: consuming Value) {
151+
self.value = initialValue
152+
}
153+
154+
func withLock<Result: ~Copyable, E: Error>(_ body: (inout Value) throws(E) -> Result) throws(E) -> Result {
155+
lock.lock()
156+
defer { lock.unlock() }
157+
return try body(&value)
158+
}
159+
}

0 commit comments

Comments
 (0)