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

Add evalResourceT #517

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions resourcet/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for resourcet

## 1.3.1

* Add `evalResourceT` which allows you to defer running the finalizer cleanup. [#517](https://github.com/snoyberg/conduit/pull/517)

## 1.3.0

* Include the exception in ReleaseTypes indicating exceptional exit.
Expand Down
21 changes: 18 additions & 3 deletions resourcet/Control/Monad/Trans/Resource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Control.Monad.Trans.Resource
, ReleaseKey
-- * Unwrap
, runResourceT
, evalResourceT
-- ** Check cleanup exceptions
, runResourceTChecked
, ResourceCleanupException (..)
Expand Down Expand Up @@ -187,14 +188,28 @@ release' istate key act = E.mask_ $ do
--
-- @since 0.3.0
runResourceT :: MonadUnliftIO m => ResourceT m a -> m a
runResourceT (ResourceT r) = withRunInIO $ \run -> do
runResourceT action = withRunInIO $ \run -> do
E.mask_ $ run $ do
(a, cleanup) <- evalResourceT action
cleanup
pure a

-- | Like 'runResourceT', but this one does *not* run the cleanup action
-- when the block exits. Instead, the cleanup action is provided for the
-- user to call themselves. This is very likely to drop the cleanup action
-- and you should only use this if you are doing something moderately
-- cursed.
--
-- @since 1.3.1
evalResourceT :: MonadUnliftIO m => ResourceT m a -> m (a, m ())
evalResourceT (ResourceT r) = withRunInIO $ \run -> do
istate <- createInternalState
E.mask $ \restore -> do
res <- restore (run (r istate)) `E.catch` \e -> do
stateCleanupChecked (Just e) istate
E.throwIO e
stateCleanupChecked Nothing istate
return res

return (res, liftIO $ stateCleanupChecked Nothing istate)

-- | Backwards compatible alias for 'runResourceT'.
--
Expand Down
2 changes: 1 addition & 1 deletion resourcet/resourcet.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: resourcet
Version: 1.3.0
Version: 1.3.1
Synopsis: Deterministic allocation and freeing of scarce resources.
description: Hackage documentation generation is not reliable. For up to date documentation, please see: <http://www.stackage.org/package/resourcet>.
License: BSD3
Expand Down
Loading