-
Couldn't load subscription status.
- Fork 1.4k
add ZSink.managed #3788
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
add ZSink.managed #3788
Conversation
| } yield push | ||
| } | ||
|
|
||
| def managed[R, E, I, A, L, Z](resource: ZManaged[R, E, A])(fn: A => ZSink[R, E, I, L, Z]): ZSink[R, E, I, L, Z] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this signature is much simpler 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I started with the original signature from the ticket, then tried to use it in fromFile and it immediately folded. Nested resources are too useful.
| } | ||
| ) | ||
| } | ||
| writer &> ZSink.count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice @simpadjo
| */ | ||
| def foreachChunk[R, E, I](f: Chunk[I] => ZIO[R, E, Any]): ZSink[R, E, I, I, Unit] = | ||
| ZSink.fromPush[R, E, I, I, Unit] { | ||
| def foreachChunk[R, E, I](f: Chunk[I] => ZIO[R, E, Any]): ZSink[R, E, I, Nothing, Unit] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| * `resource` will be finalized after the processing. | ||
| */ | ||
| def managed[R, E, I, A, L, Z](resource: ZManaged[R, E, A])(fn: A => ZSink[R, E, I, L, Z]): ZSink[R, E, I, L, Z] = | ||
| ZSink(resource.fold[ZSink[R, E, I, L, Z]](err => ZSink.fail[E, I](err).dropLeftover, m => fn(m)).flatMap(_.push)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are the leftovers dropped in the error case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ZSink.fail returns leftovers of type I and we require L.
I don't know how to properly treat allocation errors w/o losing too much of ergonomics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could require ZManaged[R, Nothing, A] but I expect most of the resources to be IO-related.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s require ZSink[R, E, I, I, Z] here. Dropping the leftovers is a bad choice.
| ) | ||
| .position(position) | ||
| ) | ||
| .orDie |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I seem to have missed this orDie here. Why is it required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't (I removed it).
Actually, we could reify Throwable to IOException, but I'm not sure how it would work in case of interruption.
|
@simpadjo Can you fix the compilation errors on Dotty? |
closes #3651
ZSink.managedZSink.fromOutputStreamManagedZSink.fromFileZSink.foreachChunksignature@regis-leray sorry to steal this ticket from you, I need this at work :)
@lvitaly - pinging you as the author of
ZSink#fromFile