aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Bracket.hs
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-08-27 04:10:47 +0000
committerPaul Oliver <contact@pauloliver.dev>2025-08-27 04:17:18 +0000
commitec1a6fba4c944d95edb2397b47b6cebc59e2758d (patch)
tree48e4eccaa30f6607ceac01e1f97007b8d39f0f00 /hsm-core/Hsm/Core/Bracket.hs
parent3806bd1f5ce56afdbb4cc0c1ed54d53e25603be2 (diff)
Moves commonly used `bracket` combinators into separate module
Diffstat (limited to 'hsm-core/Hsm/Core/Bracket.hs')
-rw-r--r--hsm-core/Hsm/Core/Bracket.hs23
1 files changed, 23 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..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