-- | Unlifted "Control.Monad.Trans.Resource".
--
-- @since 1.1.10
module UnliftIO.Resource
  ( -- * UnliftIO variants
    runResourceT
  , liftResourceT
  , allocateU
    -- * Reexports
  , module Control.Monad.Trans.Resource
  ) where

import qualified Control.Monad.Trans.Resource as Res
import Control.Monad.Trans.Resource.Internal (ResourceT (..))
import Control.Monad.IO.Unlift
import Control.Monad.Trans.Resource (ResourceT, ReleaseKey, allocate, register, release, unprotect, MonadResource)

-- | Unlifted version of 'Res.runResourceT'.
--
-- @since 1.1.10
runResourceT :: MonadUnliftIO m => ResourceT m a -> m a
runResourceT :: forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT ResourceT m a
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
Res.runResourceT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a (n :: * -> *) b.
(m a -> n b) -> ResourceT m a -> ResourceT n b
Res.transResourceT forall a. m a -> IO a
run ResourceT m a
m

-- | Lifted version of 'Res.liftResourceT'.
--
-- @since 1.1.10
liftResourceT :: MonadIO m => ResourceT IO a -> ResourceT m a
liftResourceT :: forall (m :: * -> *) a.
MonadIO m =>
ResourceT IO a -> ResourceT m a
liftResourceT (ResourceT IORef ReleaseMap -> IO a
f) = forall (m :: * -> *) a. (IORef ReleaseMap -> m a) -> ResourceT m a
ResourceT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. IORef ReleaseMap -> IO a
f

-- | Unlifted 'allocate'.
--
-- @since 1.2.6
allocateU
  :: (MonadUnliftIO m, MonadResource m)
  => m a
  -> (a -> m ())
  -> m (ReleaseKey, a)
allocateU :: forall (m :: * -> *) a.
(MonadUnliftIO m, MonadResource m) =>
m a -> (a -> m ()) -> m (ReleaseKey, a)
allocateU m a
alloc a -> m ()
free = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run ->
  forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (forall a. m a -> IO a
run m a
alloc) (forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m ()
free)