-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |