Skip to content

Commit

Permalink
Add Effectful.Prim.IORef
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Sep 4, 2024
1 parent 2a8f0b3 commit ef6e872
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions effectful/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
`Effectful.Labeled.Writer`.
* Add `throwErrorWith` and `throwError_` to `Effectful.Error.Static` and
`Effectful.Error.Dynamic`.
* Add `Effectful.Prim.IORef`.
* **Breaking changes**:
- `localSeqLend`, `localLend`, `localSeqBorrow` and `localBorrow` now take a
list of effects instead of a single one.
Expand Down
1 change: 1 addition & 0 deletions effectful/effectful.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ library
Effectful.FileSystem.IO.ByteString.Builder
Effectful.FileSystem.IO.ByteString.Lazy
Effectful.FileSystem.IO.File
Effectful.Prim.IORef
Effectful.Process
Effectful.Temporary
Effectful.Timeout
Expand Down
72 changes: 72 additions & 0 deletions effectful/src/Effectful/Prim/IORef.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- | Lifted "Data.IORef".
--
-- /Note:/ it requires 'Prim' because @MutVar@ from the @primitive@ library is a
-- generalization of 'IORef'.
--
-- @since 2.4.0.0
module Effectful.Prim.IORef
( -- * Effect
Prim

-- ** Handlers
, runPrim

-- * IORef
, IORef
, newIORef
, readIORef
, writeIORef
, modifyIORef
, modifyIORef'
, atomicModifyIORef
, atomicModifyIORef'
, atomicWriteIORef
, mkWeakIORef
) where

import Data.IORef (IORef)
import Data.IORef qualified as Ref
import System.Mem.Weak (Weak)

import Effectful
import Effectful.Dispatch.Static
import Effectful.Dispatch.Static.Primitive
import Effectful.Prim

-- | Lifted 'Ref.newIORef'.
newIORef :: Prim :> es => a -> Eff es (IORef a)
newIORef = unsafeEff_ . Ref.newIORef

-- | Lifted 'Ref.readIORef'.
readIORef :: Prim :> es => IORef a -> Eff es a
readIORef = unsafeEff_ . Ref.readIORef

-- | Lifted 'Ref.writeIORef'.
writeIORef :: Prim :> es => IORef a -> a -> Eff es ()
writeIORef var = unsafeEff_ . Ref.writeIORef var

-- | Lifted 'Ref.modifyIORef'.
modifyIORef :: Prim :> es => IORef a -> (a -> a) -> Eff es ()
modifyIORef var = unsafeEff_ . Ref.modifyIORef var

-- | Lifted 'Ref.modifyIORef''.
modifyIORef' :: Prim :> es => IORef a -> (a -> a) -> Eff es ()
modifyIORef' var = unsafeEff_ . Ref.modifyIORef' var

-- | Lifted 'Ref.atomicModifyIORef'.
atomicModifyIORef :: Prim :> es => IORef a -> (a -> (a, b)) -> Eff es b
atomicModifyIORef var = unsafeEff_ . Ref.atomicModifyIORef var

-- | Lifted 'Ref.atomicModifyIORef''.
atomicModifyIORef' :: Prim :> es => IORef a -> (a -> (a, b)) -> Eff es b
atomicModifyIORef' var = unsafeEff_ . Ref.atomicModifyIORef' var

-- | Lifted 'Ref.atomicWriteIORef''.
atomicWriteIORef :: Prim :> es => IORef a -> a -> Eff es ()
atomicWriteIORef var = unsafeEff_ . Ref.atomicWriteIORef var

-- | Lifted 'Ref.mkWeakIORef'.
mkWeakIORef :: Prim :> es => IORef a -> Eff es () -> Eff es (Weak (IORef a))
mkWeakIORef var f = unsafeEff $ \es -> do
-- The finalizer can run at any point and in any thread.
Ref.mkWeakIORef var . unEff f =<< cloneEnv es

0 comments on commit ef6e872

Please sign in to comment.