@@ -37,7 +37,6 @@ typealias Mutex = Synchronization.Mutex
3737
3838#endif
3939
40- #if compiler(>=6.0)
4140/// Invokes the passed in closure with an `IdentifableContinuation` for the current task.
4241///
4342/// The body of the closure executes synchronously on the calling actor. Once it returns the calling task is suspended.
@@ -61,7 +60,9 @@ public func withIdentifiableContinuation<T>(
6160) async -> T {
6261 let id = IdentifiableContinuation < T , Never > . ID ( )
6362 let state = Mutex ( ( isStarted: false , isCancelled: false ) )
63+ #if compiler(<6.2)
6464 nonisolated ( unsafe) let body = body
65+ #endif
6566 return await withTaskCancellationHandler {
6667 await withCheckedContinuation ( isolation: isolation, function: function) {
6768 let continuation = IdentifiableContinuation ( id: id, continuation: $0)
@@ -108,7 +109,9 @@ public func withIdentifiableThrowingContinuation<T>(
108109) async throws -> T {
109110 let id = IdentifiableContinuation < T , any Error > . ID ( )
110111 let state = Mutex ( ( isStarted: false , isCancelled: false ) )
112+ #if compiler(<6.2)
111113 nonisolated ( unsafe) let body = body
114+ #endif
112115 return try await withTaskCancellationHandler {
113116 try await withCheckedThrowingContinuation ( isolation: isolation, function: function) {
114117 let continuation = IdentifiableContinuation ( id: id, continuation: $0)
@@ -131,111 +134,13 @@ public func withIdentifiableThrowingContinuation<T>(
131134 }
132135 }
133136}
134- #else
135- /// Invokes the passed in closure with an `IdentifableContinuation` for the current task.
136- ///
137- /// The body of the closure executes synchronously on the calling actor. Once it returns the calling task is suspended.
138- /// It is possible to immediately resume the task, or escape the continuation in order to complete it afterwards, which
139- /// will then resume the suspended task.
140- ///
141- /// You must invoke the continuation's `resume` method exactly once.
142- /// - Parameters:
143- /// - isolation: Actor isolation used when executing the body closure.
144- /// - function: A string identifying the declaration that is the notional
145- /// source for the continuation, used to identify the continuation in
146- /// runtime diagnostics related to misuse of this continuation.
147- /// - body: A closure that takes a `IdentifiableContinuation` parameter.
148- /// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
149- /// - Returns: The value continuation is resumed with.
150- @_unsafeInheritExecutor
151- public func withIdentifiableContinuation< T> (
152- isolation: isolated some Actor ,
153- function: String = #function,
154- body: ( IdentifiableContinuation < T , Never > ) -> Void ,
155- onCancel handler: @Sendable ( IdentifiableContinuation < T , Never > . ID ) -> Void
156- ) async -> T {
157- let id = IdentifiableContinuation < T , Never > . ID ( )
158- let state = Mutex ( ( isStarted: false , isCancelled: false ) )
159- return await withTaskCancellationHandler {
160- await withCheckedContinuation ( function: function) {
161- let continuation = IdentifiableContinuation ( id: id, continuation: $0)
162- body ( continuation)
163- let sendCancel = state. withLock {
164- $0. isStarted = true
165- return $0. isCancelled
166- }
167- if sendCancel {
168- handler ( id)
169- }
170- _ = isolation
171- }
172- } onCancel: {
173- let sendCancel = state. withLock {
174- $0. isCancelled = true
175- return $0. isStarted
176- }
177- if sendCancel {
178- handler ( id)
179- }
180- }
181- }
182-
183- /// Invokes the passed in closure with an `IdentifableContinuation` for the current task.
184- ///
185- /// The body of the closure executes synchronously on the calling actor. Once it returns the calling task is suspended.
186- /// It is possible to immediately resume the task, or escape the continuation in order to complete it afterwards, which
187- /// will then resume the suspended task.
188- ///
189- /// You must invoke the continuation's `resume` method exactly once.
190- /// - Parameters:
191- /// - isolation: Actor isolation used when executing the body closure.
192- /// - function: A string identifying the declaration that is the notional
193- /// source for the continuation, used to identify the continuation in
194- /// runtime diagnostics related to misuse of this continuation.
195- /// - body: A closure that takes a `IdentifiableContinuation` parameter.
196- /// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
197- /// - Returns: The value continuation is resumed with.
198- @_unsafeInheritExecutor
199- public func withIdentifiableThrowingContinuation< T> (
200- isolation: isolated some Actor ,
201- function: String = #function,
202- body: ( IdentifiableContinuation < T , any Error > ) -> Void ,
203- onCancel handler: @Sendable ( IdentifiableContinuation < T , any Error > . ID ) -> Void
204- ) async throws -> T {
205- let id = IdentifiableContinuation < T , any Error > . ID ( )
206- let state = Mutex ( ( isStarted: false , isCancelled: false ) )
207- return try await withTaskCancellationHandler {
208- try await withCheckedThrowingContinuation ( function: function) {
209- let continuation = IdentifiableContinuation ( id: id, continuation: $0)
210- body ( continuation)
211- let sendCancel = state. withLock {
212- $0. isStarted = true
213- return $0. isCancelled
214- }
215- if sendCancel {
216- handler ( id)
217- }
218- _ = isolation
219- }
220- } onCancel: {
221- let sendCancel = state. withLock {
222- $0. isCancelled = true
223- return $0. isStarted
224- }
225- if sendCancel {
226- handler ( id)
227- }
228- }
229- }
230- #endif
231137
232138public struct IdentifiableContinuation < T, E> : Sendable , Identifiable where E: Error {
233139
234140 public let id : ID
235141
236142 public final class ID : Hashable , Sendable {
237143
238- @usableFromInline
239144 init ( ) { }
240145
241146 public func hash( into hasher: inout Hasher ) {
@@ -247,31 +152,20 @@ public struct IdentifiableContinuation<T, E>: Sendable, Identifiable where E: Er
247152 }
248153 }
249154
250- @usableFromInline
251155 init ( id: ID , continuation: CheckedContinuation < T , E > ) {
252156 self . id = id
253157 self . continuation = continuation
254158 }
255159
256160 private let continuation : CheckedContinuation < T , E >
257161
258- #if compiler(>=6.0)
259162 public func resume( returning value: sending T) {
260163 continuation. resume ( returning: value)
261164 }
262165
263166 public func resume( with result: sending Result< T , E > ) {
264167 continuation. resume ( with: result)
265168 }
266- #else
267- public func resume( returning value: T ) {
268- continuation. resume ( returning: value)
269- }
270-
271- public func resume( with result: Result < T , E > ) {
272- continuation. resume ( with: result)
273- }
274- #endif
275169
276170 public func resume( throwing error: E ) {
277171 continuation. resume ( throwing: error)
0 commit comments