Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This package is centered around a couple types:
Acquire
is a monadic context in which resources can be acquired. These can be executed usingacquire
, or for simpler caseswithAcquire
oracquireValue
.Resource
is a handle to a resource. The value for the resource can be read from this, and theResource
can also be used to manipulate the resource's lifetime.Liftime
is the type of first-class liftimes; resources are attached to these and can be moved between them.
Synopsis
- data Lifetime
- newLifetime :: Acquire Lifetime
- withLifetime :: (Lifetime -> IO a) -> IO a
- data Acquire a
- mkAcquire :: IO a -> (a -> IO ()) -> Acquire a
- withAcquire :: Acquire a -> (a -> IO b) -> IO b
- acquire :: Lifetime -> Acquire a -> IO (Resource a)
- acquireValue :: Lifetime -> Acquire a -> IO a
- currentLifetime :: Acquire Lifetime
- data Resource a
- getResource :: MonadSTM m => Resource a -> m (Maybe a)
- mustGetResource :: MonadSTM m => Resource a -> m a
- releaseEarly :: Resource a -> IO ()
- detach :: MonadSTM m => Resource a -> m (IO ())
- moveTo :: MonadSTM m => Resource a -> Lifetime -> m ()
- data ResourceExpired = ResourceExpired
Lifetimes
newLifetime :: Acquire Lifetime Source #
Acquire a new lifetime, as its own resource. This allows creating sub-groups of resources, which can be later moved as a unit.
withLifetime :: (Lifetime -> IO a) -> IO a Source #
Execute an IO action within the scope of a newly allocated lifetime, which ends when the IO action completes.
Acquiring resources
An Acquire
is a monadic action that acquires some number of resources,
and registers cleanup handlers to be executed when their lifetime expires.
mkAcquire :: IO a -> (a -> IO ()) -> Acquire a Source #
acquires a resource with mkAcquire
get cleanupget
, which will
be released by calling cleanup
when its lifetime ends.
withAcquire :: Acquire a -> (a -> IO b) -> IO b Source #
withAcquire
acuires a resource, uses it, and then releases it.
is equivalent to
withAcquire
(mkAcquire
get cleanup)
.bracket
get cleanup
acquire :: Lifetime -> Acquire a -> IO (Resource a) Source #
Acquire a resource, attaching it to the supplied lifetime.
currentLifetime :: Acquire Lifetime Source #
Get the lifetime for the resources being acquired.
Using resources
Represents a resource with type a
, which has a lifetime and an
associated cleanup handler.
getResource :: MonadSTM m => Resource a -> m (Maybe a) Source #
Get the value associated with a resource, returning Nothing
if the
resource's lifetime is expired.
mustGetResource :: MonadSTM m => Resource a -> m a Source #
Like getResource
, but throws a ResourceExpired
exception instead
of returning a Maybe
.
Releasing resources
releaseEarly :: Resource a -> IO () Source #
Release a resource early, before its lifetime would otherwise end.
detach :: MonadSTM m => Resource a -> m (IO ()) Source #
Detach the resource from its lifetime, returning the cleanup handler. NOTE: if the caller does not otherwise arrange to run the cleanup handler, it will *not* be executed.
Move semantics
moveTo :: MonadSTM m => Resource a -> Lifetime -> m () Source #
Move a resource to another lifetime. The resource will be detached from its existing lifetime, and so may live past it, but will be released when the new lifetime expires.
Errors
data ResourceExpired Source #
Error thrown when an attempt is made to use an expired resource or lifetime.
Instances
Exception ResourceExpired Source # | |
Defined in Lifetimes | |
Read ResourceExpired Source # | |
Defined in Lifetimes | |
Show ResourceExpired Source # | |
Defined in Lifetimes showsPrec :: Int -> ResourceExpired -> ShowS # show :: ResourceExpired -> String # showList :: [ResourceExpired] -> ShowS # | |
Eq ResourceExpired Source # | |
Defined in Lifetimes (==) :: ResourceExpired -> ResourceExpired -> Bool # (/=) :: ResourceExpired -> ResourceExpired -> Bool # | |
Ord ResourceExpired Source # | |
Defined in Lifetimes compare :: ResourceExpired -> ResourceExpired -> Ordering # (<) :: ResourceExpired -> ResourceExpired -> Bool # (<=) :: ResourceExpired -> ResourceExpired -> Bool # (>) :: ResourceExpired -> ResourceExpired -> Bool # (>=) :: ResourceExpired -> ResourceExpired -> Bool # max :: ResourceExpired -> ResourceExpired -> ResourceExpired # min :: ResourceExpired -> ResourceExpired -> ResourceExpired # |