add swiftUI code

This commit is contained in:
zeus
2025-01-22 14:09:10 +08:00
parent 68e7b7347c
commit 8a99853829
2531 changed files with 486215 additions and 0 deletions
@@ -0,0 +1,51 @@
import Dispatch
/// A ReadWriteBox grants multiple readers and single-writer guarantees on a
/// value. It is backed by a concurrent DispatchQueue.
@propertyWrapper
final class ReadWriteBox<T> {
private var _wrappedValue: T
private var queue: DispatchQueue
var wrappedValue: T {
get { read { $0 } }
set { update { $0 = newValue } }
}
var projectedValue: ReadWriteBox<T> { self }
init(wrappedValue: T) {
_wrappedValue = wrappedValue
queue = DispatchQueue(label: "GRDB.ReadWriteBox", attributes: [.concurrent])
}
func read<U>(_ block: (T) throws -> U) rethrows -> U {
try queue.sync {
try block(_wrappedValue)
}
}
func update<U>(_ block: (inout T) throws -> U) rethrows -> U {
try queue.sync(flags: [.barrier]) {
try block(&_wrappedValue)
}
}
}
extension ReadWriteBox where T: Numeric {
@discardableResult
func increment() -> T {
update { n in
n += 1
return n
}
}
@discardableResult
func decrement() -> T {
update { n in
n -= 1
return n
}
}
}