Remove workaround for compiler bug breaking constrained existential types on iOS 15 release builds
Closes #178
This commit is contained in:
parent
7cadcf1e86
commit
985eb24e88
|
@ -13,20 +13,24 @@ import os
|
||||||
// to make the lock semantics more clear
|
// to make the lock semantics more clear
|
||||||
@available(iOS, obsoleted: 16.0)
|
@available(iOS, obsoleted: 16.0)
|
||||||
class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
||||||
private let lock: LockHolder<[AnyHashable: Any]>
|
private let lock: any Lock<[Key: Value]>
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
self.lock = LockHolder(initialState: [:])
|
if #available(iOS 16.0, *) {
|
||||||
|
self.lock = OSAllocatedUnfairLock(initialState: [:])
|
||||||
|
} else {
|
||||||
|
self.lock = UnfairLock(initialState: [:])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
subscript(key: Key) -> Value? {
|
subscript(key: Key) -> Value? {
|
||||||
get {
|
get {
|
||||||
return try! lock.withLock { dict in
|
return lock.withLock { dict in
|
||||||
dict[key]
|
dict[key]
|
||||||
} as! Value?
|
}
|
||||||
}
|
}
|
||||||
set(value) {
|
set(value) {
|
||||||
_ = try! lock.withLock { dict in
|
_ = lock.withLock { dict in
|
||||||
dict[key] = value
|
dict[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,40 +38,21 @@ class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
||||||
|
|
||||||
/// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread.
|
/// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread.
|
||||||
func removeValue(forKey key: Key) -> Value? {
|
func removeValue(forKey key: Key) -> Value? {
|
||||||
return try! lock.withLock { dict in
|
return lock.withLock { dict in
|
||||||
dict.removeValue(forKey: key)
|
dict.removeValue(forKey: key)
|
||||||
} as! Value?
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(key: Key) -> Bool {
|
func contains(key: Key) -> Bool {
|
||||||
return try! lock.withLock { dict in
|
return lock.withLock { dict in
|
||||||
dict.keys.contains(key)
|
dict.keys.contains(key)
|
||||||
} as! Bool
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this should really be throws/rethrows but the stupid type-erased lock makes that not possible
|
// TODO: this should really be throws/rethrows but the stupid type-erased lock makes that not possible
|
||||||
func withLock<R>(_ body: @Sendable (inout [Key: Value]) -> R) -> R where R: Sendable {
|
func withLock<R>(_ body: @Sendable (inout [Key: Value]) throws -> R) rethrows -> R where R: Sendable {
|
||||||
return try! lock.withLock { dict in
|
return try lock.withLock { dict in
|
||||||
var downcasted = dict as! [Key: Value]
|
return try body(&dict)
|
||||||
defer { dict = downcasted }
|
|
||||||
return body(&downcasted)
|
|
||||||
} as! R
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// this type erased struct is necessary due to a compiler bug with stored constrained existential types
|
|
||||||
// see https://github.com/apple/swift/issues/61403
|
|
||||||
// see #178
|
|
||||||
fileprivate struct LockHolder<State> {
|
|
||||||
let withLock: (_ body: @Sendable (inout State) throws -> any Sendable) throws -> any Sendable
|
|
||||||
|
|
||||||
init(initialState: State) {
|
|
||||||
if #available(iOS 16.0, *) {
|
|
||||||
let lock = OSAllocatedUnfairLock(initialState: initialState)
|
|
||||||
self.withLock = lock.withLock(_:)
|
|
||||||
} else {
|
|
||||||
let lock = UnfairLock(initialState: initialState)
|
|
||||||
self.withLock = lock.withLock(_:)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue