@@ -119,7 +119,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
119119 }
120120
121121 func cancelNext( _ nextTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) {
122- state. withCriticalRegion { state -> UnsafeContinuation < Element ? , Error > ? in
122+ state. withCriticalRegion { state in
123123 let continuation : UnsafeContinuation < Element ? , Error > ?
124124
125125 switch state. emission {
@@ -140,44 +140,41 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
140140 }
141141 }
142142
143- return continuation
144- } ? . resume ( returning : nil )
143+ continuation? . resume ( returning : nil )
144+ }
145145 }
146146
147147 func next( _ nextTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) async throws -> Element ? {
148148 return try await withUnsafeThrowingContinuation { ( continuation: UnsafeContinuation < Element ? , Error > ) in
149149 var cancelled = false
150150 var potentialTermination : Termination ?
151151
152- state. withCriticalRegion { state -> UnsafeResumption < UnsafeContinuation < Element ? , Error > ? , Never > ? in
152+ state. withCriticalRegion { state in
153153
154154 if nextTokenStatus. withCriticalRegion ( { $0 } ) == . cancelled {
155155 cancelled = true
156- return nil
156+ return
157157 }
158158
159159 switch state. emission {
160160 case . idle:
161161 state. emission = . awaiting( [ Awaiting ( generation: generation, continuation: continuation) ] )
162- return nil
163162 case . pending( var sends) :
164163 let send = sends. removeFirst ( )
165164 if sends. count == 0 {
166165 state. emission = . idle
167166 } else {
168167 state. emission = . pending( sends)
169168 }
170- return UnsafeResumption ( continuation : send. continuation, success : continuation)
169+ send. continuation? . resume ( returning : continuation)
171170 case . awaiting( var nexts) :
172171 nexts. updateOrAppend ( Awaiting ( generation: generation, continuation: continuation) )
173172 state. emission = . awaiting( nexts)
174- return nil
175173 case . terminated( let termination) :
176174 potentialTermination = termination
177175 state. emission = . terminated( . finished)
178- return nil
179176 }
180- } ? . resume ( )
177+ }
181178
182179 if cancelled {
183180 continuation. resume ( returning: nil )
@@ -198,7 +195,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
198195 }
199196
200197 func cancelSend( _ sendTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) {
201- state. withCriticalRegion { state -> UnsafeContinuation < UnsafeContinuation < Element ? , Error > ? , Never > ? in
198+ state. withCriticalRegion { state in
202199 let continuation : UnsafeContinuation < UnsafeContinuation < Element ? , Error > ? , Never > ?
203200
204201 switch state. emission {
@@ -220,44 +217,43 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
220217 }
221218 }
222219
223- return continuation
224- } ? . resume ( returning : nil )
220+ continuation? . resume ( returning : nil )
221+ }
225222 }
226223
227224 func send( _ sendTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int , _ element: Element ) async {
228225 let continuation : UnsafeContinuation < Element ? , Error > ? = await withUnsafeContinuation { continuation in
229- state. withCriticalRegion { state -> UnsafeResumption < UnsafeContinuation < Element ? , Error > ? , Never > ? in
226+ state. withCriticalRegion { state in
230227
231228 if sendTokenStatus. withCriticalRegion ( { $0 } ) == . cancelled {
232- return UnsafeResumption ( continuation: continuation, success: nil )
229+ continuation. resume ( returning: nil )
230+ return
233231 }
234232
235233 switch state. emission {
236234 case . idle:
237235 state. emission = . pending( [ Pending ( generation: generation, continuation: continuation) ] )
238- return nil
239236 case . pending( var sends) :
240237 sends. updateOrAppend ( Pending ( generation: generation, continuation: continuation) )
241238 state. emission = . pending( sends)
242- return nil
243239 case . awaiting( var nexts) :
244240 let next = nexts. removeFirst ( ) . continuation
245241 if nexts. count == 0 {
246242 state. emission = . idle
247243 } else {
248244 state. emission = . awaiting( nexts)
249245 }
250- return UnsafeResumption ( continuation: continuation , success : next)
246+ continuation. resume ( returning : next)
251247 case . terminated:
252- return UnsafeResumption ( continuation: continuation , success : nil )
248+ continuation. resume ( returning : nil )
253249 }
254- } ? . resume ( )
250+ }
255251 }
256252 continuation? . resume ( returning: element)
257253 }
258254
259255 func terminateAll( error: Failure ? = nil ) {
260- let ( sends , nexts ) = state. withCriticalRegion { state -> ( OrderedSet < Pending > , OrderedSet < Awaiting > ) in
256+ state. withCriticalRegion { state in
261257
262258 let nextState : Emission
263259 if let error = error {
@@ -269,29 +265,24 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
269265 switch state. emission {
270266 case . idle:
271267 state. emission = nextState
272- return ( [ ] , [ ] )
273- case . pending( let nexts) :
268+ case . pending( let sends) :
274269 state. emission = nextState
275- return ( nexts, [ ] )
270+ for send in sends {
271+ send. continuation? . resume ( returning: nil )
272+ }
276273 case . awaiting( let nexts) :
277274 state. emission = nextState
278- return ( [ ] , nexts)
275+ if let error = error {
276+ for next in nexts {
277+ next. continuation? . resume ( throwing: error)
278+ }
279+ } else {
280+ for next in nexts {
281+ next. continuation? . resume ( returning: nil )
282+ }
283+ }
279284 case . terminated:
280- return ( [ ] , [ ] )
281- }
282- }
283-
284- for send in sends {
285- send. continuation? . resume ( returning: nil )
286- }
287-
288- if let error = error {
289- for next in nexts {
290- next. continuation? . resume ( throwing: error)
291- }
292- } else {
293- for next in nexts {
294- next. continuation? . resume ( returning: nil )
285+ break
295286 }
296287 }
297288 }
0 commit comments