Skip to content

Commit

Permalink
Merge pull request #2884 from jbwheatley/stream-resourceK
Browse files Browse the repository at this point in the history
adds Stream.resourceK and Stream.resourceWeakK methods
  • Loading branch information
mpilquist authored Oct 17, 2022
2 parents 1c78d00 + 99121ef commit 700738a
Showing 1 changed file with 12 additions and 0 deletions.
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, *] =
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

0 comments on commit 700738a

Please sign in to comment.