kotlin.coroutines.experimental.migration.CoroutinesMigration.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.coroutines.experimental.migration
import kotlin.coroutines.experimental.Continuation as ExperimentalContinuation
import kotlin.coroutines.experimental.CoroutineContext as ExperimentalCoroutineContext
import kotlin.coroutines.experimental.AbstractCoroutineContextElement as ExperimentalAbstractCoroutineContextElement
import kotlin.coroutines.experimental.EmptyCoroutineContext as ExperimentalEmptyCoroutineContext
import kotlin.coroutines.experimental.ContinuationInterceptor as ExperimentalContinuationInterceptor
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
/**
* Converts [Continuation] to [ExperimentalContinuation].
*/
@SinceKotlin("1.3")
public fun Continuation.toExperimentalContinuation(): ExperimentalContinuation =
(this as? ContinuationMigration)?.continuation ?: ExperimentalContinuationMigration(this)
/**
* Converts [ExperimentalContinuation] to [Continuation].
*/
@SinceKotlin("1.3")
public fun ExperimentalContinuation.toContinuation(): Continuation =
(this as? ExperimentalContinuationMigration)?.continuation ?: ContinuationMigration(this)
/**
* Converts [CoroutineContext] to [ExperimentalCoroutineContext].
*/
@SinceKotlin("1.3")
public fun CoroutineContext.toExperimentalCoroutineContext(): ExperimentalCoroutineContext {
val interceptor = get(ContinuationInterceptor.Key)
val migration = get(ContextMigration.Key)
val remainder = minusKey(ContinuationInterceptor.Key).minusKey(ContextMigration.Key)
val original = migration?.context ?: ExperimentalEmptyCoroutineContext
val result = if (remainder === EmptyCoroutineContext) original else original + ExperimentalContextMigration(remainder)
return if (interceptor == null) result else result + interceptor.toExperimentalContinuationInterceptor()
}
/**
* Converts [ExperimentalCoroutineContext] to [CoroutineContext].
*/
@SinceKotlin("1.3")
public fun ExperimentalCoroutineContext.toCoroutineContext(): CoroutineContext {
val interceptor = get(ExperimentalContinuationInterceptor.Key)
val migration = get(ExperimentalContextMigration.Key)
val remainder = minusKey(ExperimentalContinuationInterceptor.Key).minusKey(ExperimentalContextMigration.Key)
val original = migration?.context ?: EmptyCoroutineContext
val result = if (remainder === ExperimentalEmptyCoroutineContext) original else original + ContextMigration(remainder)
return if (interceptor == null) result else result + interceptor.toContinuationInterceptor()
}
/**
* Converts [ContinuationInterceptor] to [ExperimentalContinuationInterceptor].
*/
@SinceKotlin("1.3")
public fun ContinuationInterceptor.toExperimentalContinuationInterceptor(): ExperimentalContinuationInterceptor =
(this as? ContinuationInterceptorMigration)?.interceptor ?: ExperimentalContinuationInterceptorMigration(this)
/**
* Converts [ExperimentalContinuationInterceptor] to [ContinuationInterceptor].
*/
@SinceKotlin("1.3")
public fun ExperimentalContinuationInterceptor.toContinuationInterceptor(): ContinuationInterceptor =
(this as? ExperimentalContinuationInterceptorMigration)?.interceptor ?: ContinuationInterceptorMigration(this)
// ------------------ converter classes ------------------
// Their name starts with "Experimental" if they implement the corresponding Experimental interfaces
private class ExperimentalContinuationMigration(val continuation: Continuation): ExperimentalContinuation {
override val context = continuation.context.toExperimentalCoroutineContext()
override fun resume(value: T) = continuation.resume(value)
override fun resumeWithException(exception: Throwable) = continuation.resumeWithException(exception)
}
private class ContinuationMigration(val continuation: ExperimentalContinuation): Continuation {
override val context: CoroutineContext = continuation.context.toCoroutineContext()
override fun resumeWith(result: Result) {
result
.onSuccess { continuation.resume(it) }
.onFailure { continuation.resumeWithException(it) }
}
}
private class ExperimentalContextMigration(val context: CoroutineContext): ExperimentalAbstractCoroutineContextElement(Key) {
companion object Key : ExperimentalCoroutineContext.Key
}
private class ContextMigration(val context: ExperimentalCoroutineContext): AbstractCoroutineContextElement(Key) {
companion object Key : CoroutineContext.Key
}
private class ExperimentalContinuationInterceptorMigration(val interceptor: ContinuationInterceptor) : ExperimentalContinuationInterceptor {
override val key: ExperimentalCoroutineContext.Key<*>
get() = ExperimentalContinuationInterceptor.Key
override fun interceptContinuation(continuation: ExperimentalContinuation): ExperimentalContinuation =
interceptor.interceptContinuation(continuation.toContinuation()).toExperimentalContinuation()
}
private class ContinuationInterceptorMigration(val interceptor: ExperimentalContinuationInterceptor) : ContinuationInterceptor {
override val key: CoroutineContext.Key<*>
get() = ContinuationInterceptor.Key
override fun interceptContinuation(continuation: Continuation): Continuation =
interceptor.interceptContinuation(continuation.toExperimentalContinuation()).toContinuation()
}
internal fun ((Continuation) -> Any?).toExperimentalSuspendFunction(): (ExperimentalContinuation) -> Any? =
ExperimentalSuspendFunction0Migration(this)
internal fun ((T1, Continuation) -> Any?).toExperimentalSuspendFunction(): (T1, ExperimentalContinuation) -> Any? =
ExperimentalSuspendFunction1Migration(this)
internal fun ((T1, T2, Continuation) -> Any?).toExperimentalSuspendFunction(): (T1, T2, ExperimentalContinuation) -> Any? =
ExperimentalSuspendFunction2Migration(this)
private class ExperimentalSuspendFunction0Migration(
val function: (Continuation) -> Any?
) : (ExperimentalContinuation) -> Any? {
override fun invoke(continuation: ExperimentalContinuation): Any? {
return function(continuation.toContinuation())
}
}
private class ExperimentalSuspendFunction1Migration(
val function: (T1, Continuation) -> Any?
) : (T1, ExperimentalContinuation) -> Any? {
override fun invoke(t1: T1, continuation: ExperimentalContinuation): Any? {
return function(t1, continuation.toContinuation())
}
}
private class ExperimentalSuspendFunction2Migration(
val function: (T1, T2, Continuation) -> Any?
) : (T1, T2, ExperimentalContinuation) -> Any? {
override fun invoke(t1: T1, t2: T2, continuation: ExperimentalContinuation): Any? {
return function(t1, t2, continuation.toContinuation())
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy