|
1 | | -/// Suspends the current task, then calls the given closure with a throwing continuation for the current task. |
2 | | -/// Continuation is cancelled with error if current task is cancelled and cancellation handler is immediately invoked. |
3 | | -/// |
4 | | -/// This operation cooperatively checks for cancellation and reacting to it by cancelling the throwing continuation with an error |
5 | | -/// and the cancellation handler is always and immediately invoked after that. |
6 | | -/// For example, even if the operation is running code that never checks for cancellation, |
7 | | -/// a cancellation handler still runs and provides a chance to run some cleanup code. |
8 | | -/// |
9 | | -/// - Parameters: |
10 | | -/// - handler: A closure that is called after cancelling continuation. |
11 | | -/// You must not resume the continuation in closure. |
12 | | -/// - fn: A closure that takes a throwing continuation parameter. |
13 | | -/// You must resume the continuation exactly once. |
14 | | -/// |
15 | | -/// - Returns: The value passed to the continuation. |
16 | | -/// - Throws: If `resume(throwing:)` is called on the continuation, this function throws that error. |
17 | | -/// |
18 | | -/// - Important: The continuation provided in cancellation handler is already resumed with cancellation error. |
19 | | -/// Trying to resume the continuation here will cause runtime error/unexpected behavior. |
20 | | -func withThrowingContinuationCancellationHandler<C: ThrowingContinuable>( |
21 | | - handler: @Sendable (C) -> Void, |
22 | | - _ fn: (C) -> Void |
23 | | -) async throws -> C.Success where C.Failure == Error { |
24 | | - let wrapper = ContinuationWrapper<C>() |
25 | | - let value = try await withTaskCancellationHandler { |
26 | | - guard let continuation = wrapper.value else { return } |
27 | | - wrapper.cancel(withError: CancellationError()) |
28 | | - handler(continuation) |
29 | | - } operation: { () -> C.Success in |
30 | | - let value = try await C.with { continuation in |
31 | | - wrapper.value = continuation |
32 | | - fn(continuation) |
33 | | - } |
34 | | - return value |
35 | | - } |
36 | | - return value |
37 | | -} |
38 | | - |
39 | | -/// Wrapper type used to store `continuation` and |
40 | | -/// provide cancellation mechanism. |
41 | | -final class ContinuationWrapper<Wrapped: Continuable> { |
42 | | - /// The underlying continuation referenced. |
43 | | - var value: Wrapped? |
44 | | - |
45 | | - /// Creates a new instance with a continuation reference passed. |
46 | | - /// By default no continuation is stored. |
47 | | - /// |
48 | | - /// - Parameter value: A continuation reference to store. |
49 | | - /// |
50 | | - /// - Returns: The newly created continuation wrapper. |
51 | | - init(value: Wrapped? = nil) { |
52 | | - self.value = value |
53 | | - } |
54 | | - |
55 | | - /// Resume continuation with passed error, |
56 | | - /// without checking if continuation already resumed. |
57 | | - /// |
58 | | - /// - Parameter error: Error passed to continuation. |
59 | | - func cancel(withError error: Wrapped.Failure) { |
60 | | - value?.resume(throwing: error) |
61 | | - } |
62 | | -} |
63 | | - |
64 | 1 | /// A type that allows to interface between synchronous and asynchronous code, |
65 | 2 | /// by representing task state and allowing task resuming with some value or error. |
66 | 3 | protocol Continuable: Sendable { |
@@ -151,6 +88,59 @@ extension CheckedContinuation: ThrowingContinuable where E == Error { |
151 | 88 | } |
152 | 89 | } |
153 | 90 |
|
| 91 | +protocol NonThrowingContinuable: Continuable { |
| 92 | + /// The type of error to resume the continuation with in case of failure. |
| 93 | + associatedtype Failure = Never |
| 94 | + /// Suspends the current task, then calls the given closure |
| 95 | + /// with a nonthrowing continuation for the current task. |
| 96 | + /// |
| 97 | + /// The continuation can be resumed exactly once, |
| 98 | + /// subsequent resumes have different behavior depending on type implemeting. |
| 99 | + /// |
| 100 | + /// - Parameter fn: A closure that takes the nonthrowing continuation parameter. |
| 101 | + /// You can resume the continuation exactly once. |
| 102 | + /// |
| 103 | + /// - Returns: The value passed to the continuation by the closure. |
| 104 | + @inlinable |
| 105 | + static func with(_ fn: (Self) -> Void) async -> Success |
| 106 | +} |
| 107 | + |
| 108 | +extension UnsafeContinuation: NonThrowingContinuable where E == Never { |
| 109 | + /// Suspends the current task, then calls the given closure |
| 110 | + /// with an unsafe nonthrowing continuation for the current task. |
| 111 | + /// |
| 112 | + /// The continuation must be resumed exactly once, subsequent resumes will cause runtime error. |
| 113 | + /// Use `CheckedContinuation` to capture relevant data in case of runtime errors. |
| 114 | + /// |
| 115 | + /// - Parameter fn: A closure that takes an `UnsafeContinuation` parameter. |
| 116 | + /// You must resume the continuation exactly once. |
| 117 | + /// |
| 118 | + /// - Returns: The value passed to the continuation by the closure. |
| 119 | + @inlinable |
| 120 | + static func with(_ fn: (UnsafeContinuation<T, E>) -> Void) async -> T { |
| 121 | + return await withUnsafeContinuation(fn) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +extension CheckedContinuation: NonThrowingContinuable where E == Never { |
| 126 | + /// Suspends the current task, then calls the given closure |
| 127 | + /// with a checked nonthrowing continuation for the current task. |
| 128 | + /// |
| 129 | + /// The continuation must be resumed exactly once, subsequent resumes will cause runtime error. |
| 130 | + /// `CheckedContinuation` logs messages proving additional info on these errors. |
| 131 | + /// Once all errors resolved, use `UnsafeContinuation` in release mode to benefit improved performance |
| 132 | + /// at the loss of additional runtime checks. |
| 133 | + /// |
| 134 | + /// - Parameter fn: A closure that takes a `CheckedContinuation` parameter. |
| 135 | + /// You must resume the continuation exactly once. |
| 136 | + /// |
| 137 | + /// - Returns: The value passed to the continuation by the closure. |
| 138 | + @inlinable |
| 139 | + static func with(_ body: (CheckedContinuation<T, E>) -> Void) async -> T { |
| 140 | + return await withCheckedContinuation(body) |
| 141 | + } |
| 142 | +} |
| 143 | + |
154 | 144 | #if DEBUG || ASYNCOBJECTS_USE_CHECKEDCONTINUATION |
155 | 145 | /// The continuation type used in package in `DEBUG` mode |
156 | 146 | /// or if `ASYNCOBJECTS_USE_CHECKEDCONTINUATION` flag turned on. |
|
0 commit comments