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

adds Stream.resourceK and Stream.resourceWeakK methods #2884

Merged
merged 3 commits into from
Oct 17, 2022
Merged
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
12 changes: 12 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3672,6 +3672,12 @@ object Stream extends StreamLowPriority {
def resource[F[_], O](r: Resource[F, O])(implicit F: MonadCancel[F, _]): Stream[F, O] =
resourceWeak(r).scope

/** Same as [[resource]], but expressed as a FunctionK. */
def resourceK[F[_]](implicit F: MonadCancel[F, _]): Resource[F, *] ~> Stream[F, *] =
jbwheatley marked this conversation as resolved.
Show resolved Hide resolved
new (Resource[F, *] ~> Stream[F, *]) {
override def apply[A](fa: Resource[F, A]): Stream[F, A] = resource[F, A](fa)
}

/** Like [[resource]] but does not introduce a scope, allowing finalization to occur after
* subsequent appends or other scope-preserving transformations.
*
Expand All @@ -3691,6 +3697,12 @@ object Stream extends StreamLowPriority {
case Resource.Pure(o) => Stream.emit(o)
}

/** Same as [[resourceWeak]], but expressed as a FunctionK. */
def resourceWeakK[F[_]](implicit F: MonadCancel[F, _]): Resource[F, *] ~> Stream[F, *] =
new (Resource[F, *] ~> Stream[F, *]) {
override def apply[A](fa: Resource[F, A]): Stream[F, A] = resourceWeak[F, A](fa)
}

/** 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))
Expand Down