general-allocate-0.2.1.1: Exception-safe resource management in more monads
CopyrightCopyright 2022 Shea Levy.
LicenseApache-2.0
Maintainershea@shealevy.com
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Monad.Allocate

Description

This module defines interfaces for safe resource usage on top of GeneralAllocate, where resources have an arbitrary lifetime and can be released manually or at the end of the computation.

For contexts where nested scope-based allocation and release is sufficient, see Control.Monad.With.

This design is heavily based on MonadResource from resourcet. resourcet is Copyright (c)2011, Michael Snoyman, and licensed under the BSD 3-clause license available at LICENSE.resourcet.

Synopsis

Documentation

class (Monad m, Monad (AllocationContext m)) => MonadAllocate m where Source #

A monad allowing for exception-safe resource usage with arbitrary lifetimes.

The guarantees of MonadAllocate are weaker than MonadResource: in some monads, it's possible for resources not to get cleaned up if the entire monadic computation is going to be aborted (e.g. an async exception sent to a thread executing a monad with no exception catching). Of course, MonadResource itself can't guarantee cleanup in the presence of SIGKILL... In any case, this allows for MonadAllocate to be implemented lawfully in more monads (see NoContinuationResourceT). This allows for writing monad-generic exception-safe code which can be properly instantiated in IO or mocked out in ST without changing the code.

Minimal complete definition

generalAllocate, generalRelease

Associated Types

type AllocationContext m :: Type -> Type Source #

The monad in which resources are allocated and released.

type GeneralReleaseKey m Source #

A handle to release some resource early manually

type AllocationException m Source #

The exception type of the monadic context

Methods

generalAllocate :: GeneralAllocate (AllocationContext m) (AllocationException m) () () a -> m (GeneralReleaseKey m, a) Source #

Allocate some resource, which will be cleaned up on call to generalRelease or the end of the current resource scope, whichever comes first.

generalRegister :: (GeneralReleaseType (AllocationException m) () -> AllocationContext m ()) -> m (GeneralReleaseKey m) Source #

Register an action which will be guaranteed to run on call to generalRelease or the end of the current resource scope, whichever comes first.

generalRelease :: GeneralReleaseKey m -> AllocationContext m () Source #

Run a release action from a prior call to generalAllocate/generalRegister.

Instances

Instances details
MonadResource m => MonadAllocate (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

(PrimMonad m, MonadWith m) => MonadAllocate (NoContinuationResourceT m) Source # 
Instance details

Defined in Control.Monad.NoContinuation.Resource.Internal

MonadIO m => MonadAllocate (ResourceT m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (MaybeT m) Source # 
Instance details

Defined in Control.Monad.Allocate

(MonadAllocate m, MonadTrans t, Monad (t m)) => MonadAllocate (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (ReaderT r m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (StateT s m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (StateT s m) Source # 
Instance details

Defined in Control.Monad.Allocate

(Monoid w, MonadAllocate m) => MonadAllocate (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Allocate

(Monoid w, MonadAllocate m) => MonadAllocate (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadAllocate m => MonadAllocate (ContT r m) Source # 
Instance details

Defined in Control.Monad.Allocate

Associated Types

type AllocationContext (ContT r m) :: Type -> Type Source #

type GeneralReleaseKey (ContT r m) Source #

type AllocationException (ContT r m) Source #

(Monoid w, MonadAllocate m) => MonadAllocate (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Allocate

Associated Types

type AllocationContext (RWST r w s m) :: Type -> Type Source #

type GeneralReleaseKey (RWST r w s m) Source #

type AllocationException (RWST r w s m) Source #

(Monoid w, MonadAllocate m) => MonadAllocate (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Allocate

Associated Types

type AllocationContext (RWST r w s m) :: Type -> Type Source #

type GeneralReleaseKey (RWST r w s m) Source #

type AllocationException (RWST r w s m) Source #

type MonadAllocateExceptable m = (MonadAllocate m, Exceptable (AllocationException m)) Source #

A MonadAllocate whose exception type can be projected into the Haskell exception hierarchy

newtype AllocateViaResource m a Source #

A helper for DerivingVia a MonadAllocate instance for any MonadResource.

Constructors

AllocateViaResource (m a) 

Instances

Instances details
Applicative m => Applicative (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

Functor m => Functor (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

Methods

fmap :: (a -> b) -> AllocateViaResource m a -> AllocateViaResource m b #

(<$) :: a -> AllocateViaResource m b -> AllocateViaResource m a #

Monad m => Monad (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

MonadResource m => MonadAllocate (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

type AllocationContext (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

type AllocationException (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

type GeneralReleaseKey (AllocateViaResource m) Source # 
Instance details

Defined in Control.Monad.Allocate

newtype AllocateViaLift t m a Source #

A helper for DerivingVia a MonadAllocate instance for any MonadTransformed MonadAllocate.

Constructors

AllocateViaLift (t m a) 

Instances

Instances details
MonadTrans t => MonadTrans (AllocateViaLift t) Source # 
Instance details

Defined in Control.Monad.Allocate

Methods

lift :: Monad m => m a -> AllocateViaLift t m a #

Applicative (t m) => Applicative (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

Methods

pure :: a -> AllocateViaLift t m a #

(<*>) :: AllocateViaLift t m (a -> b) -> AllocateViaLift t m a -> AllocateViaLift t m b #

liftA2 :: (a -> b -> c) -> AllocateViaLift t m a -> AllocateViaLift t m b -> AllocateViaLift t m c #

(*>) :: AllocateViaLift t m a -> AllocateViaLift t m b -> AllocateViaLift t m b #

(<*) :: AllocateViaLift t m a -> AllocateViaLift t m b -> AllocateViaLift t m a #

Functor (t m) => Functor (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

Methods

fmap :: (a -> b) -> AllocateViaLift t m a -> AllocateViaLift t m b #

(<$) :: a -> AllocateViaLift t m b -> AllocateViaLift t m a #

Monad (t m) => Monad (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

Methods

(>>=) :: AllocateViaLift t m a -> (a -> AllocateViaLift t m b) -> AllocateViaLift t m b #

(>>) :: AllocateViaLift t m a -> AllocateViaLift t m b -> AllocateViaLift t m b #

return :: a -> AllocateViaLift t m a #

(MonadAllocate m, MonadTrans t, Monad (t m)) => MonadAllocate (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

type AllocationContext (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

type AllocationException (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate

type GeneralReleaseKey (AllocateViaLift t m) Source # 
Instance details

Defined in Control.Monad.Allocate