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

Using resourcet with streamly #2701

Open
harendra-kumar opened this issue Jan 28, 2024 · 0 comments
Open

Using resourcet with streamly #2701

harendra-kumar opened this issue Jan 28, 2024 · 0 comments

Comments

@harendra-kumar
Copy link
Member

resourcet package can be used with streamly-core. With streamly package it would require unliftio instead of monad-control which is only implemented under an experimental flag as of now. Pasting some old comments from another issue below.

runResourceT can be written using MonadBaseControl:

{-# LANGUAGE FlexibleContexts #-}

module RunResourceT where

import Control.Exception (catch, mask, throwIO)
import Control.Monad.IO.Unlift (MonadIO(..), MonadUnliftIO, withRunInIO)
import Control.Monad.Trans.Control (MonadBaseControl, control)
import Control.Monad.Trans.Resource (createInternalState)
import Control.Monad.Trans.Resource.Internal
       (ResourceT(..), stateCleanupChecked)

runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a
runResourceT (ResourceT r) = control $ \run -> do
    istate <- createInternalState
    mask $ \restore -> do
        res <- restore (run (r istate)) `catch` \e -> do
            stateCleanupChecked (Just e) istate
            throwIO e
        stateCleanupChecked Nothing istate
        return res

You can use this function instead of the runResourceT in the resourcet package and use the regular resourcet package. Rest of the functionality you can take from resourcet package. I have not tested it though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant