Skip to content

Commit

Permalink
prePR
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Jun 6, 2023
1 parent a081729 commit df58ff1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 14 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ lazy val core = crossProject(JVMPlatform)
name := "sillio",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % "2.9.0",
"org.scalameta" %%% "munit" % "0.7.29" % Test)
"org.scalameta" %%% "munit" % "0.7.29" % Test
)
)

lazy val benchmarks = project.in(file("benchmarks"))
lazy val benchmarks = project
.in(file("benchmarks"))
.dependsOn(core.jvm)
.enablePlugins(JmhPlugin)
16 changes: 16 additions & 0 deletions core/src/main/scala/sillio/ArrayStack.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

private final class ArrayStack[A <: AnyRef] {
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/sillio/ByteStack.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

private final class ByteStack {
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/sillio/Fiber.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

trait Fiber[+A] {
Expand Down
37 changes: 31 additions & 6 deletions core/src/main/scala/sillio/IO.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

import cats.{Monad, StackSafeMonad}

import scala.concurrent.{Await, CancellationException, ExecutionContext, Future, Promise}
import scala.concurrent.{
Await,
CancellationException,
ExecutionContext,
Future,
Promise
}
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}

sealed abstract class IO[+A] {
def flatMap[B](f: A => IO[B]): IO[B] = IO.FlatMap(this, f)
def handleErrorWith[A2 >: A](f: Throwable => IO[A2]): IO[A2] = IO.HandleErrorWith(this, f)
def handleErrorWith[A2 >: A](f: Throwable => IO[A2]): IO[A2] =
IO.HandleErrorWith(this, f)

def start: IO[Fiber[A]] = IO.Start(this)

Expand All @@ -18,8 +41,8 @@ sealed abstract class IO[+A] {

fiber onComplete {
case Left(Some(t)) => promise.complete(Failure(t))
case Left(None) => promise.complete(Failure(new CancellationException))
case Right(a) => promise.complete(Success(a))
case Left(None) => promise.complete(Failure(new CancellationException))
case Right(a) => promise.complete(Success(a))
}

executor.execute(fiber)
Expand Down Expand Up @@ -56,11 +79,13 @@ object IO {
def tag: Int = 2
}

final case class HandleErrorWith[A](ioa: IO[A], f: Throwable => IO[A]) extends IO[A] {
final case class HandleErrorWith[A](ioa: IO[A], f: Throwable => IO[A])
extends IO[A] {
def tag: Int = 3
}

final case class Async[+A](k: (Either[Throwable, A] => Unit) => Unit) extends IO[A] {
final case class Async[+A](k: (Either[Throwable, A] => Unit) => Unit)
extends IO[A] {
def tag: Int = 4
}

Expand Down
27 changes: 24 additions & 3 deletions core/src/main/scala/sillio/IOFiber.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

import cats.syntax.all._
Expand All @@ -8,7 +24,9 @@ import scala.util.control.NonFatal

import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference}

final class IOFiber[A](_current: IO[A], executor: ExecutionContext) extends Fiber[A] with Runnable {
final class IOFiber[A](_current: IO[A], executor: ExecutionContext)
extends Fiber[A]
with Runnable {

private[this] var current: IO[Any] = _current

Expand All @@ -17,7 +35,8 @@ final class IOFiber[A](_current: IO[A], executor: ExecutionContext) extends Fibe

continuations.push(0)

private[this] val listeners: AtomicReference[Set[Either[Option[Throwable], A] => Unit]] =
private[this] val listeners
: AtomicReference[Set[Either[Option[Throwable], A] => Unit]] =
new AtomicReference(Set())

@volatile
Expand Down Expand Up @@ -152,7 +171,9 @@ final class IOFiber[A](_current: IO[A], executor: ExecutionContext) extends Fibe
}
}

private[this] def fireCompletion(outcome: Either[Option[Throwable], A]): Unit = {
private[this] def fireCompletion(
outcome: Either[Option[Throwable], A]
): Unit = {
val ls = listeners.getAndSet(null)
if (ls != null) {
this.outcome = outcome
Expand Down
26 changes: 23 additions & 3 deletions core/src/test/scala/sillio/IOSuite.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Daniel Spiewak
*
* 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 sillio

import cats.syntax.all._
Expand Down Expand Up @@ -96,7 +112,11 @@ class IOSuite extends FunSuite {
}

override def munitValueTransforms = super.munitValueTransforms ++ List(
new ValueTransform("IO", {
case ioa: IO[Any] => ioa.unsafeToFuture(ExecutionContext.global)
}))
new ValueTransform(
"IO",
{ case ioa: IO[Any] =>
ioa.unsafeToFuture(ExecutionContext.global)
}
)
)
}

0 comments on commit df58ff1

Please sign in to comment.