Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dual-effect Queue constructors, expose methods to change effect type of SyncRef and AsyncDeferred without mapK #1889

Closed
wants to merge 7 commits into from
Closed
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
22 changes: 14 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,12 @@ lazy val kernel = crossProject(JSPlatform, JVMPlatform)
libraryDependencies += "org.specs2" %%% "specs2-core" % Specs2Version % Test)
.settings(dottyLibrarySettings)
.settings(libraryDependencies += "org.typelevel" %%% "cats-core" % CatsVersion)
.jsSettings(
Compile / doc / sources := {
if (scalaVersion.value == "3.0.0-RC2")
Seq()
else
(Compile / doc / sources).value
})
.jsSettings(Compile / doc / sources := {
if (scalaVersion.value == "3.0.0-RC2")
Seq()
else
(Compile / doc / sources).value
})

/**
* Reference implementations (including a pure ConcurrentBracket), generic ScalaCheck
Expand Down Expand Up @@ -329,7 +328,14 @@ lazy val std = crossProject(JSPlatform, JVMPlatform)
else
"org.specs2" %%% "specs2-scalacheck" % Specs2Version % Test
},
libraryDependencies += "org.scalacheck" %%% "scalacheck" % ScalaCheckVersion % Test
libraryDependencies += "org.scalacheck" %%% "scalacheck" % ScalaCheckVersion % Test,
mimaBinaryIssueFilters ++= Seq(
// introduced by #1889, removal of private classes
ProblemFilters.exclude[MissingClassProblem]("cats.effect.std.Queue$AbstractQueue"),
ProblemFilters.exclude[MissingClassProblem]("cats.effect.std.Queue$BoundedQueue"),
ProblemFilters.exclude[MissingClassProblem]("cats.effect.std.Queue$DroppingQueue"),
ProblemFilters.exclude[MissingClassProblem]("cats.effect.std.Queue$CircularBufferQueue")
)
)

/**
Expand Down
62 changes: 57 additions & 5 deletions kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ object Deferred {
def apply[F[_], A](implicit F: GenConcurrent[F, _]): F[Deferred[F, A]] =
F.deferred[A]

/**
* Like `apply` but returns `AsyncDeferred`, which provides methods
* to change its effect type without use of a natural transformation.
*/
def async[F[_], A](implicit F: Async[F]): F[AsyncDeferred[F, A]] =
F.delay(new AsyncDeferred[F, A])

/**
* Like `apply` but returns the newly allocated Deferred directly
* instead of wrapping it in `F.delay`. This method is considered
Expand All @@ -91,11 +98,11 @@ object Deferred {
val dummyId = 0L
}

final class AsyncDeferred[F[_], A](implicit F: Async[F]) extends Deferred[F, A] {
// shared mutable state
private[this] val ref = new AtomicReference[State[A]](
State.Unset(LongMap.empty, State.initialId)
)
private[kernel] sealed trait AsyncDeferredSource[F[_], A]
extends DeferredSource[F, A]
with AsyncDeferredLike[A] {
protected implicit def F: Async[F]
private[Deferred] val ref: AtomicReference[State[A]]

def get: F[A] = {
// side-effectful
Expand Down Expand Up @@ -157,6 +164,12 @@ object Deferred {
case State.Unset(_, _) => None
}
}
}

sealed trait SyncDeferredSink[F[_], A] extends DeferredSink[F, A] { self =>
protected implicit def F: Sync[F]

private[Deferred] val ref: AtomicReference[State[A]]

def complete(a: A): F[Boolean] = {
def notifyReaders(readers: LongMap[A => Unit]): F[Unit] = {
Expand Down Expand Up @@ -192,6 +205,45 @@ object Deferred {

F.defer(loop())
}

/*
* Returns a new view of this `SyncDeferredSink` instance that shares
* the same state but which suspends operations in `G` rather than `F`.
*
* Similar to `mapK`, but requires a [[Sync]] instance instead of a natural
* transformation.
*/
def transformSync[G[_]](implicit G: Sync[G]): SyncDeferredSink[G, A] =
new SyncDeferredSink[G, A] {
override protected val F: Sync[G] = G
override private[Deferred] val ref: AtomicReference[State[A]] = self.ref
}
}

sealed trait AsyncDeferredLike[A] {
def transformAsync[G[_]](implicit G: Async[G]): AsyncDeferred[G, A]
}

sealed class AsyncDeferred[F[_], A](implicit protected val F: Async[F])
extends Deferred[F, A]
with AsyncDeferredSource[F, A]
with SyncDeferredSink[F, A] { self =>
// shared mutable state
private[Deferred] val ref = new AtomicReference[State[A]](
State.Unset(LongMap.empty, State.initialId)
)

/*
* Returns a new view of this `AsyncDeferred` instance that shares
* the same state but which suspends operations in `G` rather than `F`.
*
* Similar to `mapK`, but requires an [[Async]] instance instead of
* a natural transformation.
*/
def transformAsync[G[_]](implicit G: Async[G]): AsyncDeferred[G, A] =
new AsyncDeferred[G, A] {
override private[Deferred] val ref: AtomicReference[State[A]] = self.ref
}
}

implicit def catsInvariantForDeferred[F[_]: Functor]: Invariant[Deferred[F, *]] =
Expand Down
13 changes: 12 additions & 1 deletion kernel/shared/src/main/scala/cats/effect/kernel/Ref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ object Ref {
*/
def of[F[_], A](a: A)(implicit mk: Make[F]): F[Ref[F, A]] = mk.refOf(a)

/**
* Like [[of]], but returns `SyncRef` which provides the [[SyncRef#transformSync]]
* method to change its effect type without use of a natural transformation.
*/
def sync[F[_], A](a: A)(implicit F: Sync[F]): F[SyncRef[F, A]] = syncIn[F, F, A](a)

def syncIn[F[_], G[_], A](a: A)(implicit F: Sync[F], G: Sync[G]): F[SyncRef[G, A]] =
F.delay(new SyncRef(new AtomicReference(a)))

/**
* Creates a Ref starting with the value of the one in `source`.
*
Expand Down Expand Up @@ -269,7 +278,7 @@ object Ref {
def of[A](a: A): F[Ref[F, A]] = mk.refOf(a)
}

final private class SyncRef[F[_], A](ar: AtomicReference[A])(implicit F: Sync[F])
final class SyncRef[F[_], A] private[Ref] (ar: AtomicReference[A])(implicit F: Sync[F])
extends Ref[F, A] {
def get: F[A] = F.delay(ar.get)

Expand Down Expand Up @@ -350,6 +359,8 @@ object Ref {
val f = state.runF.value
modify(a => f(a).value)
}

def transformSync[G[_]: Sync]: SyncRef[G, A] = new SyncRef(ar)
}

final private[kernel] class TransformedRef[F[_], G[_], A](
Expand Down
Loading