resourcet-1.3.0: Deterministic allocation and freeing of scarce resources.

Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Acquire.Internal

Synopsis

- newtype Acquire a = Acquire ((forall b. IO b -> IO b) -> IO (Allocated a))
- data Allocated a = Allocated !a !(ReleaseType -> IO ())
- with :: MonadUnliftIO m => Acquire a -> (a -> m b) -> m b
- mkAcquire :: IO a -> (a -> IO ()) -> Acquire a
- data ReleaseType where
  - ReleaseEarly
  - ReleaseNormal
  - ReleaseExceptionWith SomeException
  - pattern ReleaseException :: ReleaseType
- mkAcquireType :: IO a -> (a -> ReleaseType -> IO ()) -> Acquire a

# Documentation

newtype Acquire a Source #

A method for acquiring a scarce resource, providing the means of freeing it when no longer needed. This data type provides `Functor`/`Applicative`/`Monad` instances for composing different resources together. You can allocate these resources using either the `bracket` pattern (via `with`) or using `ResourceT` (via `allocateAcquire`).

This concept was originally introduced by Gabriel Gonzalez and described at: http://www.haskellforall.com/2013/06/the-resource-applicative.html. The implementation in this package is slightly different, due to taking a different approach to async exception safety.

Since: 1.1.0

Constructors

Acquire ((forall b. IO b -> IO b) -> IO (Allocated a)) |

#### Instances

Instances details

MonadIO Acquire Source # |
Instance details Defined in Data.Acquire.Internal Methods liftIO :: IO a -> Acquire a Source #
Applicative Acquire Source # |
Instance details Defined in Data.Acquire.Internal Methods pure :: a -> Acquire a Source # (<*>) :: Acquire (a -> b) -> Acquire a -> Acquire b Source # liftA2 :: (a -> b -> c) -> Acquire a -> Acquire b -> Acquire c Source # (*>) :: Acquire a -> Acquire b -> Acquire b Source # (<*) :: Acquire a -> Acquire b -> Acquire a Source #
Functor Acquire Source # |
Instance details Defined in Data.Acquire.Internal Methods fmap :: (a -> b) -> Acquire a -> Acquire b Source # (<$) :: a -> Acquire b -> Acquire a Source #
Monad Acquire Source # |
Instance details Defined in Data.Acquire.Internal Methods (>>=) :: Acquire a -> (a -> Acquire b) -> Acquire b Source # (>>) :: Acquire a -> Acquire b -> Acquire b Source # return :: a -> Acquire a Source #

data Allocated a Source #

Constructors

Allocated !a !(ReleaseType -> IO ()) |

with :: MonadUnliftIO m => Acquire a -> (a -> m b) -> m b Source #

Allocate the given resource and provide it to the provided function. The resource will be freed as soon as the inner block is exited, whether normally or via an exception. This function is similar in function to `bracket`.

Since: 1.1.0

mkAcquire Source #

Arguments

:: IO a | acquire the resource
-> (a -> IO ()) | free the resource
-> Acquire a |

Create an `Acquire` value using the given allocate and free functions.

To acquire and free the resource in an arbitrary monad with `MonadUnliftIO`, do the following:

    acquire <- withRunInIO $ \runInIO ->
      return $ mkAcquire (runInIO create) (runInIO . free)

Note that this is only safe if the Acquire is run and freed within the same monadic scope it was created in.

Since: 1.1.0

data ReleaseType Source #

The way in which a release is called.

Since: 1.1.2

Constructors

ReleaseEarly |
ReleaseNormal |
ReleaseExceptionWith SomeException |

Bundled Patterns

pattern ReleaseException :: ReleaseType | Deprecated: Use `ReleaseExceptionWith`, which has the exception in the constructor. This pattern synonym hides the exception and can obscure problems.

#### Instances

Instances details

Show ReleaseType Source # |
Instance details Defined in Data.Acquire.Internal Methods showsPrec :: Int -> ReleaseType -> ShowS Source # show :: ReleaseType -> String Source # showList :: [ReleaseType] -> ShowS Source #

mkAcquireType Source #

Arguments

:: IO a | acquire the resource
-> (a -> ReleaseType -> IO ()) | free the resource
-> Acquire a |

Same as `mkAcquire`, but the cleanup function will be informed of how cleanup was initiated. This allows you to distinguish, for example, between normal and exceptional exits.

To acquire and free the resource in an arbitrary monad with `MonadUnliftIO`, do the following:

    acquire <- withRunInIO $ \runInIO ->
      return $ mkAcquireType (runInIO create) (\a -> runInIO . free a)

Note that this is only safe if the Acquire is run and freed within the same monadic scope it was created in.

Since: 1.1.2