Reformat coroutine sources

This commit is contained in:
Ilya Gorbunov
2018-11-17 00:45:03 +03:00
parent ec3692026d
commit 7d61a2de73
5 changed files with 21 additions and 17 deletions
@@ -22,8 +22,8 @@ internal abstract class CoroutineImpl(private val resultContinuation: Continuati
public fun intercepted(): Continuation<Any?> = public fun intercepted(): Continuation<Any?> =
intercepted_ intercepted_
?: (context[ContinuationInterceptor]?.interceptContinuation(this) ?: this) ?: (context[ContinuationInterceptor]?.interceptContinuation(this) ?: this)
.also { intercepted_ = it } .also { intercepted_ = it }
override fun resumeWith(result: Result<Any?>) { override fun resumeWith(result: Result<Any?>) {
var current = this var current = this
@@ -84,13 +84,15 @@ public inline class Result<out T> @PublishedApi internal constructor(
/** /**
* Returns an instance that encapsulates the given [value] as successful value. * Returns an instance that encapsulates the given [value] as successful value.
*/ */
@InlineOnly public inline fun <T> success(value: T): Result<T> = @InlineOnly
public inline fun <T> success(value: T): Result<T> =
Result(value) Result(value)
/** /**
* Returns an instance that encapsulates the given [exception] as failure. * Returns an instance that encapsulates the given [exception] as failure.
*/ */
@InlineOnly public inline fun <T> failure(exception: Throwable): Result<T> = @InlineOnly
public inline fun <T> failure(exception: Throwable): Result<T> =
Result(createFailure(exception)) Result(createFailure(exception))
} }
@@ -181,7 +183,7 @@ public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwabl
contract { contract {
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
} }
return when(val exception = exceptionOrNull()) { return when (val exception = exceptionOrNull()) {
null -> value as T null -> value as T
else -> onFailure(exception) else -> onFailure(exception)
} }
@@ -216,7 +218,7 @@ public inline fun <R, T> Result<T>.fold(
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE) callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
} }
return when(val exception = exceptionOrNull()) { return when (val exception = exceptionOrNull()) {
null -> onSuccess(value as T) null -> onSuccess(value as T)
else -> onFailure(exception) else -> onFailure(exception)
} }
@@ -271,11 +273,11 @@ public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Resu
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> { public inline fun <R, T : R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> {
contract { contract {
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
} }
return when(val exception = exceptionOrNull()) { return when (val exception = exceptionOrNull()) {
null -> this null -> this
else -> Result.success(transform(exception)) else -> Result.success(transform(exception))
} }
@@ -291,9 +293,9 @@ public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable)
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3") @SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> { public inline fun <R, T : R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> {
val value = value // workaround for inline classes BE bug val value = value // workaround for inline classes BE bug
return when(val exception = exceptionOrNull()) { return when (val exception = exceptionOrNull()) {
null -> this null -> this
else -> runCatching { transform(exception) } else -> runCatching { transform(exception) }
} }
@@ -7,7 +7,6 @@ package kotlin.coroutines
import kotlin.coroutines.intrinsics.* import kotlin.coroutines.intrinsics.*
import kotlin.internal.InlineOnly import kotlin.internal.InlineOnly
import kotlin.jvm.JvmName
/** /**
* Interface representing a continuation after a suspension point that returns value of type `T`. * Interface representing a continuation after a suspension point that returns value of type `T`.
@@ -40,7 +39,8 @@ public annotation class RestrictsSuspension
* Resumes the execution of the corresponding coroutine passing [value] as the return value of the last suspension point. * Resumes the execution of the corresponding coroutine passing [value] as the return value of the last suspension point.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@InlineOnly public inline fun <T> Continuation<T>.resume(value: T): Unit = @InlineOnly
public inline fun <T> Continuation<T>.resume(value: T): Unit =
resumeWith(Result.success(value)) resumeWith(Result.success(value))
/** /**
@@ -48,7 +48,8 @@ public annotation class RestrictsSuspension
* last suspension point. * last suspension point.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@InlineOnly public inline fun <T> Continuation<T>.resumeWithException(exception: Throwable): Unit = @InlineOnly
public inline fun <T> Continuation<T>.resumeWithException(exception: Throwable): Unit =
resumeWith(Result.failure(exception)) resumeWith(Result.failure(exception))
@@ -56,7 +57,8 @@ public annotation class RestrictsSuspension
* Creates [Continuation] instance with a given [context] and a given implementation of [resumeWith] method. * Creates [Continuation] instance with a given [context] and a given implementation of [resumeWith] method.
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@InlineOnly public inline fun <T> Continuation( @InlineOnly
public inline fun <T> Continuation(
context: CoroutineContext, context: CoroutineContext,
crossinline resumeWith: (Result<T>) -> Unit crossinline resumeWith: (Result<T>) -> Unit
): Continuation<T> = ): Continuation<T> =