{-# LANGUAGE CPP #-}
module Data.Acquire
( Acquire
, with
, withAcquire
, mkAcquire
, mkAcquireType
, allocateAcquire
, ReleaseType (..)
) where
import Control.Monad.Trans.Resource.Internal
import Data.Acquire.Internal
import Control.Monad.IO.Unlift (MonadIO (..), MonadUnliftIO)
import qualified Control.Exception as E
allocateAcquire :: MonadResource m => Acquire a -> m (ReleaseKey, a)
allocateAcquire :: forall (m :: * -> *) a.
MonadResource m =>
Acquire a -> m (ReleaseKey, a)
allocateAcquire = ResourceT IO (ReleaseKey, a) -> m (ReleaseKey, a)
forall a. ResourceT IO a -> m a
forall (m :: * -> *) a. MonadResource m => ResourceT IO a -> m a
liftResourceT (ResourceT IO (ReleaseKey, a) -> m (ReleaseKey, a))
-> (Acquire a -> ResourceT IO (ReleaseKey, a))
-> Acquire a
-> m (ReleaseKey, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Acquire a -> ResourceT IO (ReleaseKey, a)
forall a. Acquire a -> ResourceT IO (ReleaseKey, a)
allocateAcquireRIO
allocateAcquireRIO :: Acquire a -> ResourceT IO (ReleaseKey, a)
allocateAcquireRIO :: forall a. Acquire a -> ResourceT IO (ReleaseKey, a)
allocateAcquireRIO (Acquire (forall b. IO b -> IO b) -> IO (Allocated a)
f) = (IORef ReleaseMap -> IO (ReleaseKey, a))
-> ResourceT IO (ReleaseKey, a)
forall (m :: * -> *) a. (IORef ReleaseMap -> m a) -> ResourceT m a
ResourceT ((IORef ReleaseMap -> IO (ReleaseKey, a))
-> ResourceT IO (ReleaseKey, a))
-> (IORef ReleaseMap -> IO (ReleaseKey, a))
-> ResourceT IO (ReleaseKey, a)
forall a b. (a -> b) -> a -> b
$ \IORef ReleaseMap
istate -> IO (ReleaseKey, a) -> IO (ReleaseKey, a)
forall b. IO b -> IO b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ReleaseKey, a) -> IO (ReleaseKey, a))
-> IO (ReleaseKey, a) -> IO (ReleaseKey, a)
forall a b. (a -> b) -> a -> b
$ ((forall b. IO b -> IO b) -> IO (ReleaseKey, a))
-> IO (ReleaseKey, a)
forall b. ((forall b. IO b -> IO b) -> IO b) -> IO b
E.mask (((forall b. IO b -> IO b) -> IO (ReleaseKey, a))
-> IO (ReleaseKey, a))
-> ((forall b. IO b -> IO b) -> IO (ReleaseKey, a))
-> IO (ReleaseKey, a)
forall a b. (a -> b) -> a -> b
$ \forall b. IO b -> IO b
restore -> do
Allocated a
a ReleaseType -> IO ()
free <- (forall b. IO b -> IO b) -> IO (Allocated a)
f IO b -> IO b
forall b. IO b -> IO b
restore
ReleaseKey
key <- IORef ReleaseMap -> (ReleaseType -> IO ()) -> IO ReleaseKey
registerType IORef ReleaseMap
istate ReleaseType -> IO ()
free
(ReleaseKey, a) -> IO (ReleaseKey, a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ReleaseKey
key, a
a)
withAcquire :: MonadUnliftIO m => Acquire a -> (a -> m b) -> m b
withAcquire :: forall (m :: * -> *) a b.
MonadUnliftIO m =>
Acquire a -> (a -> m b) -> m b
withAcquire = Acquire a -> (a -> m b) -> m b
forall (m :: * -> *) a b.
MonadUnliftIO m =>
Acquire a -> (a -> m b) -> m b
with
{-# INLINE withAcquire #-}