Skip to content

Commit

Permalink
Resource Arrow 2.0 (#2786)
Browse files Browse the repository at this point in the history
* Flatten Resource ADT, maintain API
  • Loading branch information
nomisRev committed Sep 3, 2022
1 parent 83ecb7b commit cc8a9fd
Show file tree
Hide file tree
Showing 41 changed files with 1,488 additions and 1,818 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ import kotlin.coroutines.resumeWithException
* <!--- INCLUDE
* import arrow.core.continuations.effect
* import arrow.fx.coroutines.ExitCase
* import arrow.fx.coroutines.Resource
* import arrow.fx.coroutines.fromAutoCloseable
* import arrow.fx.coroutines.releaseCase
* import arrow.fx.coroutines.ResourceScope
* import arrow.fx.coroutines.autoCloseable
* import arrow.fx.coroutines.resourceScope
* import io.kotest.assertions.fail
* import io.kotest.matchers.shouldBe
* import io.kotest.matchers.types.shouldBeTypeOf
Expand All @@ -402,15 +402,18 @@ import kotlin.coroutines.resumeWithException
* val error = "Error"
* val exit = CompletableDeferred<ExitCase>()
*
* fun bufferedReader(path: String): Resource<BufferedReader> =
* Resource.fromAutoCloseable { File(path).bufferedReader() }
* .releaseCase { _, exitCase -> exit.complete(exitCase) }
* suspend fun ResourceScope.bufferedReader(path: String): BufferedReader =
* autoCloseable { File(path).bufferedReader() }.also {
* onRelease { exitCase -> exit.complete(exitCase) }
* }
*
* effect<String, Int> {
* val lineCount = bufferedReader("build.gradle.kts")
* .use { reader -> shift<Int>(error) }
* lineCount
* }.fold({ it shouldBe error }, { fail("Int can never be the result") })
* resourceScope {
* effect<String, Int> {
* val reader = bufferedReader("build.gradle.kts")
* shift<Int>(error)
* reader.lineSequence().count()
* }.fold({ it shouldBe error }, { fail("Int can never be the result") })
* }
* exit.await().shouldBeTypeOf<ExitCase.Cancelled>()
* }
* ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package arrow.core.examples.exampleEffectGuide09

