Skip to content

Commit fabd45e

Browse files
committed
[concurrency] add resume<E: Error>(with: Result<T, E>) to UnsafeThrowingContinuation
A handy overload that allows one to return a Result<> to a continuation without needing to manually unwrap it in client code. Resolves rdar://71870249
1 parent c4c27f6 commit fabd45e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public struct UnsafeThrowingContinuation<T> {
4747
public func resume(returning: __owned T)
4848
@_silgen_name("swift_continuation_throwingResumeWithError")
4949
public func resume(throwing: __owned Error)
50+
51+
public func resume<E: Error>(with result: Result<T, E>) {
52+
switch result {
53+
case .success(let val):
54+
self.resume(returning: val)
55+
case .failure(let err):
56+
self.resume(throwing: err)
57+
}
58+
}
5059
}
5160

5261
#if _runtime(_ObjC)

test/Concurrency/async_tasks.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ func test_unsafeThrowingContinuations() async {
6262
continuation.resume(throwing: MyError())
6363
}
6464

65+
// using resume(with:)
66+
let _: String = try await withUnsafeThrowingContinuation { continuation in
67+
let result : Result<String, MyError> = .success("")
68+
continuation.resume(with: result)
69+
}
70+
71+
let _: String = try await withUnsafeThrowingContinuation { continuation in
72+
continuation.resume(with: .failure(MyError()))
73+
}
74+
6575
// TODO: Potentially could offer some warnings if we know that a continuation was resumed or escaped at all in a closure?
6676
}
6777

0 commit comments

Comments
 (0)