Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/zio/Managed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ object Managed {
final def makeExit[E, A](acquire: IO[E, A])(release: (A, Exit[Any, Any]) => UIO[Any]): Managed[E, A] =
ZManaged.makeExit(acquire)(release)

/**
* See [[zio.ZManaged.makeInterruptible]]
*/
final def makeInterruptible[R, E, A](acquire: IO[E, A])(release: A => UIO[Any]): Managed[E, A] =
ZManaged.makeInterruptible(acquire)(release)

/**
* See [[zio.ZManaged.mergeAll]]
*/
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self =>
self &&& ZIO.identity[R1]

/**
* Runs the specified effect if this effect is interrupted.
* Runs the specified effect if this effect is externally interrupted.
*/
final def onInterrupt[R1 <: R](cleanup: ZIO[R1, Nothing, Any]): ZIO[R1, E, A] =
self.ensuring(
Expand Down Expand Up @@ -2622,7 +2622,7 @@ object ZIO extends ZIOFunctions {
def apply[R1 <: R](release: A => ZIO[R1, Nothing, Any]): BracketRelease[R1, E, A] =
new BracketRelease[R1, E, A](acquire, release)
}
class BracketRelease[-R, +E, +A](acquire: ZIO[R, E, A], release: A => ZIO[R, Nothing, Any]) {
final class BracketRelease[-R, +E, +A](acquire: ZIO[R, E, A], release: A => ZIO[R, Nothing, Any]) {
def apply[R1 <: R, E1 >: E, B](use: A => ZIO[R1, E1, B]): ZIO[R1, E1, B] =
ZIO.bracket(acquire, release, use)
}
Expand All @@ -2633,7 +2633,7 @@ object ZIO extends ZIOFunctions {
): BracketExitRelease[R1, E, E1, A, B] =
new BracketExitRelease(acquire, release)
}
class BracketExitRelease[-R, +E, E1, +A, B](
final class BracketExitRelease[-R, +E, E1, +A, B](
acquire: ZIO[R, E, A],
release: (A, Exit[E1, B]) => ZIO[R, Nothing, Any]
) {
Expand Down
40 changes: 20 additions & 20 deletions core/shared/src/main/scala/zio/ZManaged.scala
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,9 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
ZManaged {
Ref.make[Exit[Any, Any] => ZIO[R1, Nothing, Any]](_ => UIO.unit).map { finalizer =>
Reservation(
acquire = ZIO.uninterruptibleMask { restore =>
for {
res <- self.reserve
exitA <- restore(res.acquire).run
_ <- finalizer.set(exitU => res.release(exitU).ensuring(cleanup(exitA)))
a <- ZIO.done(exitA)
} yield a
},
acquire = ZIO.bracketExit(self.reserve)(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what's the motivation for the change? The differences are mainly correct usage of ZIOFn and error preservation (which shouldn't matter here because finalizer.set is a UIO)

Copy link
Member Author

@neko-kai neko-kai Oct 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iravid Less low-level code; also this is the only instance of uninterruptibleMask(restore(_).catch) aside of bracketExit in core and turns out it just duplicates it – making it just reuse bracketExit also safeguards it against any changes to finalizer behavior :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense!

(res, exitA: Exit[E, A]) => finalizer.set(exitU => res.release(exitU).ensuring(cleanup(exitA)))
)(_.acquire),
release = e => finalizer.get.flatMap(f => f(e))
)
}
Expand All @@ -427,14 +422,9 @@ final case class ZManaged[-R, +E, +A](reserve: ZIO[R, E, Reservation[R, E, A]])
ZManaged {
Ref.make[Exit[Any, Any] => ZIO[R1, Nothing, Any]](_ => UIO.unit).map { finalizer =>
Reservation(
acquire = ZIO.uninterruptibleMask { restore =>
for {
res <- self.reserve
exitA <- restore(res.acquire).run
_ <- finalizer.set(exitU => cleanup(exitA).ensuring(res.release(exitU)))
a <- ZIO.done(exitA)
} yield a
},
acquire = ZIO.bracketExit(self.reserve)(
(res, exitA: Exit[E, A]) => finalizer.set(exitU => cleanup(exitA).ensuring(res.release(exitU)))
)(_.acquire),
release = e => finalizer.get.flatMap(f => f(e))
)
}
Expand Down Expand Up @@ -1044,28 +1034,38 @@ object ZManaged {
final val interrupt: ZManaged[Any, Nothing, Nothing] = halt(Cause.interrupt)

/**
* Lifts a `ZIO[R, E, R]` into `ZManaged[R, E, R]` with a release action.
* Lifts a `ZIO[R, E, A]` into `ZManaged[R, E, A]` with a release action.
* The acquire and release actions will be performed uninterruptibly.
*/
final def make[R, E, A](acquire: ZIO[R, E, A])(release: A => ZIO[R, Nothing, Any]): ZManaged[R, E, A] =
ZManaged(acquire.map(r => Reservation(IO.succeed(r), _ => release(r))))

/**
* Lifts a synchronous effect into `ZManaged[R, Throwable, R]` with a release action.
* Lifts a synchronous effect into `ZManaged[R, Throwable, A]` with a release action.
* The acquire and release actions will be performed uninterruptibly.
*/
final def makeEffect[R, A](acquire: => A)(release: A => _): ZManaged[R, Throwable, A] =
final def makeEffect[R, A](acquire: => A)(release: A => Any): ZManaged[R, Throwable, A] =
make(Task(acquire))(a => Task(release(a)).orDie)

/**
* Lifts a `ZIO[R, E, R]` into `ZManaged[R, E, R]` with a release action that handles `Exit`.
* Lifts a `ZIO[R, E, A]` into `ZManaged[R, E, A]` with a release action that handles `Exit`.
* The acquire and release actions will be performed uninterruptibly.
*/
final def makeExit[R, E, A](
acquire: ZIO[R, E, A]
)(release: (A, Exit[Any, Any]) => ZIO[R, Nothing, Any]): ZManaged[R, E, A] =
ZManaged(acquire.map(r => Reservation(IO.succeed(r), e => release(r, e))))

/**
* Lifts a ZIO[R, E, A] into ZManaged[R, E, A] with a release action.
* The acquire action will be performed interruptibly, while release
* will be performed uninterruptibly.
*/
final def makeInterruptible[R, E, A](
acquire: ZIO[R, E, A]
)(release: A => ZIO[R, Nothing, Any]): ZManaged[R, E, A] =
ZManaged.fromEffect(acquire).onExitFirst(_.foreach(release))

/**
* Merges an `Iterable[IO]` to a single IO, working sequentially.
*/
Expand Down