Copyright | Copyright 2022 Shea Levy. |
---|---|
License | Apache-2.0 |
Maintainer | shea@shealevy.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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
- class (Monad m, Monad (AllocationContext m)) => MonadAllocate m where
- type AllocationContext m :: Type -> Type
- type GeneralReleaseKey m
- type AllocationException m
- generalAllocate :: GeneralAllocate (AllocationContext m) (AllocationException m) () () a -> m (GeneralReleaseKey m, a)
- generalRegister :: (GeneralReleaseType (AllocationException m) () -> AllocationContext m ()) -> m (GeneralReleaseKey m)
- generalRelease :: GeneralReleaseKey m -> AllocationContext m ()
- type MonadAllocateExceptable m = (MonadAllocate m, Exceptable (AllocationException m))
- newtype AllocateViaResource m a = AllocateViaResource (m a)
- newtype AllocateViaLift t m a = AllocateViaLift (t m a)
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.
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
type AllocationException m = SomeException
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
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
.
AllocateViaResource (m a) |
Instances
newtype AllocateViaLift t m a Source #
A helper for DerivingVia a
MonadAllocate
instance for any MonadTrans
formed MonadAllocate
.
AllocateViaLift (t m a) |