File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Sources/Basics/Concurrency Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ public final class ThreadSafeBox<Value> {
24
24
self . underlying = seed
25
25
}
26
26
27
+ public func mutate( body: ( Value ? ) throws -> Value ? ) rethrows {
28
+ try self . lock. withLock {
29
+ let value = try body ( self . underlying)
30
+ self . underlying = value
31
+ }
32
+ }
33
+
27
34
@discardableResult
28
35
public func memoize( body: ( ) throws -> Value ) rethrows -> Value {
29
36
if let value = self . get ( ) {
@@ -100,4 +107,16 @@ extension ThreadSafeBox where Value == Int {
100
107
}
101
108
}
102
109
110
+ extension ThreadSafeBox where Value == String {
111
+ public func append( _ value: String ) {
112
+ self . mutate { existingValue in
113
+ if let existingValue {
114
+ return existingValue + value
115
+ } else {
116
+ return value
117
+ }
118
+ }
119
+ }
120
+ }
121
+
103
122
extension ThreadSafeBox : @unchecked Sendable where Value: Sendable { }
You can’t perform that action at this time.
0 commit comments