@@ -175,32 +175,16 @@ internal class DispatchedContinuation<in T>(
175
175
}
176
176
177
177
@Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
178
- inline fun resumeCancellable (value : T ) {
179
- if (dispatcher.isDispatchNeeded(context)) {
180
- _state = value
181
- resumeMode = MODE_CANCELLABLE
182
- dispatcher.dispatch(context, this )
183
- } else {
184
- executeUnconfined(value, MODE_CANCELLABLE ) {
185
- if (! resumeCancelled()) {
186
- resumeUndispatched(value)
187
- }
188
- }
189
- }
190
- }
191
-
192
- @Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
193
- inline fun resumeCancellableWithException (exception : Throwable ) {
194
- val context = continuation.context
195
- val state = CompletedExceptionally (exception)
178
+ inline fun resumeCancellableWith (result : Result <T >) {
179
+ val state = result.toState()
196
180
if (dispatcher.isDispatchNeeded(context)) {
197
- _state = CompletedExceptionally (exception)
181
+ _state = state
198
182
resumeMode = MODE_CANCELLABLE
199
183
dispatcher.dispatch(context, this )
200
184
} else {
201
185
executeUnconfined(state, MODE_CANCELLABLE ) {
202
186
if (! resumeCancelled()) {
203
- resumeUndispatchedWithException(exception )
187
+ resumeUndispatchedWith(result )
204
188
}
205
189
}
206
190
}
@@ -218,16 +202,9 @@ internal class DispatchedContinuation<in T>(
218
202
}
219
203
220
204
@Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
221
- inline fun resumeUndispatched (value : T ) {
222
- withCoroutineContext(context, countOrElement) {
223
- continuation.resume(value)
224
- }
225
- }
226
-
227
- @Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
228
- inline fun resumeUndispatchedWithException (exception : Throwable ) {
205
+ inline fun resumeUndispatchedWith (result : Result <T >) {
229
206
withCoroutineContext(context, countOrElement) {
230
- continuation.resumeWithStackTrace(exception )
207
+ continuation.resumeWith(result )
231
208
}
232
209
}
233
210
@@ -243,6 +220,12 @@ internal class DispatchedContinuation<in T>(
243
220
" DispatchedContinuation[$dispatcher , ${continuation.toDebugString()} ]"
244
221
}
245
222
223
+ @Suppress(" NOTHING_TO_INLINE" ) // we need it inline to save us an entry on the stack
224
+ internal inline fun <T > Continuation<T>.resumeCancellableWith (result : Result <T >) = when (this ) {
225
+ is DispatchedContinuation -> resumeCancellableWith(result)
226
+ else -> resumeWith(result)
227
+ }
228
+
246
229
internal fun DispatchedContinuation<Unit>.yieldUndispatched (): Boolean =
247
230
executeUnconfined(Unit , MODE_CANCELLABLE , doYield = true ) {
248
231
run ()
0 commit comments