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 +++++++++++++++++++++++ hsm-core/hsm-core.cabal | 8 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 hsm-core/Hsm/Core/Bracket.hs (limited to 'hsm-core') 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 diff --git a/hsm-core/hsm-core.cabal b/hsm-core/hsm-core.cabal index 856a359..67a63e2 100644 --- a/hsm-core/hsm-core.cabal +++ b/hsm-core/hsm-core.cabal @@ -3,12 +3,16 @@ author: Paul Oliver name: hsm-core version: 0.1.0.0 - library build-depends: , base + , effectful-core , template-haskell + , transformers default-language: GHC2024 - exposed-modules: Hsm.Core.Serial + exposed-modules: + Hsm.Core.Bracket + Hsm.Core.Serial + ghc-options: -O2 -Wall -Werror -Wno-star-is-type -Wunused-packages -- cgit v1.2.1