-
Notifications
You must be signed in to change notification settings - Fork 506
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
Fiber locals #1393
Fiber locals #1393
Changes from 10 commits
45c72e2
775d5a4
b3210b3
1928cce
2c66dba
efd820b
c9587a3
3026597
5d027c8
e1917ad
b038ad3
e274861
7f690fd
8cc384d
269ca45
bee13b1
c220cd1
4ea46ad
69d998e
b8c8b70
1de3310
f85827e
6cec2c6
dc7e11d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2020 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.local | ||
RaasAhsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import cats.effect.{IO, IOFiber} | ||
|
||
final class Local[A] private (index: Int, default: A) { | ||
RaasAhsan marked this conversation as resolved.
Show resolved
Hide resolved
RaasAhsan marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we extend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a review comment about it up here #1393 (comment) . I need to recall my thoughts on this but I was more inclined not to |
||
|
||
def get: IO[A] = | ||
IO.Local(state => (state, state.get(index).map(_.asInstanceOf[A]).getOrElse(default))) | ||
RaasAhsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def set(value: A): IO[Unit] = | ||
IO.Local(state => (state + (index -> value), ())) | ||
|
||
def update(f: A => A): IO[Unit] = | ||
get.flatMap(a => set(f(a))) | ||
|
||
def getAndSet(value: A): IO[A] = | ||
get <* set(value) | ||
|
||
def clear: IO[Unit] = | ||
IO.Local(state => (state - index, ())) | ||
|
||
} | ||
|
||
object Local { | ||
|
||
def apply[A](default: A): IO[Local[A]] = | ||
of(default) | ||
|
||
def of[A](default: A): IO[Local[A]] = | ||
IO { | ||
val index = IOFiber.nextLocalIndex() | ||
new Local(index, default) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2020 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.local | ||
|
||
import cats.effect.IO | ||
import cats.effect.kernel.Ref | ||
|
||
final class LocalRef[A] private (local: Local[Ref[IO, A]]) extends Ref[IO, A] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not crazy about the name, but I can't think of a better one. This is basically just a newtype around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep |
||
|
||
override def get: IO[A] = | ||
local.get.flatMap(_.get) | ||
|
||
override def set(a: A): IO[Unit] = | ||
local.get.flatMap(_.set(a)) | ||
|
||
override def access: IO[(A, A => IO[Boolean])] = | ||
local.get.flatMap(_.access) | ||
|
||
override def tryUpdate(f: A => A): IO[Boolean] = | ||
local.get.flatMap(_.tryUpdate(f)) | ||
|
||
override def tryModify[B](f: A => (A, B)): IO[Option[B]] = | ||
local.get.flatMap(_.tryModify(f)) | ||
|
||
override def update(f: A => A): IO[Unit] = | ||
local.get.flatMap(_.update(f)) | ||
|
||
override def modify[B](f: A => (A, B)): IO[B] = | ||
local.get.flatMap(_.modify(f)) | ||
|
||
override def tryModifyState[B](state: cats.data.State[A, B]): IO[Option[B]] = | ||
local.get.flatMap(_.tryModifyState(state)) | ||
|
||
override def modifyState[B](state: cats.data.State[A, B]): IO[B] = | ||
local.get.flatMap(_.modifyState(state)) | ||
|
||
} | ||
|
||
object LocalRef { | ||
|
||
def apply[A](default: A): IO[LocalRef[A]] = | ||
of(default) | ||
|
||
def of[A](default: A): IO[LocalRef[A]] = | ||
for { | ||
ref <- Ref.of[IO, A](default) | ||
local <- Local.of[Ref[IO, A]](ref) | ||
} yield new LocalRef(local) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2020 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats | ||
package effect | ||
package local | ||
|
||
class LocalRefSpec extends BaseSpec { | ||
|
||
"LocalRef" should { | ||
"return a default value" in ticked { implicit ticker => | ||
val io = LocalRef(0).flatMap(_.get) | ||
|
||
io must completeAs(0) | ||
} | ||
|
||
"set and get a value" in ticked { implicit ticker => | ||
val io = for { | ||
local <- LocalRef(0) | ||
_ <- local.set(10) | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"preserve locals across async boundaries" in ticked { implicit ticker => | ||
val io = for { | ||
local <- LocalRef(0) | ||
_ <- local.set(10) | ||
_ <- IO.cede | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"children fibers can read locals" in ticked { implicit ticker => | ||
val io = for { | ||
local <- LocalRef(0) | ||
_ <- local.set(10) | ||
f <- local.get.start | ||
value <- f.joinAndEmbedNever | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"child local manipulation is visible to parents" in ticked { implicit ticker => | ||
val io = for { | ||
local <- LocalRef(0) | ||
f <- local.set(20).start | ||
_ <- f.join | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(20) | ||
} | ||
|
||
"parent local manipulation is visible to children" in ticked { implicit ticker => | ||
val io = for { | ||
local <- LocalRef(0) | ||
d1 <- Deferred[IO, Unit] | ||
f <- (d1.get *> local.get).start | ||
_ <- local.set(10) | ||
_ <- d1.complete(()) | ||
value <- f.joinAndEmbedNever | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2020 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats | ||
package effect | ||
package local | ||
|
||
class LocalSpec extends BaseSpec { | ||
|
||
"Local" should { | ||
"return a default value" in ticked { implicit ticker => | ||
val io = Local(0).flatMap(_.get) | ||
|
||
io must completeAs(0) | ||
} | ||
|
||
"set and get a value" in ticked { implicit ticker => | ||
val io = for { | ||
local <- Local(0) | ||
_ <- local.set(10) | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"preserve locals across async boundaries" in ticked { implicit ticker => | ||
val io = for { | ||
local <- Local(0) | ||
_ <- local.set(10) | ||
_ <- IO.cede | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"copy locals to children fibers" in ticked { implicit ticker => | ||
val io = for { | ||
local <- Local(0) | ||
_ <- local.set(10) | ||
f <- local.get.start | ||
value <- f.joinAndEmbedNever | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"child local manipulation is invisible to parents" in ticked { implicit ticker => | ||
val io = for { | ||
local <- Local(10) | ||
f <- local.set(20).start | ||
_ <- f.join | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"parent local manipulation is invisible to children" in ticked { implicit ticker => | ||
val io = for { | ||
local <- Local(0) | ||
d1 <- Deferred[IO, Unit] | ||
f <- (d1.get *> local.get).start | ||
_ <- local.set(10) | ||
_ <- d1.complete(()) | ||
value <- f.joinAndEmbedNever | ||
} yield value | ||
|
||
io must completeAs(0) | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two options here for holding local state: