Safe Haskell | None |
---|
Allocate resources which are guaranteed to be released.
One point to note: all register cleanup actions live in IO, not the main monad. This allows both more efficient code, and for monads to be transformed.
- data ResourceT m a
- runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a
- mapResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n b
- class MonadIO m => MonadResource m where
- with :: IO a -> (a -> IO ()) -> m (ReleaseKey, a)
- register :: IO () -> m ReleaseKey
- release :: ReleaseKey -> m ()
- data ReleaseKey
The ResourceT
monad transformer
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
with
. The with
function corresponds closely to bracket
. These
functions are provided by 'ResourceT'\'s MonadResource
instance.
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.
Pass-through instances for the mtl
type classes are provided
automatically by the mtl-evil-instances
package.
MonadTrans ResourceT | |
MonadTransControl ResourceT | |
MonadBaseControl b m => MonadBaseControl b (ResourceT m) | |
Monad m => Monad (ResourceT m) | |
Monad m => Functor (ResourceT m) | |
MonadPlus m => MonadPlus (ResourceT m) | |
Monad m => Applicative (ResourceT m) | |
MonadPlus m => Alternative (ResourceT m) | |
(MonadFork m, MonadBaseControl IO m) => MonadFork (ResourceT m) | |
MonadBaseControl IO m => MonadResource (ResourceT m) |
Running
runResourceT :: MonadBaseControl IO m => ResourceT m a -> m aSource
Unwrap a ResourceT
transformer, and call all registered release
actions.
Note that there is some reference counting involved due to the
implementation of fork
used in the MonadFork
instance. If multiple
threads are sharing the same collection of resources, only the last call
to runResourceT
will deallocate the resources.
Monad transformation
mapResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n bSource
Transform the monad a ResourceT
lives in. This is most often used to
strip or add new transformers to a stack, e.g. to run a ReaderT
.
The MonadResource
type class
class MonadIO m => MonadResource m whereSource
The MonadResource
type class. This provides the with
, register
and
release
functions, which are the main functionality of this package. The
main instance of this class is ResourceT
.
The others instances are overlapping instances (in the spirit of
mtl-evil-instances
), which provide automatic pass-through instances for
MonadResource
for every monad transformer. This means that you don't have
to provide a pass-through instance of MonadResource
for every monad
transformer you write.
with :: IO a -> (a -> IO ()) -> m (ReleaseKey, a)Source
Perform some allocation, and automatically register a cleanup action.
register :: IO () -> m ReleaseKeySource
Register some action that will be run precisely once, either when
runResourceT
is called, or when the ReleaseKey
is passed to
release
.
release :: ReleaseKey -> m ()Source
Call a release action early, and deregister it from the list of cleanup actions to be performed.
(MonadBase b m, MonadResource b) => MonadResource m | |
(MonadTrans t, Monad (t m), MonadResource m) => MonadResource (t m) | |
MonadBaseControl IO m => MonadResource (ResourceT m) |
data ReleaseKey Source