Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ lazy val kernel = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(
name := "cats-effect-kernel",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % CatsVersion
"org.typelevel" %%% "cats-core" % CatsVersion,
"org.typelevel" %%% "cats-mtl" % CatsMtlVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a meaningful extension of the dependency surface area. It's not something I would rule out, since most people depend on core or std, but it's still not something to do lightly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree; however, I thought having a PR would be more compelling than just opening an issue for it

),
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[MissingClassProblem]("cats.effect.kernel.Ref$SyncRef"),
Expand Down Expand Up @@ -501,7 +502,9 @@ lazy val laws = crossProject(JSPlatform, JVMPlatform, NativePlatform)
name := "cats-effect-laws",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-laws" % CatsVersion,
"org.typelevel" %%% "discipline-munit" % DisciplineMUnitVersion % Test)
"org.typelevel" %%% "cats-mtl-laws" % CatsMtlVersion % Test,
"org.typelevel" %%% "discipline-munit" % DisciplineMUnitVersion % Test
)
)

/**
Expand Down
36 changes: 36 additions & 0 deletions kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.data.Kleisli
import cats.effect.kernel.Resource.Pure
import cats.effect.kernel.implicits._
import cats.effect.kernel.instances.spawn
import cats.mtl.{LiftKind, LiftValue}
import cats.syntax.all._

import scala.annotation.tailrec
Expand Down Expand Up @@ -1275,6 +1276,10 @@ private[effect] trait ResourceHOInstances0 extends ResourceHOInstances1 {
def K = K0
def G = G0
}

implicit def catsEffectLiftKindForResource[F[_]](
implicit F: MonadCancel[F, ?]): LiftKind[F, Resource[F, *]] =
liftKindImpl(F)
}

private[effect] trait ResourceHOInstances1 extends ResourceHOInstances2 {
Expand All @@ -1289,6 +1294,25 @@ private[effect] trait ResourceHOInstances1 extends ResourceHOInstances2 {
def F = F0
def rootCancelScope = F0.rootCancelScope
}

protected[this] def liftKindImpl[F[_]](F: MonadCancel[F, ?]): LiftKind[F, Resource[F, *]] =
new LiftKind[F, Resource[F, *]] {
implicit val applicativeF: MonadCancel[F, ?] = F
val applicativeG: Applicative[Resource[F, *]] = catsEffectMonadForResource
def apply[A](fa: F[A]): Resource[F, A] = Resource.eval(fa)
def limitedMapK[A](ga: Resource[F, A])(scope: F ~> F): Resource[F, A] =
ga.mapK(scope)
}

implicit def catsEffectLiftKindForResourceComposed[F[_], G[_]](
implicit inner: LiftKind[F, G],
G: MonadCancel[G, ?]
): LiftKind[F, Resource[G, *]] =
inner.andThen(liftKindImpl(G))

implicit def catsEffectLiftValueForResource[F[_]](
implicit F: Applicative[F]): LiftValue[F, Resource[F, *]] =
liftValueImpl(F)
}

private[effect] trait ResourceHOInstances2 extends ResourceHOInstances3 {
Expand All @@ -1308,6 +1332,18 @@ private[effect] trait ResourceHOInstances2 extends ResourceHOInstances3 {

final implicit def catsEffectDeferForResource[F[_]]: Defer[Resource[F, *]] =
new ResourceDefer[F]

protected[this] def liftValueImpl[F[_]](F: Applicative[F]): LiftValue[F, Resource[F, *]] =
new LiftValue[F, Resource[F, *]] {
val applicativeF: Applicative[F] = F
val applicativeG: Applicative[Resource[F, *]] = catsEffectMonadForResource
def apply[A](fa: F[A]): Resource[F, A] = Resource.eval(fa)
}

implicit def catsEffectLiftValueForResourceComposed[F[_], G[_]](
implicit inner: LiftValue[F, G]
): LiftValue[F, Resource[G, *]] =
inner.andThen(liftValueImpl(inner.applicativeG))
}

private[effect] trait ResourceHOInstances3 extends ResourceHOInstances4 {
Expand Down
82 changes: 82 additions & 0 deletions laws/shared/src/test/scala/cats/effect/kernel/ResourceSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cats
package effect
package kernel

import cats.data.OptionT
import cats.effect.kernel.testkit.PureConcGenerators._
import cats.effect.kernel.testkit.pure._
import cats.laws.discipline.arbitrary.catsLawsArbitraryForOptionT
import cats.mtl.laws.discipline.{LiftKindTests, LiftValueTests}
import cats.syntax.flatMap._
import cats.syntax.functor._

import org.scalacheck.{Arbitrary, Gen}

import munit.DisciplineSuite

class ResourceSuite extends DisciplineSuite {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, there are already resource tests in core, though I don't object to having a few more in laws.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the issue is that these tests depend on testkit (for PureConc), which core's tests do not have access to

type PCT[A] = PureConc[Throwable, A]

private[this] val counter: PCT[Ref[PCT, Long]] = Concurrent[PCT].ref(0)

implicit val eqThrowable: Eq[Throwable] = Eq.fromUniversalEquals

implicit val arbitraryScope: Arbitrary[PCT ~> PCT] =
Arbitrary {
Gen.const {
new (PCT ~> PCT) {
def apply[A](fa: PCT[A]): PCT[A] =
for {
ref <- counter
res <- ref.update(_ + 1) >> fa
} yield res
}
}
}

implicit def arbitraryPCTUnit(): Arbitrary[PCT[Unit]] =
Arbitrary {
Gen.const {
counter.flatMap(_.update(_ * 10)).void
}
}

implicit def eqResource[F[_], A](
implicit F: MonadCancelThrow[F],
eqFAUnit: Eq[F[(A, Unit)]]
): Eq[Resource[F, A]] =
Eq.by {
_.allocated.flatMap {
case (acquire, release) =>
release.map(acquire -> _)
}
}

implicit def arbitraryResource[F[_]: Functor, A](
implicit arbFA: Arbitrary[F[A]],
arbFUnit: Arbitrary[F[Unit]]
): Arbitrary[Resource[F, A]] =
Arbitrary {
for {
acquire <- arbFA.arbitrary
release <- arbFUnit.arbitrary
} yield Resource(acquire.map(_ -> release))
}

checkAll(
"LiftValue[PureConc, Resource[PureConc, *]]",
LiftValueTests[PCT, Resource[PCT, *]].liftValue[Int, Int]
)
checkAll(
"LiftValue[PureConc, Resource[OptionT[PureConc, *], *]]",
LiftValueTests[PCT, Resource[OptionT[PCT, *], *]].liftValue[Int, Int]
)
checkAll(
"LiftKind[PureConc, Resource[PureConc, *]]",
LiftKindTests[PCT, Resource[PCT, *]].liftKind[Int, Int]
)
checkAll(
"LiftKind[PureConc, Resource[OptionT[PureConc, *], *]]",
LiftKindTests[PCT, Resource[OptionT[PCT, *], *]].liftKind[Int, Int]
)
}