Skip to content

Commit

Permalink
Inline runReleaseAndRethrow and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
kyay10 committed Nov 5, 2024
1 parent 5414813 commit 184927b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<arrow.fx.coroutines/ExitCase, kotlin/Unit>): #A // arrow.fx.coroutines/guaranteeCase|guaranteeCase(kotlin.coroutines.SuspendFunction0<0:0>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ExitCase,kotlin.Unit>){0§<kotlin.Any?>}[0]
final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/onCancel(kotlin.coroutines/SuspendFunction0<#A>, crossinline kotlin.coroutines/SuspendFunction0<kotlin/Unit>): #A // arrow.fx.coroutines/onCancel|onCancel(kotlin.coroutines.SuspendFunction0<0:0>;kotlin.coroutines.SuspendFunction0<kotlin.Unit>){0§<kotlin.Any?>}[0]
final suspend inline fun <#A: kotlin/Any?> arrow.fx.coroutines/resourceScope(kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ResourceScope, #A>): #A // arrow.fx.coroutines/resourceScope|resourceScope(kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ResourceScope,0:0>){0§<kotlin.Any?>}[0]
final suspend inline fun arrow.fx.coroutines/runReleaseAndRethrow(kotlin/Throwable?, crossinline kotlin.coroutines/SuspendFunction0<kotlin/Unit>) // arrow.fx.coroutines/runReleaseAndRethrow|runReleaseAndRethrow(kotlin.Throwable?;kotlin.coroutines.SuspendFunction0<kotlin.Unit>){}[0]
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ public suspend inline fun <A> 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)
}
}
}

/**
Expand Down Expand Up @@ -210,18 +221,6 @@ public suspend inline fun <A, B> 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 <R> finalizeCase(block: () -> R, finalizer: (ExitCase) -> Unit): R {
var exitCase: ExitCase = ExitCase.Completed
Expand Down

0 comments on commit 184927b

Please sign in to comment.