module RIO.Prelude.URef
( URef
, IOURef
, newURef
, readURef
, writeURef
, modifyURef
) where
import RIO.Prelude.Reexports
import qualified Data.Vector.Unboxed.Mutable as MUVector
newtype URef s a = URef (MUVector.MVector s a)
type IOURef = URef (PrimState IO)
newURef :: (PrimMonad m, Unbox a) => a -> m (URef (PrimState m) a)
newURef :: a -> m (URef (PrimState m) a)
newURef a
a = (MVector (PrimState m) a -> URef (PrimState m) a)
-> m (MVector (PrimState m) a) -> m (URef (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MVector (PrimState m) a -> URef (PrimState m) a
forall s a. MVector s a -> URef s a
URef (Int -> a -> m (MVector (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> a -> m (MVector (PrimState m) a)
MUVector.replicate Int
1 a
a)
readURef :: (PrimMonad m, Unbox a) => URef (PrimState m) a -> m a
readURef :: URef (PrimState m) a -> m a
readURef (URef MVector (PrimState m) a
v) = MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> m a
MUVector.unsafeRead MVector (PrimState m) a
v Int
0
writeURef :: (PrimMonad m, Unbox a) => URef (PrimState m) a -> a -> m ()
writeURef :: URef (PrimState m) a -> a -> m ()
writeURef (URef MVector (PrimState m) a
v) = MVector (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> Int -> a -> m ()
MUVector.unsafeWrite MVector (PrimState m) a
v Int
0
modifyURef :: (PrimMonad m, Unbox a) => URef (PrimState m) a -> (a -> a) -> m ()
modifyURef :: URef (PrimState m) a -> (a -> a) -> m ()
modifyURef URef (PrimState m) a
u a -> a
f = URef (PrimState m) a -> m a
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
URef (PrimState m) a -> m a
readURef URef (PrimState m) a
u m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= URef (PrimState m) a -> a -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
URef (PrimState m) a -> a -> m ()
writeURef URef (PrimState m) a
u (a -> m ()) -> (a -> a) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f