blob: 99475900af79221fb31234e280d188e47ff31164 [file] [log] [blame]
Roman Elizarovee88fbe2017-02-22 09:53:49 +03001package kotlinx.coroutines.experimental.intrinsics
2
3import kotlin.coroutines.experimental.Continuation
4import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
5import kotlin.coroutines.experimental.suspendCoroutine
6
7/**
8 * Use this function to restart coroutine directly from inside of [suspendCoroutine].
9 *
10 * @suppress **This is unstable API and it is subject to change.**
11 */
12@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
13internal fun <R> (suspend () -> R).startUndispatchedCoroutine(completion: Continuation<R>) {
14 val value = try {
15 (this as kotlin.jvm.functions.Function1<Continuation<R>, Any?>).invoke(completion)
16 } catch (e: Throwable) {
17 completion.resumeWithException(e)
18 return
19 }
20 if (value !== COROUTINE_SUSPENDED)
21 completion.resume(value as R)
22}
23
24/**
25 * Use this function to restart coroutine directly from inside of [suspendCoroutine].
26 *
27 * @suppress **This is unstable API and it is subject to change.**
28 */
29@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")
30internal fun <E, R> (suspend (E) -> R).startUndispatchedCoroutine(element: E, completion: Continuation<R>) {
31 val value = try {
32 (this as kotlin.jvm.functions.Function2<E, Continuation<R>, Any?>).invoke(element, completion)
33 } catch (e: Throwable) {
34 completion.resumeWithException(e)
35 return
36 }
37 if (value !== COROUTINE_SUSPENDED)
38 completion.resume(value as R)
39}