module Data.RRBVector.Internal.IntRef
( IntRef
, newIntRef
, readIntRef
, writeIntRef
) where
import Control.Monad.ST
import Data.Primitive.PrimArray
newtype IntRef s = IntRef (MutablePrimArray s Int)
newIntRef :: Int -> ST s (IntRef s)
newIntRef :: forall s. Int -> ST s (IntRef s)
newIntRef Int
i = do
MutablePrimArray s Int
arr <- forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
1
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s Int
arr Int
0 Int
i
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall s. MutablePrimArray s Int -> IntRef s
IntRef MutablePrimArray s Int
arr)
{-# INLINE newIntRef #-}
readIntRef :: IntRef s -> ST s Int
readIntRef :: forall s. IntRef s -> ST s Int
readIntRef (IntRef MutablePrimArray s Int
arr) = forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray MutablePrimArray s Int
arr Int
0
{-# INLINE readIntRef #-}
writeIntRef :: IntRef s -> Int -> ST s ()
writeIntRef :: forall s. IntRef s -> Int -> ST s ()
writeIntRef (IntRef MutablePrimArray s Int
arr) = forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s Int
arr Int
0
{-# INLINE writeIntRef #-}