From ec1a6fba4c944d95edb2397b47b6cebc59e2758d Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Wed, 27 Aug 2025 04:10:47 +0000 Subject: Moves commonly used `bracket` combinators into separate module --- hsm-core/Hsm/Core/Bracket.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 hsm-core/Hsm/Core/Bracket.hs (limited to 'hsm-core/Hsm/Core/Bracket.hs') diff --git a/hsm-core/Hsm/Core/Bracket.hs b/hsm-core/Hsm/Core/Bracket.hs new file mode 100644 index 0000000..f666d86 --- /dev/null +++ b/hsm-core/Hsm/Core/Bracket.hs @@ -0,0 +1,23 @@ +-- 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 -- cgit v1.2.1