{-# LANGUAGE MagicHash #-}
module Std.Data.PrimSTRef.Base
(
PrimSTRef(..)
, newPrimSTRef
, readPrimSTRef
, writePrimSTRef
, modifyPrimSTRef
) where
import Data.Primitive.Types
import Data.Primitive.ByteArray
import GHC.Prim
import GHC.ST
import GHC.Types
newtype PrimSTRef s a = PrimSTRef (MutableByteArray s)
newPrimSTRef :: Prim a => a -> ST s (PrimSTRef s a)
newPrimSTRef init = do
mba <- newByteArray (I# (sizeOf# init))
writeByteArray mba 0 init
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 #-}