From 184927bf28e62ac250369e6b6375da975beac3c6 Mon Sep 17 00:00:00 2001 From: Youssef Shoaib Date: Tue, 5 Nov 2024 14:25:12 +0000 Subject: [PATCH] Inline runReleaseAndRethrow and simplify --- .../api/arrow-fx-coroutines.api | 1 - .../api/arrow-fx-coroutines.klib.api | 1 - .../kotlin/arrow/fx/coroutines/Bracket.kt | 25 +++++++++---------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api index fac7ff190b7..669e1fea5a9 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api +++ b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api @@ -10,7 +10,6 @@ public final class arrow/fx/coroutines/BracketKt { public static final fun guarantee (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun guaranteeCase (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun onCancel (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun runReleaseAndRethrow (Ljava/lang/Throwable;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class arrow/fx/coroutines/CountDownLatch { diff --git a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.klib.api b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.klib.api index 2c0b1b3e3c8..09e4fdb83d8 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.klib.api +++ b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.klib.api @@ -235,4 +235,3 @@ final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/guarantee(kotlin. final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/guaranteeCase(kotlin.coroutines/SuspendFunction0<#A>, crossinline kotlin.coroutines/SuspendFunction1): #A // arrow.fx.coroutines/guaranteeCase|guaranteeCase(kotlin.coroutines.SuspendFunction0<0:0>;kotlin.coroutines.SuspendFunction1){0§}[0] final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/onCancel(kotlin.coroutines/SuspendFunction0<#A>, crossinline kotlin.coroutines/SuspendFunction0): #A // arrow.fx.coroutines/onCancel|onCancel(kotlin.coroutines.SuspendFunction0<0:0>;kotlin.coroutines.SuspendFunction0){0§}[0] final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/resourceScope(kotlin.coroutines/SuspendFunction1): #A // arrow.fx.coroutines/resourceScope|resourceScope(kotlin.coroutines.SuspendFunction1){0§}[0] -final suspend inline fun arrow.fx.coroutines/runReleaseAndRethrow(kotlin/Throwable?, crossinline kotlin.coroutines/SuspendFunction0) // arrow.fx.coroutines/runReleaseAndRethrow|runReleaseAndRethrow(kotlin.Throwable?;kotlin.coroutines.SuspendFunction0){}[0] diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt index c8209e9ffcd..0a98b60a085 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt @@ -88,7 +88,18 @@ public suspend inline fun guaranteeCase( fa: suspend () -> A, crossinline finalizer: suspend (ExitCase) -> Unit ): A = finalizeCase({ fa() }) { ex -> - runReleaseAndRethrow(ex.errorOrNull) { finalizer(ex) } + try { + withContext(NonCancellable) { + finalizer(ex) + } + } catch (e: Throwable) { + e.nonFatalOrThrow() + when (ex) { + ExitCase.Completed -> throw e + is ExitCase.Failure -> ex.failure.addSuppressed(e) + is ExitCase.Cancelled -> ex.exception.addSuppressed(e) + } + } } /** @@ -210,18 +221,6 @@ public suspend inline fun bracketCase( return guaranteeCase({ use(acquired) }) { release(acquired, it) } } -@PublishedApi -internal suspend inline fun runReleaseAndRethrow(original: Throwable?, crossinline f: suspend () -> Unit) { - try { - withContext(NonCancellable) { - f() - } - } catch (e: Throwable) { - original?.addSuppressed(e.nonFatalOrThrow()) ?: throw e - } - original?.let { throw it } -} - @PublishedApi internal inline fun finalizeCase(block: () -> R, finalizer: (ExitCase) -> Unit): R { var exitCase: ExitCase = ExitCase.Completed