Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Support for scala-native #619

Merged
merged 3 commits into from
Sep 4, 2023
Merged
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
44 changes: 14 additions & 30 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ jobs:
strategy:
fail-fast: false
matrix:
java: ['[email protected]']
scala: ['2.11.12', '2.12.17', '2.13.10', '3.2.0']
java: ['[email protected]', '11', '17']
scala: ['2.12.15', '2.13.11', '3.3.0']
platform: ['JVM', 'Native']
steps:
- name: Checkout current branch
uses: actions/checkout@v3.0.0
uses: actions/checkout@v3.3.0
with:
fetch-depth: 0
- name: Setup Scala and Java
Expand All @@ -60,37 +61,20 @@ jobs:
java-version: ${{ matrix.java }}
- name: Cache scala dependencies
uses: coursier/cache-action@v6
- name: Test 2.x
if: ${{ !startsWith(matrix.scala, '3.') }}
run: ./sbt ++${{ matrix.scala }}! test
- name: Test 3.x
if: ${{ startsWith(matrix.scala, '3.') }}
run: ./sbt ++${{ matrix.scala }}! testDotty

testJvms:
runs-on: ubuntu-20.04
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
java: ['11', '17']
steps:
- name: Checkout current branch
uses: actions/[email protected]
with:
fetch-depth: 0
- name: Setup Scala and Java
uses: olafurpg/setup-scala@v13
- name: Install libuv
if: matrix.platform == 'Native'
run: sudo apt-get update && sudo apt-get install -y libuv1-dev
- name: Set Swap Space
if: matrix.platform == 'Native'
uses: pierotofy/set-swap-space@master
with:
java-version: ${{ matrix.java }}
- name: Cache scala dependencies
uses: coursier/cache-action@v6
- name: Test
run: ./sbt test
swap-size-gb: 9
- name: Tests
run: ./sbt ++${{ matrix.scala }} zioNio${{ matrix.platform }}/test

ci:
runs-on: ubuntu-20.04
needs: [lint, mdoc, test, testJvms]
needs: [lint, mdoc, test]
steps:
- name: Report successful build
run: echo "ci passed"
Expand Down
21 changes: 1 addition & 20 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
rules = [
Disable
DisableSyntax
ExplicitResultTypes
LeakingImplicitClassVal
NoAutoTupling
NoValInForComprehension
OrganizeImports
ProcedureSyntax
RemoveUnused
]

Disable {
ifSynthetic = [
"scala/Option.option2Iterable"
"scala/Predef.any2stringadd"
]
}

OrganizeImports {
# Allign with IntelliJ IDEA so that they don't fight each other
groupedImports = Merge
}

RemoveUnused {
imports = false // handled by OrganizeImports
}
]
48 changes: 32 additions & 16 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ inThisBuild(
licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
developers := List(
Developer("jdegoes", "John De Goes", "[email protected]", url("http://degoes.net"))
)
),
scalaVersion := "2.13.11",
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision
)
)

Expand All @@ -19,29 +22,40 @@ addCommandAlias(
";zioNio/test;examples/test"
)

val zioVersion = "2.0.6"
val zioVersion = "2.0.16"

lazy val root = crossProject(JVMPlatform, NativePlatform)
.in(file("."))
.settings(publish / skip := true)
.aggregate(zioNio, examples)

lazy val zioNio = project
lazy val zioNio = crossProject(JVMPlatform, NativePlatform)
.in(file("nio"))
.settings(stdSettings("zio-nio"))
.settings(
.settings(crossProjectSettings)
.settings(buildInfoSettings("zio.nio"))
.settings(scala3Settings)
.jvmSettings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"org.scala-lang.modules" %% "scala-collection-compat" % "2.9.0",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0",
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))
)
.settings(scala3Settings)
.settings(
scalacOptions ++= {
if (scalaVersion.value == Scala3)
Seq.empty
else
Seq("-P:silencer:globalFilters=[zio.stacktracer.TracingImplicits.disableAutoTrace]")
}
.nativeSettings(Test / fork := false)
.nativeSettings(
libraryDependencies ++= Seq(
"dev.zio" %%% "zio" % zioVersion,
"dev.zio" %%% "zio-streams" % zioVersion,
"org.scala-lang.modules" %%% "scala-collection-compat" % "2.11.0",
"dev.zio" %%% "zio-test" % zioVersion % Test,
"dev.zio" %%% "zio-test-sbt" % zioVersion % Test,
"io.github.cquiroz" %%% "scala-java-time" % "2.5.0"
),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))
)

lazy val docs = project
Expand All @@ -51,21 +65,23 @@ lazy val docs = project
moduleName := "zio-nio-docs",
scalacOptions -= "-Yno-imports",
scalacOptions -= "-Xfatal-warnings",
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(zioNio),
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(zioNio.jvm),
ScalaUnidoc / unidoc / target := (LocalRootProject / baseDirectory).value / "website" / "static" / "api",
cleanFiles += (ScalaUnidoc / unidoc / target).value,
docusaurusCreateSite := docusaurusCreateSite.dependsOn(Compile / unidoc).value,
docusaurusPublishGhpages := docusaurusPublishGhpages.dependsOn(Compile / unidoc).value
)
.dependsOn(zioNio)
.dependsOn(zioNio.jvm)
.enablePlugins(MdocPlugin, DocusaurusPlugin, ScalaUnidocPlugin)

lazy val examples = project
lazy val examples = crossProject(JVMPlatform, NativePlatform)
.in(file("examples"))
.settings(stdSettings("examples"))
.settings(
publish / skip := true,
moduleName := "examples"
)
.settings(crossProjectSettings)
.settings(buildInfoSettings("examples"))
.settings(scala3Settings)
.dependsOn(zioNio)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package zio
package nio
package examples

