-- 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