diff options
Diffstat (limited to 'hsm-core/Hsm/Core/Bracket.hs')
-rw-r--r-- | hsm-core/Hsm/Core/Bracket.hs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/hsm-core/Hsm/Core/Bracket.hs b/hsm-core/Hsm/Core/Bracket.hs new file mode 100644 index 0000000..92428de --- /dev/null +++ b/hsm-core/Hsm/Core/Bracket.hs @@ -0,0 +1,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 |