Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Effectful.Prim.IORef #236

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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