aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Bracket.hs
blob: 92428deca26811d23e96e391adb4123914a91218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- Resource management combinators for safe acquisition/release patterns.
-- Provides specialized bracket variants for common scenarios.
module Hsm.Core.Bracket
  ( bracketConst
  , bracketCont
  , bracketLiftIO_
  )
where

import Control.Monad.Trans.Cont (Cont, cont)
import Effectful (Eff, IOE, liftIO, (:>))
import Effectful.Exception (bracket, bracket_)

-- Ignores allocated resource in the action
bracketConst :: Eff es a -> (a -> Eff es b) -> Eff es c -> Eff es c
bracketConst alloc dealloc = bracket alloc dealloc . const

-- Continuation-passing style integration
bracketCont :: Eff es a -> (a -> Eff es b) -> Cont (Eff es c) a
bracketCont alloc dealloc = cont $ bracket alloc dealloc

-- Lifts IO operations into Effectful brackets
bracketLiftIO_ :: IOE :> es => IO a -> IO b -> Eff es c -> Eff es c
bracketLiftIO_ alloc dealloc = bracket_ (liftIO alloc) $ liftIO dealloc