improve default runCatchingSafely/safelyWithResult and add suspend variances of safe/unsafe SafeWrapper interface

This commit is contained in:
InsanusMokrassar committed 2022-12-09 12:14:24 +06:00
1 parent 43fe06206a
commit ced05a4586
3 files changed
+26 -1

No files matched your search

+10
View File
@@ -5,3 +5,13 @@ plugins {
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":micro_utils.coroutines")
}
}
}
}
@@ -1,8 +1,12 @@
package dev.inmo.micro_utils.safe_wrapper
import dev.inmo.micro_utils.coroutines.runCatchingSafely
interface SafeWrapper<T> {
fun <R> safe (block: T.() -> R): Result<R> = unsafeTarget().runCatching(block)
fun <R> safe(block: T.() -> R): Result<R> = unsafeTarget().runCatching(block)
fun <R> unsafe(block: T.() -> R): R = unsafeTarget().block()
suspend fun <R> safeS(block: suspend T.() -> R): Result<R> = unsafeTarget().runCatchingSafely(block = block)
suspend fun <R> unsafeS(block: suspend T.() -> R): R = unsafeTarget().block()
fun unsafeTarget(): T
class Default<T>(private val t: T) : SafeWrapper<T> { override fun unsafeTarget(): T = t }