diff --git a/app/src/main/java/moe/tarsin/coroutines/Result.kt b/app/src/main/java/moe/tarsin/coroutines/Result.kt new file mode 100644 index 000000000..11acbb34a --- /dev/null +++ b/app/src/main/java/moe/tarsin/coroutines/Result.kt @@ -0,0 +1,21 @@ +package moe.tarsin.coroutines + +import kotlinx.coroutines.CancellationException + +inline fun runCatchingCancellationTransparent(block: () -> R): Result { + return try { + Result.success(block()) + } catch (e: Throwable) { + if (e is CancellationException) throw e + Result.failure(e) + } +} + +inline fun T.runCatchingCancellationTransparent(block: T.() -> R): Result { + return try { + Result.success(block()) + } catch (e: Throwable) { + if (e is CancellationException) throw e + Result.failure(e) + } +}