aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-08-20 19:10:16 +0000
committerPaul Oliver <contact@pauloliver.dev>2025-08-23 22:59:12 +0000
commit82b02509150b615360118ca381ad8c9fd39d2f29 (patch)
tree97118f30a334a7b1fc9b6b7f6a8bbcbc87ae5aeb
parentdea39a25938662e6af932bc9a957073d8364dde1 (diff)
Falls back to `bracket` unless `releaseEff` is needed
-rw-r--r--hsm-gpio/Hsm/GPIO.hs15
-rw-r--r--hsm-pwm/Hsm/PWM.hs12
-rw-r--r--hsm-pwm/hsm-pwm.cabal1
-rw-r--r--hsm-repl/Hsm/Repl.hs8
-rw-r--r--hsm-repl/Test/Repl.hs2
-rw-r--r--hsm-repl/hsm-repl.cabal2
6 files changed, 15 insertions, 25 deletions
diff --git a/hsm-gpio/Hsm/GPIO.hs b/hsm-gpio/Hsm/GPIO.hs
index 7cc2c36..0e4e2e5 100644
--- a/hsm-gpio/Hsm/GPIO.hs
+++ b/hsm-gpio/Hsm/GPIO.hs
@@ -21,6 +21,7 @@ import Effectful.Dispatch.Static
, getStaticRep
, unsafeEff_
)
+import Effectful.Exception (bracket)
import Effectful.Resource (Resource, allocateEff, releaseEff)
import Foreign.C.String (withCString)
import Foreign.C.Types (CSize (CSize), CUInt)
@@ -92,12 +93,12 @@ runGPIO consumer action = do
(lineSettingsKey, lineSettings) <- lineSettingsBracket
(lineConfigKey, lineConfig) <- lineConfigBracket lineSettings
(requestConfigKey, requestConfig) <- requestConfigBracket
- (_, lineRequest) <- lineRequestBracket chip requestConfig lineConfig
- releaseEff requestConfigKey
- releaseEff lineConfigKey
- releaseEff lineSettingsKey
- releaseEff chipKey
- evalStaticRep (GPIO lineRequest) action
+ lineRequestBracket chip requestConfig lineConfig $ \lineRequest -> do
+ releaseEff requestConfigKey
+ releaseEff lineConfigKey
+ releaseEff lineSettingsKey
+ releaseEff chipKey
+ evalStaticRep (GPIO lineRequest) action
where
chipBracket = allocateEff chipAlloc chipDealloc
where
@@ -148,7 +149,7 @@ runGPIO consumer action = do
requestConfigDealloc requestConfig = do
logMsg Info "Freeing request config"
liftIO $ requestConfigFree requestConfig
- lineRequestBracket chip requestConfig lineConfig = allocateEff lineRequestAlloc lineRequestDealloc
+ lineRequestBracket chip requestConfig lineConfig = bracket lineRequestAlloc lineRequestDealloc
where
lineRequestAlloc = do
logMsg Info "Allocating line request"
diff --git a/hsm-pwm/Hsm/PWM.hs b/hsm-pwm/Hsm/PWM.hs
index 400e704..9a4fe5c 100644
--- a/hsm-pwm/Hsm/PWM.hs
+++ b/hsm-pwm/Hsm/PWM.hs
@@ -20,7 +20,7 @@ module Hsm.PWM
where
import Control.Concurrent (threadDelay)
-import Control.Monad (forM_, void)
+import Control.Monad (forM_)
import Control.Monad.Loops (untilM_)
import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, liftIO, (:>))
import Effectful.Dispatch.Static
@@ -29,7 +29,7 @@ import Effectful.Dispatch.Static
, evalStaticRep
, unsafeEff_
)
-import Effectful.Resource (Resource, allocateEff_)
+import Effectful.Exception (bracket_)
import Hsm.Core.Serial (makeSerial)
import Hsm.Log (Log, Severity (Info, Trace), logMsg)
import System.FilePath ((</>))
@@ -100,12 +100,8 @@ setCycleDuration channel cycleDuration = do
setDutyCycle channel $ cycleDuration `div` 2
setEnable channel True
-runPWM
- :: (IOE :> es, Log "pwm" :> es, Resource :> es) => Eff (PWM : es) a -> Eff es a
-runPWM action =
- evalStaticRep (PWM ()) $ do
- void $ allocateEff_ pwmAlloc pwmDealloc
- action
+runPWM :: (IOE :> es, Log "pwm" :> es) => Eff (PWM : es) a -> Eff es a
+runPWM = evalStaticRep (PWM ()) . bracket_ pwmAlloc pwmDealloc
where
exportPath = chipPath </> "export"
unexportPath = chipPath </> "unexport"
diff --git a/hsm-pwm/hsm-pwm.cabal b/hsm-pwm/hsm-pwm.cabal
index 2f3134a..808c7cb 100644
--- a/hsm-pwm/hsm-pwm.cabal
+++ b/hsm-pwm/hsm-pwm.cabal
@@ -12,7 +12,6 @@ library
, hsm-core
, hsm-log
, monad-loops
- , resourcet-effectful
, unix
default-language: GHC2024
diff --git a/hsm-repl/Hsm/Repl.hs b/hsm-repl/Hsm/Repl.hs
index bcde6ad..1da7493 100644
--- a/hsm-repl/Hsm/Repl.hs
+++ b/hsm-repl/Hsm/Repl.hs
@@ -17,7 +17,7 @@ import Effectful.Dispatch.Static
, getStaticRep
, unsafeEff_
)
-import Effectful.Resource (Resource, allocateEff)
+import Effectful.Exception (bracket)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Generic.Data.Function.Common.Generic.Meta (KnownSymbols, symbolVals)
import Hsm.Log (Log, Severity (Attention, Info, Trace), logMsg)
@@ -89,12 +89,10 @@ repl = query >>= maybe (return Nothing) parse
runRepl
:: forall p ms t es a
- . (IOE :> es, Log "repl" :> es, Resource :> es)
+ . (IOE :> es, Log "repl" :> es)
=> Eff (Repl p ms t : es) a
-> Eff es a
-runRepl action = do
- inputState <- snd <$> allocateEff inputStateAlloc inputStateDealloc
- evalStaticRep (Repl inputState) action
+runRepl action = bracket inputStateAlloc inputStateDealloc $ \inputState -> evalStaticRep (Repl inputState) action
where
inputStateAlloc = do
logMsg Info "Initializing input"
diff --git a/hsm-repl/Test/Repl.hs b/hsm-repl/Test/Repl.hs
index 9052ef1..3b0e534 100644
--- a/hsm-repl/Test/Repl.hs
+++ b/hsm-repl/Test/Repl.hs
@@ -2,7 +2,6 @@ import Control.Monad (void)
import Control.Monad.Loops (whileJust_)
import Data.Function ((&))
import Effectful (runEff)
-import Effectful.Resource (runResource)
import Hsm.Log (Severity (Trace), runLog)
import Hsm.Repl (repl, runRepl)
@@ -11,5 +10,4 @@ main =
void (whileJust_ repl return)
& runRepl @"exec-repl λ " @'["Prelude"] @[Bool]
& runLog @"repl" Trace
- & runResource
& runEff
diff --git a/hsm-repl/hsm-repl.cabal b/hsm-repl/hsm-repl.cabal
index b37e923..eb755a6 100644
--- a/hsm-repl/hsm-repl.cabal
+++ b/hsm-repl/hsm-repl.cabal
@@ -12,7 +12,6 @@ library
, haskeline
, hint
, hsm-log
- , resourcet-effectful
default-language: GHC2024
exposed-modules: Hsm.Repl
@@ -30,7 +29,6 @@ executable test-repl
, hint
, hsm-log
, monad-loops
- , resourcet-effectful
default-language: GHC2024
ghc-options: