Skip to content
New issue

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

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

Already on GitHub? # to your account

AutoCloseable support #2803

Merged
merged 5 commits into from
Jan 28, 2022
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
14 changes: 13 additions & 1 deletion core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3599,7 +3599,7 @@ object Stream extends StreamLowPriority {
/** Alias for `eval(fo).repeat`. */
def repeatEval[F[_], O](fo: F[O]): Stream[F, O] = eval(fo).repeat

/** Converts the supplied resource in to a singleton stream. */
/** Converts the supplied resource into a singleton stream. */
def resource[F[_], O](r: Resource[F, O])(implicit F: MonadCancel[F, _]): Stream[F, O] =
resourceWeak(r).scope

Expand All @@ -3622,6 +3622,18 @@ object Stream extends StreamLowPriority {
case Resource.Pure(o) => Stream.emit(o)
}

/** Converts the supplied [[java.lang.Autoclosable]] into a singleton stream. */
def fromAutoCloseable[F[_]: Sync, O <: AutoCloseable](fo: F[O]): Stream[F, O] =
Stream.resource(Resource.fromAutoCloseable(fo))

/** Like [[fromAutoClosable]] but does not introduce a scope, allowing finalization to occur after
* subsequent appends or other scope-preserving transformations.
*
* Scopes can be manually introduced via [[Stream#scope]] if desired.
*/
def fromAutoCloseableWeak[F[_]: Sync, O <: AutoCloseable](fo: F[O]): Stream[F, O] =
Stream.resourceWeak(Resource.fromAutoCloseable(fo))

/** Retries `fo` on failure, returning a singleton stream with the
* result of `fo` as soon as it succeeds.
*
Expand Down
27 changes: 27 additions & 0 deletions core/shared/src/test/scala/fs2/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Prop.forAll
import org.scalacheck.effect.PropF.forAllF
import fs2.concurrent.SignallingRef
import java.util.concurrent.atomic.AtomicInteger

class StreamSuite extends Fs2Suite {

Expand Down Expand Up @@ -602,6 +603,32 @@ class StreamSuite extends Fs2Suite {
}
}

test("fromAutoCloseable") {
class Auto(var closed: Boolean = false) extends AutoCloseable {
override def close(): Unit =
closed = true
}

Stream.fromAutoCloseable(IO(new Auto())).compile.toList.flatMap {
case h :: Nil => IO(assert(h.closed))
case _ => IO(fail("Did not close AutoClosable"))
}
}

test("fromAutoCloseableWeak") {
val counter = new AtomicInteger()
class Auto(var i: Int = 0) extends AutoCloseable {
override def close(): Unit =
i = counter.incrementAndGet()
}

(Stream.fromAutoCloseableWeak(IO(new Auto()))
++ Stream.fromAutoCloseable(IO(new Auto()))).compile.toList.flatMap {
case x :: y :: Nil => IO(assertEquals(x.i, 2)) >> IO(assertEquals(y.i, 1))
case _ => IO(fail("Did not close AutoClosable"))
}
}

group("resource safety") {
test("1") {
forAllF { (s1: Stream[Pure, Int]) =>
Expand Down