UnliftIO.Resource

resourcet-1.2.4.3: Deterministic allocation and freeing of scarce resources.

Safe Haskell | Safe-Inferred
Language | Haskell2010

UnliftIO.Resource

Contents

- UnliftIO variants
- Reexports

Description

Unlifted Control.Monad.Trans.Resource.

Since: 1.1.10

Synopsis

- runResourceT :: MonadUnliftIO m => ResourceT m a -> m a
- liftResourceT :: MonadIO m => ResourceT IO a -> ResourceT m a
- data ResourceT m a
- data ReleaseKey
- class MonadIO m => MonadResource m
- register :: MonadResource m => IO () -> m ReleaseKey
- release :: MonadIO m => ReleaseKey -> m ()
- unprotect :: MonadIO m => ReleaseKey -> m (Maybe (IO ()))
- allocate :: MonadResource m => IO a -> (a -> IO ()) -> m (ReleaseKey, a)

# UnliftIO variants

runResourceT :: MonadUnliftIO m => ResourceT m a -> m a #

Unlifted version of `runResourceT`.

Since: 1.1.10

liftResourceT :: MonadIO m => ResourceT IO a -> ResourceT m a #

Lifted version of `liftResourceT`.

Since: 1.1.10

# Reexports

data ResourceT m a #

The Resource transformer. This transformer keeps track of all registered actions, and calls them upon exit (via `runResourceT`). Actions may be registered via `register`, or resources may be allocated atomically via `allocate`. `allocate` corresponds closely to `bracket`.

Releasing may be performed before exit via the `release` function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling `release` will deregister the action, so that a release action will only ever be called once.

Since 0.3.0

#### Instances

Instances details

