From 18b1130c628956b998c9811f0c64642b94cb4ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernd=20Pr=C3=BCnster?= Date: Wed, 16 Oct 2024 14:17:52 +0200 Subject: [PATCH] address more PR feedback --- .../kotlin/at/asitplus/KmmResult.kt | 10 +++--- .../commonMain/kotlin/at/asitplus/NonFatal.kt | 33 ++++++++----------- .../kotlin/at/asitplus/NonFatal.jvm.kt | 1 + .../kotlin/at/asitplus/NonFatal.nonJvm.kt | 1 + 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/kmmresult/src/commonMain/kotlin/at/asitplus/KmmResult.kt b/kmmresult/src/commonMain/kotlin/at/asitplus/KmmResult.kt index 875ffdc..6c59566 100644 --- a/kmmresult/src/commonMain/kotlin/at/asitplus/KmmResult.kt +++ b/kmmresult/src/commonMain/kotlin/at/asitplus/KmmResult.kt @@ -267,8 +267,9 @@ inline fun KmmResult.recoverCatching(block: (error: Throwable) -> /** * Non-fatal-only-catching version of stdlib's [runCatching], directly returning a [KmmResult] -- - * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s - * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic. + * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s + * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) + * logic to avoid a dependency on Arrow for a single function. */ @Suppress("TooGenericExceptionCaught") inline fun catching(block: () -> T): KmmResult { @@ -286,8 +287,9 @@ inline fun catching(block: () -> T): KmmResult { /** * Non-fatal-only-catching version of stdlib's [runCatching] (calling the specified function [block] with `this` value * as its receiver), directly returning a [KmmResult] -- - * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s - * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic. + * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s + * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) + * logic to avoid a dependency on Arrow for a single function. */ @Suppress("TooGenericExceptionCaught") inline fun T.catching(block: T.() -> R): KmmResult { diff --git a/kmmresult/src/commonMain/kotlin/at/asitplus/NonFatal.kt b/kmmresult/src/commonMain/kotlin/at/asitplus/NonFatal.kt index 72d7b0f..767bee6 100644 --- a/kmmresult/src/commonMain/kotlin/at/asitplus/NonFatal.kt +++ b/kmmresult/src/commonMain/kotlin/at/asitplus/NonFatal.kt @@ -1,42 +1,37 @@ package at.asitplus -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract - /** * Throws any fatal exceptions. This is a re-implementation taken from Arrow's * [`nonFatalOrThrow`](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) – - * not because it is bad, it is actually pretty much perfect. - * However, the arrow dependency triggered an obscure IDEA bug, resulting in `NoClasDefFoundErrors` instead of correct - * behaviour. * We therefore removed the dependency and added the functionality directly to KmmResult. - * - * Please note that this was never a problem building anything that depended on KmmResult, it only made debugging - * in IDEA a nightmare. + * to avoid a dependency on Arrow for a single function. */ +@Suppress("NOTHING_TO_INLINE") expect inline fun Throwable.nonFatalOrThrow(): Throwable /** - * Helper to effectively convert stdlib's [runCatching] to behave like KmmResults Non-fatal-only [catching]. I.e. any + * Helper to effectively convert stdlib's [runCatching] to behave like KmmResult's Non-fatal-only [catching]. I.e. any * fatal exceptions are thrown. * The reason this exists is that [catching] incurs instantiation cost. * This helper hence provides the best of both worlds. */ +@Suppress("NOTHING_TO_INLINE") inline fun Result.nonFatalOrThrow(): Result = this.onFailure { it.nonFatalOrThrow() } - /** * Non-fatal-only-catching version of stdlib's [runCatching], returning a [Result] -- - * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s - * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic. + * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s + * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) + * logic to avoid a dependency on Arrow for a single function. */ -@Suppress("TooGenericExceptionCaught") -inline fun catchingUnwrapped(block: () -> T): Result = runCatching (block).nonFatalOrThrow() +@Suppress("NOTHING_TO_INLINE") +inline fun catchingUnwrapped(block: () -> T): Result = runCatching(block).nonFatalOrThrow() /** * Non-fatal-only-catching version of stdlib's [runCatching] (calling the specified function [block] with `this` value * as its receiver), directly returning a [Result] -- - * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s - * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic. + * Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s + * [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) + * logic to avoid a dependency on Arrow for a single function. */ -@Suppress("TooGenericExceptionCaught") -inline fun T.catchingUnwrapped(block: T.() -> R): Result = runCatching (block).nonFatalOrThrow() \ No newline at end of file +@Suppress("NOTHING_TO_INLINE") +inline fun T.catchingUnwrapped(block: T.() -> R): Result = runCatching(block).nonFatalOrThrow() diff --git a/kmmresult/src/jvmMain/kotlin/at/asitplus/NonFatal.jvm.kt b/kmmresult/src/jvmMain/kotlin/at/asitplus/NonFatal.jvm.kt index 9c3c0b2..5deae44 100644 --- a/kmmresult/src/jvmMain/kotlin/at/asitplus/NonFatal.jvm.kt +++ b/kmmresult/src/jvmMain/kotlin/at/asitplus/NonFatal.jvm.kt @@ -2,6 +2,7 @@ package at.asitplus import kotlin.coroutines.cancellation.CancellationException //Taken from Arrow: https://github.com/arrow-kt/arrow/blob/99de6148320a4299a5aef20686a6063ca732026b/arrow-libs/core/arrow-core/src/jvmMain/kotlin/arrow/core/NonFatal.kt +@Suppress("NOTHING_TO_INLINE") actual inline fun Throwable.nonFatalOrThrow(): Throwable = when (this) { is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError, is CancellationException -> throw this else -> this diff --git a/kmmresult/src/nonJvmMain/kotlin/at/asitplus/NonFatal.nonJvm.kt b/kmmresult/src/nonJvmMain/kotlin/at/asitplus/NonFatal.nonJvm.kt index 592ad7a..5283ea5 100644 --- a/kmmresult/src/nonJvmMain/kotlin/at/asitplus/NonFatal.nonJvm.kt +++ b/kmmresult/src/nonJvmMain/kotlin/at/asitplus/NonFatal.nonJvm.kt @@ -2,6 +2,7 @@ package at.asitplus import kotlin.coroutines.cancellation.CancellationException //Taken from Arrow: https://github.com/arrow-kt/arrow/blob/99de6148320a4299a5aef20686a6063ca732026b/arrow-libs/core/arrow-core/src/nonJvmMain/kotlin/arrow/core/NonFatal.kt +@Suppress("NOTHING_TO_INLINE") actual inline fun Throwable.nonFatalOrThrow(): Throwable = when (this) { is CancellationException -> throw this else -> this