import arrow.core.continuations.effect
import arrow.fx.coroutines.ExitCase
import arrow.fx.coroutines.Resource
import arrow.fx.coroutines.fromAutoCloseable
import arrow.fx.coroutines.releaseCase
import arrow.fx.coroutines.ResourceScope
import arrow.fx.coroutines.autoCloseable
import arrow.fx.coroutines.resourceScope
import io.kotest.assertions.fail
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf
Expand All @@ -17,14 +17,17 @@ suspend fun main() {
val error = "Error"
val exit = CompletableDeferred<ExitCase>()

fun bufferedReader(path: String): Resource<BufferedReader> =
Resource.fromAutoCloseable { File(path).bufferedReader() }
.releaseCase { _, exitCase -> exit.complete(exitCase) }
suspend fun ResourceScope.bufferedReader(path: String): BufferedReader =
autoCloseable { File(path).bufferedReader() }.also {
onRelease { exitCase -> exit.complete(exitCase) }
}

effect<String, Int> {
val lineCount = bufferedReader("build.gradle.kts")
.use { reader -> shift<Int>(error) }
lineCount
}.fold({ it shouldBe error }, { fail("Int can never be the result") })
resourceScope {
effect<String, Int> {
val reader = bufferedReader("build.gradle.kts")
shift<Int>(error)
reader.lineSequence().count()
}.fold({ it shouldBe error }, { fail("Int can never be the result") })
}
exit.await().shouldBeTypeOf<ExitCase.Cancelled>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class arrow/fx/coroutines/Predef_testKt {
}

public final class arrow/fx/coroutines/Predef_test_jvmKt {
public static final fun getSingle ()Larrow/fx/coroutines/Resource;
public static final fun getSingle ()Lkotlin/jvm/functions/Function2;
public static final fun getSingleThreadName ()Ljava/lang/String;
public static final fun getThreadName ()Lkotlin/jvm/functions/Function1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.coroutines.CoroutineContext
public val singleThreadName: String = "single"

@Deprecated(deprecateArrowTestModules)
public val single: Resource<CoroutineContext> = Resource.singleThreadContext(singleThreadName)
public val single: Resource<CoroutineContext> = singleThreadContext("single")

@Deprecated(deprecateArrowTestModules)
public val threadName: suspend () -> String =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package arrow.fx.coroutines

import arrow.core.Either
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
Expand All @@ -14,38 +13,38 @@ import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn

class PredefTest : ArrowFxSpec(
spec = {

"suspended always suspends" {
checkAll(Arb.int()) { i ->
val promise = CompletableDeferred<Int>()

val x = i.suspended()
.startCoroutineUninterceptedOrReturn(
Continuation(EmptyCoroutineContext) {
promise.completeWith(it)
}
)

x shouldBe COROUTINE_SUSPENDED
promise.await() shouldBe i
}
}

"shift" {
checkAll(Arb.string(), Arb.string()) { a, b ->
val t0 = threadName.invoke()

Resource.singleThreadContext(a)
.zip(Resource.singleThreadContext(b))
.use { (ui, io) ->
t0 shouldBe threadName.invoke()

ui.shift()
threadName.invoke() shouldBe a

io.shift()
threadName.invoke() shouldBe b
}
val t0 = Thread.currentThread().name
resourceScope {
val ui = singleThreadContext(a)
val io = singleThreadContext(b)
t0 shouldBe Thread.currentThread().name
ui.shift()
Thread.currentThread().name shouldBe a
io.shift()
Thread.currentThread().name shouldBe b
}
}
}
}
Expand Down
128 changes: 41 additions & 87 deletions arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
public final class arrow/fx/coroutines/AcquireStep {
public static final field INSTANCE Larrow/fx/coroutines/AcquireStep;
}

public abstract interface class arrow/fx/coroutines/Atomic {
public static final field Companion Larrow/fx/coroutines/Atomic$Companion;
public abstract fun access (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -301,80 +305,48 @@ public final class arrow/fx/coroutines/Race3Kt {
public static synthetic fun raceN$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public abstract class arrow/fx/coroutines/Resource {
public static final field Companion Larrow/fx/coroutines/Resource$Companion;
public final fun ap (Larrow/fx/coroutines/Resource;)Larrow/fx/coroutines/Resource;
public final fun flatMap (Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public final fun map (Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
public final fun parZip (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public final fun parZip (Lkotlin/coroutines/CoroutineContext;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public static synthetic fun parZip$default (Larrow/fx/coroutines/Resource;Lkotlin/coroutines/CoroutineContext;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)Larrow/fx/coroutines/Resource;
public final fun tap (Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
public final fun use (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun zip (Larrow/fx/coroutines/Resource;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function10;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function9;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function8;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function7;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function6;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function5;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function4;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public final fun zip (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
}

public final class arrow/fx/coroutines/Resource$Allocate : arrow/fx/coroutines/Resource {
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)V
public final fun getAcquire ()Lkotlin/jvm/functions/Function1;
public final fun getRelease ()Lkotlin/jvm/functions/Function3;
}

public final class arrow/fx/coroutines/Resource$Bind : arrow/fx/coroutines/Resource {
public fun <init> (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function1;)V
public final fun getF ()Lkotlin/jvm/functions/Function1;
public final fun getSource ()Larrow/fx/coroutines/Resource;
}

public final class arrow/fx/coroutines/Resource$Companion {
public final fun defer (Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public final fun getUnit ()Larrow/fx/coroutines/Resource;
public final fun invoke (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public final fun just (Ljava/lang/Object;)Larrow/fx/coroutines/Resource;
}

public final class arrow/fx/coroutines/Resource$Defer : arrow/fx/coroutines/Resource {
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final fun getResource ()Lkotlin/jvm/functions/Function1;
}

public final class arrow/fx/coroutines/Resource$Dsl : arrow/fx/coroutines/Resource {
public fun <init> (Lkotlin/jvm/functions/Function2;)V
public final fun component1 ()Lkotlin/jvm/functions/Function2;
public final fun copy (Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource$Dsl;
public static synthetic fun copy$default (Larrow/fx/coroutines/Resource$Dsl;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Larrow/fx/coroutines/Resource$Dsl;
public fun equals (Ljava/lang/Object;)Z
public final fun getDsl ()Lkotlin/jvm/functions/Function2;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
public abstract interface annotation class arrow/fx/coroutines/ResourceDSL : java/lang/annotation/Annotation {
}

public final class arrow/fx/coroutines/ResourceExtensionsKt {
public static final fun fromAutoCloseable (Larrow/fx/coroutines/Resource$Companion;Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public static final fun fromCloseable (Larrow/fx/coroutines/Resource$Companion;Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public static final fun fromExecutor (Larrow/fx/coroutines/Resource$Companion;Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public static final fun singleThreadContext (Larrow/fx/coroutines/Resource$Companion;Ljava/lang/String;)Larrow/fx/coroutines/Resource;
public static final fun autoCloseable (Larrow/fx/coroutines/ResourceScope;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun autoCloseable (Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function2;
public static synthetic fun autoCloseable$default (Larrow/fx/coroutines/ResourceScope;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun autoCloseable$default (Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
public static final fun closeable (Larrow/fx/coroutines/ResourceScope;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun closeable (Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function2;
public static synthetic fun closeable$default (Larrow/fx/coroutines/ResourceScope;Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun closeable$default (Lkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
public static final fun executor-KLykuaI (JLkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function2;
public static synthetic fun executor-KLykuaI$default (JLkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
public static final fun executor-WPwdCS8 (Larrow/fx/coroutines/ResourceScope;JLkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun executor-WPwdCS8$default (Larrow/fx/coroutines/ResourceScope;JLkotlinx/coroutines/CoroutineDispatcher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun fixedThreadPoolContext (ILjava/lang/String;)Lkotlin/jvm/functions/Function2;
public static final fun fixedThreadPoolContext (Larrow/fx/coroutines/ResourceScope;ILjava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun singleThreadContext (Larrow/fx/coroutines/ResourceScope;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun singleThreadContext (Ljava/lang/String;)Lkotlin/jvm/functions/Function2;
}

public final class arrow/fx/coroutines/ResourceKt {
public static final fun asFlow (Larrow/fx/coroutines/Resource;)Lkotlinx/coroutines/flow/Flow;
public static final fun release (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
public static final fun release-zgiIeyo (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
public static final fun releaseCase (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public static final fun releaseCase-zgiIeyo (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource;
public static final fun resource (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function1;
public static final fun sequence (Ljava/lang/Iterable;)Larrow/fx/coroutines/Resource;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public static final fun traverseResource (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Resource;
public static final fun asFlow (Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/flow/Flow;
public static final fun resource (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2;
public static final fun resource (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
public static final fun resourceScope (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun use (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public abstract interface class arrow/fx/coroutines/ResourceScope {
public abstract fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun install (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun onRelease (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun release (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun releaseCase (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class arrow/fx/coroutines/ResourceScope$DefaultImpls {
public static fun onRelease (Larrow/fx/coroutines/ResourceScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun release (Larrow/fx/coroutines/ResourceScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static fun releaseCase (Larrow/fx/coroutines/ResourceScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public abstract class arrow/fx/coroutines/Schedule {
Expand Down Expand Up @@ -494,24 +466,6 @@ public final class arrow/fx/coroutines/ScheduleKt {
public static final fun retryOrElseEither (Larrow/fx/coroutines/Schedule;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class arrow/fx/coroutines/Use {
public static final synthetic fun box-impl (Lkotlin/jvm/functions/Function1;)Larrow/fx/coroutines/Use;
public static fun constructor-impl (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function1;
public fun equals (Ljava/lang/Object;)Z
public static fun equals-impl (Lkotlin/jvm/functions/Function1;Ljava/lang/Object;)Z
public static final fun equals-impl0 (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Z
public fun hashCode ()I
public static fun hashCode-impl (Lkotlin/jvm/functions/Function1;)I
public fun toString ()Ljava/lang/String;
public static fun toString-impl (Lkotlin/jvm/functions/Function1;)Ljava/lang/String;
public final synthetic fun unbox-impl ()Lkotlin/jvm/functions/Function1;
}

public final class arrow/fx/coroutines/continuations/ResourceKt {
public static final fun resource (Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource;
}

public abstract interface class arrow/fx/coroutines/continuations/ResourceScope {
public abstract fun bind (Larrow/fx/coroutines/Resource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract interface annotation class arrow/fx/coroutines/ScopeDSL : java/lang/annotation/Annotation {
}

Loading

0 comments on commit cc8a9fd

Please sign in to comment.