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