Introduce runCatchingCancellationTransparent
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user