diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2023-09-28 01:29:24 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2023-09-28 01:29:24 +0530 |
commit | 41c86b67f3f1fe320e702b05887f22188a5b42fc (patch) | |
tree | fa1b5c3c93d4d9e293dadf226e69d7c47a90d5ff /coroutine-utils | |
parent | 5f3aa611c94a0da933e7a94f6fa0cceba14899fa (diff) |
refactor: use `runSuspendCatching` from `kotlin-result`
Diffstat (limited to 'coroutine-utils')
-rw-r--r-- | coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt b/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt deleted file mode 100644 index 677c2bc3..00000000 --- a/coroutine-utils/src/main/kotlin/app/passwordstore/util/coroutines/RunSuspendCatching.kt +++ /dev/null @@ -1,51 +0,0 @@ -@file:OptIn(ExperimentalContracts::class) -@file:Suppress("RedundantSuspendModifier") - -package app.passwordstore.util.coroutines - -import com.github.michaelbull.result.Err -import com.github.michaelbull.result.Ok -import com.github.michaelbull.result.Result -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract -import kotlinx.coroutines.CancellationException - -/** - * Calls the specified function [block] and returns its encapsulated result if invocation was - * successful, catching any [Throwable] except [CancellationException] that was thrown from the - * [block] function execution and encapsulating it as a failure. - */ -@OptIn(ExperimentalContracts::class) -public suspend inline fun <V> runSuspendCatching(block: () -> V): Result<V, Throwable> { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - - return try { - Ok(block()) - } catch (e: CancellationException) { - throw e - } catch (e: Throwable) { - Err(e) - } -} - -/** - * Calls the specified function [block] with [this] value as its receiver and returns its - * encapsulated result if invocation was successful, catching any [Throwable] except - * [CancellationException] that was thrown from the [block] function execution and encapsulating it - * as a failure. - */ -@OptIn(ExperimentalContracts::class) -public suspend inline infix fun <T, V> T.runSuspendCatching( - block: T.() -> V -): Result<V, Throwable> { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - - return try { - Ok(block()) - } catch (e: CancellationException) { - throw e - } catch (e: Throwable) { - Err(e) - } -} |