Introduce runCatchingCancellationTransparent

This commit is contained in:
tarsin committed 2023-02-08 22:26:45 +08:00
1 parent d83bb0f964
commit b246e163d2
1 file changed
+21
@@ -0,0 +1,21 @@
package moe.tarsin.coroutines
import kotlinx.coroutines.CancellationException
inline fun <R> runCatchingCancellationTransparent(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (e: Throwable) {
if (e is CancellationException) throw e
Result.failure(e)
}
}
inline fun <T, R> T.runCatchingCancellationTransparent(block: T.() -> R): Result<R> {
return try {
Result.success(block())
} catch (e: Throwable) {
if (e is CancellationException) throw e
Result.failure(e)
}
}