diff --git a/Sources/Lock/AsyncLock.swift b/Sources/Lock/AsyncLock.swift index ddd4795..f66c118 100644 --- a/Sources/Lock/AsyncLock.swift +++ b/Sources/Lock/AsyncLock.swift @@ -53,10 +53,10 @@ public final class AsyncLock { state.resumeNextContinuation() } - public func withLock( + public func withLock( isolation: isolated (any Actor)? = #isolation, - _ block: @isolated(any) () async throws -> T - ) async rethrows -> T { + _ block: @isolated(any) () async throws(E) -> T + ) async throws(E) -> T { await lock() do { diff --git a/Sources/Lock/AsyncRecursiveLock.swift b/Sources/Lock/AsyncRecursiveLock.swift index 1c26976..948739f 100644 --- a/Sources/Lock/AsyncRecursiveLock.swift +++ b/Sources/Lock/AsyncRecursiveLock.swift @@ -8,10 +8,10 @@ final class AsyncRecursiveLock { public init() { } - public func withLock( + public func withLock( isolation: isolated (any Actor)? = #isolation, - _ block: () async throws -> T - ) async rethrows -> T { + _ block: () async throws(E) -> T + ) async throws(E) -> T { let id = ObjectIdentifier(self) var set = Self.lockedSet @@ -23,17 +23,23 @@ final class AsyncRecursiveLock { return try await block() } - return try await internalLock.withLock { - try await Self.$lockedSet.withValue(set) { - try await block() + return try await internalLock.withLock { () throws(E) -> T in + do { + return try await Self.$lockedSet.withValue(set) { + try await block() + } + } catch { + /* withValue from TaskLocal does not seem to properly throw typed errors (yet?). + * It does rethrows though, so the forced cast should be valid. */ + throw error as! E } } } -// public func withLock( +// public func withLock( // isolation: isolated (any Actor)? = #isolation, -// _ block: () async throws -> T -// ) async rethrows -> T { +// _ block: () async throws(E) -> T +// ) async throws(E) -> T { // if Self.locked { // return try await block() // } @@ -51,7 +57,7 @@ final class AsyncRecursiveLock { // } catch { // internalLock.unlock() // -// throw error +// throw error as! E // } // } }