resource-simple-0.2: Allocate resources which are guaranteed to be released.

Safe HaskellNone




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.


The ResourceT monad transformer

data ResourceT m a Source

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.


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.

data ReleaseKey Source

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