module Z.Data.PrimRef.PrimSTRef
(
PrimSTRef(..)
, newPrimSTRef
, readPrimSTRef
, writePrimSTRef
, modifyPrimSTRef
) where
import Data.Primitive.Types
import Data.Primitive.ByteArray
import GHC.ST
import GHC.Exts
newtype PrimSTRef s a = PrimSTRef (MutableByteArray s)
newPrimSTRef :: Prim a => a -> ST s (PrimSTRef s a)
newPrimSTRef x = do
mba <- newByteArray (I# (sizeOf# x))
writeByteArray mba 0 x
return (PrimSTRef mba)
{-# INLINE newPrimSTRef #-}
readPrimSTRef :: Prim a => PrimSTRef s a -> ST s a
readPrimSTRef (PrimSTRef mba) = readByteArray mba 0
{-# INLINE readPrimSTRef #-}
writePrimSTRef :: Prim a => PrimSTRef s a -> a -> ST s ()
writePrimSTRef (PrimSTRef mba) x = writeByteArray mba 0 x
{-# INLINE writePrimSTRef #-}
modifyPrimSTRef :: Prim a => PrimSTRef s a -> (a -> a) -> ST s ()
modifyPrimSTRef ref f = readPrimSTRef ref >>= writePrimSTRef ref . f
{-# INLINE modifyPrimSTRef #-}