import zio.nio.file.{Path, WatchService}
import zio.nio.file.Path
import zio.nio.file.WatchService

import zio.{Console, ZIOAppDefault}

import java.nio.file.{StandardWatchEventKinds, WatchEvent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package nio
package examples

import zio._
import zio.nio.channels.{BlockingNioOps, ServerSocketChannel, SocketChannel}
import zio.nio.channels.BlockingNioOps
import zio.nio.channels.{ServerSocketChannel, SocketChannel}

import zio.nio.charset.Charset
import zio.stream._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package zio.nio

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{Trace, ZIO}

import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.nio

import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.{ZIO, Trace}

import java.io.IOException
Expand Down
47 changes: 47 additions & 0 deletions nio/jvm/src/main/scala/zio/nio/ZStreamHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package zio.nio

import zio.stream.ZStream
import zio.{Scope, ZIO, Trace}

/**
* A mutable buffer of shorts.
*/
object ZStreamHelper {

/**
* Creates a stream from a Java stream
*/
final def fromJavaStream[A](stream: => java.util.stream.Stream[A])(implicit
trace: Trace
): ZStream[Any, Throwable, A] =
fromJavaStream(stream, ZStream.DefaultChunkSize)

/**
* Creates a stream from a Java stream
*/
final def fromJavaStream[A](
stream: => java.util.stream.Stream[A],
chunkSize: Int
)(implicit trace: Trace): ZStream[Any, Throwable, A] =
ZStream.fromJavaIteratorScoped(
ZIO.acquireRelease(ZIO.attempt(stream))(stream => ZIO.succeed(stream.close())).map(_.iterator()),
chunkSize
)

/**
* Creates a stream from a scoped Java stream
*/
final def fromJavaStreamScoped[R, A](stream: => ZIO[Scope with R, Throwable, java.util.stream.Stream[A]])(implicit
trace: Trace
): ZStream[R, Throwable, A] =
fromJavaStreamScoped[R, A](stream, ZStream.DefaultChunkSize)

/**
* Creates a stream from a scoped Java stream
*/
final def fromJavaStreamScoped[R, A](
stream: => ZIO[Scope with R, Throwable, java.util.stream.Stream[A]],
chunkSize: Int
)(implicit trace: Trace): ZStream[R, Throwable, A] =
ZStream.scoped[R](stream).flatMap(fromJavaStream(_, chunkSize))
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.nio
package channels
import zio._
import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.stream.{Stream, ZSink, ZStream}

import java.io.{EOFException, IOException}
Expand Down Expand Up @@ -149,7 +149,7 @@ object AsynchronousByteChannel {
)(op: C => CompletionHandler[A, Any] => Any)(implicit trace: Trace): IO[IOException, A] =
ZIO
.attempt(op(channel))
.flatMap(ZIO.asyncWithCompletionHandler)
.flatMap(ZIO.asyncWithCompletionHandler _)
.refineToOrDie[IOException]
.onInterrupt(ZIO.attempt(channel.close()).ignore)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zio.nio
package channels
import zio._
import zio.nio.file.Path
import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.stream.{Stream, ZSink, ZStream}

import java.io.{EOFException, IOException}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.nio
package channels

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{IO, Scope, Trace, UIO, ZIO}

import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.nio
package channels

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{IO, Scope, Trace, ZIO}

import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package channels

import zio.nio.channels.SelectionKey.Operation
import zio.nio.channels.spi.SelectorProvider
import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.{Exit, Fiber, IO, Scope, Trace, UIO, ZIO}

import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.nio.channels

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{IO, Trace, UIO, ZIO}
import zio.{Trace, UIO, ZIO}

import java.nio.{channels => jc}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package zio.nio
package channels

import com.github.ghik.silencer.silent
import zio.nio.channels.spi.SelectorProvider
import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.{Duration, IO, Scope, Trace, UIO, ZIO}

import java.io.IOException
Expand All @@ -27,7 +26,6 @@ final class Selector(private[nio] val selector: JSelector) extends IOCloseable {
def provider(implicit trace: Trace): UIO[SelectorProvider] =
ZIO.succeed(selector.provider()).map(new SelectorProvider(_))

@silent
def keys(implicit trace: Trace): UIO[Set[SelectionKey]] =
ZIO
.succeed(selector.keys())
Expand All @@ -40,7 +38,6 @@ final class Selector(private[nio] val selector: JSelector) extends IOCloseable {
* an object to the key set will cause an `UnsupportedOperationException` to be thrown. The selected-key set is not
* thread-safe.
*/
@silent
def selectedKeys(implicit trace: Trace): UIO[mutable.Set[SelectionKey]] =
ZIO
.succeed(selector.selectedKeys())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package zio.nio.channels.spi

import zio.nio.channels._
import zio.stacktracer.TracingImplicits.disableAutoTrace

import zio.{IO, Trace, ZIO}

import java.io.IOException
import java.net.ProtocolFamily
import java.nio.channels.spi.{SelectorProvider => JSelectorProvider}
import java.nio.{channels => jc}
import zio.nio.channels.{SocketChannel, ServerSocketChannel}
import zio.nio.channels.Selector
import zio.nio.channels.DatagramChannel
import zio.nio.channels.Pipe

final class SelectorProvider(private val selectorProvider: JSelectorProvider) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package zio.nio.file

import zio.{Trace, ZIO, Scope}
import java.io.IOException
import java.nio.{file => jf}
import zio.nio.IOCloseableManagement

trait FileSystemPlatformSpecific { self =>

def jFileSystem: jf.FileSystem

def newWatchService(implicit trace: Trace): ZIO[Scope, IOException, WatchService] =
ZIO.attemptBlockingIO(WatchService.fromJava(jFileSystem.newWatchService())).toNioScoped
}
Loading