diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-08-27 04:10:47 +0000 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-08-27 04:17:18 +0000 |
commit | ec1a6fba4c944d95edb2397b47b6cebc59e2758d (patch) | |
tree | 48e4eccaa30f6607ceac01e1f97007b8d39f0f00 /hsm-core | |
parent | 3806bd1f5ce56afdbb4cc0c1ed54d53e25603be2 (diff) |
Moves commonly used `bracket` combinators into separate module
Diffstat (limited to 'hsm-core')
-rw-r--r-- | hsm-core/Hsm/Core/Bracket.hs | 23 | ||||
-rw-r--r-- | hsm-core/hsm-core.cabal | 8 |
2 files changed, 29 insertions, 2 deletions
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 <contact@pauloliver.dev> 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 |