module Data.Mutable.VRef
(
VRef
, IOVRef
, asVRef
, MutableRef (..)
) where
import Control.Monad (liftM)
import Data.Mutable.Class
import qualified Data.Vector.Generic.Mutable as V
import qualified Data.Vector.Mutable as VB
newtype VRef s a = VRef (VB.MVector s a)
asVRef :: VRef s a -> VRef s a
asVRef x = x
type IOVRef = VRef (PrimState IO)
instance MutableContainer (VRef s a) where
type MCState (VRef s a) = s
instance MutableRef (VRef s a) where
type RefElement (VRef s a) = a
newRef = liftM VRef . V.replicate 1
readRef (VRef v) = V.unsafeRead v 0
writeRef (VRef v) = V.unsafeWrite v 0
modifyRef (VRef v) f = V.unsafeRead v 0 >>= V.unsafeWrite v 0 . f
modifyRef' = modifyRef