Skip to content

Commit 3569d2d

Browse files
committed
convert to use typed throws
1 parent fa43f78 commit 3569d2d

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

Sources/FoundationEssentials/LockedState.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ package struct LockedState<State> {
113113
return initialState
114114
})
115115
}
116-
117-
package func withLock<T>(_ body: @Sendable (inout State) throws -> T) rethrows -> T {
116+
117+
package func withLock<T, E: Error>(
118+
_ body: (inout sending State) throws(E) -> sending T
119+
) throws(E) -> sending T {
118120
try withLockUnchecked(body)
119121
}
120-
121-
package func withLockUnchecked<T>(_ body: (inout State) throws -> T) rethrows -> T {
122-
try _buffer.withUnsafeMutablePointers { state, lock in
122+
123+
package func withLockUnchecked<T, E: Error>(
124+
_ body: (inout sending State) throws(E) -> sending T
125+
) throws(E) -> sending T {
126+
try _buffer.withUnsafeMutablePointers { (state, lock) throws(E) in
123127
_Lock.lock(lock)
124128
defer { _Lock.unlock(lock) }
125129
return try body(&state.pointee)

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
6666
internal let interopObservationForMonitor: LockedState<(any Sendable)?> = LockedState(initialState: nil)
6767
internal let monitorInterop: LockedState<Bool> = LockedState(initialState: false)
6868

69-
#if FOUNDATION_FRAMEWORK
69+
#if FOUNDATION_FRAMEWORK
7070
internal let parentBridge: LockedState<Foundation.Progress?> = LockedState(initialState: nil) // dummy, set upon calling setParentBridge
71-
#endif
71+
#endif
7272
// Interop properties - Actually set and called
7373
internal let ghostReporter: ProgressManager? // set at init, used to call notify observers
7474
internal let observers: LockedState<[@Sendable (ObserverState) -> Void]> = LockedState(initialState: [])// storage for all observers, set upon calling addObservers
@@ -340,8 +340,10 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
340340
}
341341

342342
/// Mutates any settable properties that convey information about progress.
343-
public func withProperties<T>(_ closure: @Sendable (inout Values) throws -> T) rethrows -> T {
344-
return try state.withLock { state in
343+
public func withProperties<T, E: Error>(
344+
_ closure: (inout sending Values) throws(E) -> sending T
345+
) throws(E) -> sending T {
346+
return try state.withLock { (state) throws(E) -> T in
345347
var values = Values(manager: self, state: state)
346348
// This is done to avoid copy on write later
347349
state = State(fractionState: FractionState(indeterminate: true, selfFraction: _ProgressFraction(), childFraction: _ProgressFraction()), otherProperties: [:], childrenOtherProperties: [:])
@@ -351,24 +353,6 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
351353
}
352354
}
353355

354-
// public func withProperties<T, E: Error>(
355-
// _ closure: (inout sending Values) throws(E) -> sending T
356-
// ) throws(E) -> sending T {
357-
// return try state.withLock { state in
358-
// var values = Values(manager: self, state: state)
359-
// // This is done to avoid copy on write later
360-
// state = State(fractionState: FractionState(indeterminate: true, selfFraction: _ProgressFraction(), childFraction: _ProgressFraction()), otherProperties: [:], childrenOtherProperties: [:])
361-
//
362-
// do {
363-
// let result = try closure(&values)
364-
// state = values.state
365-
// return result
366-
// } catch let localError {
367-
// throw localError as! E
368-
// }
369-
// }
370-
// }
371-
372356
//MARK: ProgressManager Properties getters
373357
/// Returns nil if `self` was instantiated without total units;
374358
/// returns a `Int` value otherwise.
@@ -468,7 +452,7 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
468452

469453
if next.isFinished {
470454
// Remove from children list
471-
// _ = children.withLock { $0.remove(self) }
455+
// _ = children.withLock { $0.remove(self) }
472456

473457
if portion != 0 {
474458
// Update our self completed units
@@ -506,7 +490,7 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
506490
observation = monitorObservation
507491
}
508492
}
509-
493+
510494
internal func setMonitorInterop(to value: Bool) {
511495
monitorInterop.withLock { monitorInterop in
512496
monitorInterop = value
@@ -556,8 +540,10 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
556540
parentReporter.updateChildFraction(from: updates.0, to: updates.1, portion: portionOfParent)
557541
}
558542

559-
internal func getAdditionalProperties<T>(_ closure: @Sendable (Values) throws -> T) rethrows -> T {
560-
try state.withLock { state in
543+
internal func getAdditionalProperties<T, E: Error>(
544+
_ closure: (sending Values) throws(E) -> sending T
545+
) throws(E) -> sending T {
546+
try state.withLock { state throws(E) -> T in
561547
let values = Values(manager: self, state: state)
562548
// No need to modify state since this is read-only
563549
let result = try closure(values)

0 commit comments

Comments
 (0)