MonadTrans ResourceT # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods lift :: Monad m => m a -> ResourceT m a #
MonadRWS r w s m => MonadRWS r w s (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal
MonadError e m => MonadError e (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods throwError :: e -> ResourceT m a # catchError :: ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #
MonadReader r m => MonadReader r (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods ask :: ResourceT m r # local :: (r -> r) -> ResourceT m a -> ResourceT m a # reader :: (r -> a) -> ResourceT m a #
MonadState s m => MonadState s (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods get :: ResourceT m s # put :: s -> ResourceT m () # state :: (s -> (a, s)) -> ResourceT m a #
MonadWriter w m => MonadWriter w (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods writer :: (a, w) -> ResourceT m a # tell :: w -> ResourceT m () # listen :: ResourceT m a -> ResourceT m (a, w) # pass :: ResourceT m (a, w -> w) -> ResourceT m a #
MonadFail m => MonadFail (ResourceT m) # | Since: 1.2.2
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods fail :: String -> ResourceT m a #
MonadFix m => MonadFix (ResourceT m) # | Since: 1.1.8
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods mfix :: (a -> ResourceT m a) -> ResourceT m a #
MonadIO m => MonadIO (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftIO :: IO a -> ResourceT m a #
Alternative m => Alternative (ResourceT m) # | Since 1.1.5
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods empty :: ResourceT m a # (<|>) :: ResourceT m a -> ResourceT m a -> ResourceT m a # some :: ResourceT m a -> ResourceT m [a] # many :: ResourceT m a -> ResourceT m [a] #
Applicative m => Applicative (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods pure :: a -> ResourceT m a # (<*>) :: ResourceT m (a -> b) -> ResourceT m a -> ResourceT m b # liftA2 :: (a -> b -> c) -> ResourceT m a -> ResourceT m b -> ResourceT m c # (*>) :: ResourceT m a -> ResourceT m b -> ResourceT m b # (<*) :: ResourceT m a -> ResourceT m b -> ResourceT m a #
Functor m => Functor (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods fmap :: (a -> b) -> ResourceT m a -> ResourceT m b # (<$) :: a -> ResourceT m b -> ResourceT m a #
Monad m => Monad (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods (>>=) :: ResourceT m a -> (a -> ResourceT m b) -> ResourceT m b # (>>) :: ResourceT m a -> ResourceT m b -> ResourceT m b # return :: a -> ResourceT m a #
MonadPlus m => MonadPlus (ResourceT m) # | Since 1.1.5
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods mzero :: ResourceT m a # mplus :: ResourceT m a -> ResourceT m a -> ResourceT m a #
MonadCatch m => MonadCatch (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods catch :: Exception e => ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #
MonadMask m => MonadMask (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods mask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b # uninterruptibleMask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b # generalBracket :: ResourceT m a -> (a -> ExitCase b -> ResourceT m c) -> (a -> ResourceT m b) -> ResourceT m (b, c) #
MonadThrow m => MonadThrow (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods throwM :: Exception e => e -> ResourceT m a #
MonadCont m => MonadCont (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods callCC :: ((a -> ResourceT m b) -> ResourceT m a) -> ResourceT m a #
PrimMonad m => PrimMonad (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Associated Types type PrimState (ResourceT m) # Methods primitive :: (State# (PrimState (ResourceT m)) -> (# State# (PrimState (ResourceT m)), a #)) -> ResourceT m a #
MonadIO m => MonadResource (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ResourceT m a #
MonadUnliftIO m => MonadUnliftIO (ResourceT m) # | Since: 1.1.10
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods withRunInIO :: ((forall a. ResourceT m a -> IO a) -> IO b) -> ResourceT m b #
type PrimState (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal type PrimState (ResourceT m) = PrimState m

data ReleaseKey #

A lookup key for a specific release action. This value is returned by `register` and `allocate`, and is passed to `release`.

Since 0.3.0

class MonadIO m => MonadResource m #

A `Monad` which allows for safe resource allocation. In theory, any monad transformer stack which includes a `ResourceT` can be an instance of `MonadResource`.

Note: `runResourceT` has a requirement for a `MonadUnliftIO m` monad, which allows control operations to be lifted. A `MonadResource` does not have this requirement. This means that transformers such as `ContT` can be an instance of `MonadResource`. However, the `ContT` wrapper will need to be unwrapped before calling `runResourceT`.

Since 0.3.0

Minimal complete definition

liftResourceT

#### Instances

Instances details

MonadIO m => MonadResource (ResourceT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ResourceT m a #
MonadResource m => MonadResource (ListT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ListT m a #
MonadResource m => MonadResource (MaybeT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> MaybeT m a #
MonadResource m => MonadResource (ExceptT e m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ExceptT e m a #
MonadResource m => MonadResource (IdentityT m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> IdentityT m a #
MonadResource m => MonadResource (ReaderT r m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ReaderT r m a #
MonadResource m => MonadResource (StateT s m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> StateT s m a #
MonadResource m => MonadResource (StateT s m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> StateT s m a #
(Monoid w, MonadResource m) => MonadResource (WriterT w m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> WriterT w m a #
(Monoid w, MonadResource m) => MonadResource (WriterT w m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> WriterT w m a #
MonadResource m => MonadResource (ContT r m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> ContT r m a #
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> RWST r w s m a #
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) # |
Instance details Defined in Control.Monad.Trans.Resource.Internal Methods liftResourceT :: ResourceT IO a -> RWST r w s m a #

register :: MonadResource m => IO () -> m ReleaseKey #

Register some action that will be called precisely once, either when `runResourceT` is called, or when the `ReleaseKey` is passed to `release`.

Since 0.3.0

release :: MonadIO m => ReleaseKey -> m () #

Call a release action early, and deregister it from the list of cleanup actions to be performed.

Since 0.3.0

unprotect :: MonadIO m => ReleaseKey -> m (Maybe (IO ())) #

Unprotect resource from cleanup actions; this allows you to send resource into another resourcet process and reregister it there. It returns a release action that should be run in order to clean resource or Nothing in case if resource is already freed.

Since 0.4.5

allocate #

Arguments

:: MonadResource m |
=> IO a | allocate
-> (a -> IO ()) | free resource
-> m (ReleaseKey, a) |

Perform some allocation, and automatically register a cleanup action.

This is almost identical to calling the allocation and then `register`ing the release action, but this properly handles masking of asynchronous exceptions.

Since 0.3.0