Skip to content

Commit

Permalink
Add evalResourceT
Browse files Browse the repository at this point in the history
  • Loading branch information
parsonsmatt committed Dec 13, 2024
1 parent ca1850d commit 0e3e810
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 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,27 @@ 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 = do
(a, cleanup) <- evalResourceT action
liftIO 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 TODO
evalResourceT :: MonadUnliftIO m => ResourceT m a -> m (a, IO ())
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, stateCleanupChecked Nothing istate)

-- | Backwards compatible alias for 'runResourceT'.
--
Expand Down

0 comments on commit 0e3e810

Please sign in to comment.