module Streamly.Internal.Data.IORef.Unboxed
(
IORef
, newIORef
, writeIORef
, modifyIORef'
, readIORef
, toStreamD
)
where
#include "inline.hs"
import Data.Proxy (Proxy(..))
import Control.Monad.IO.Class (MonadIO(..))
import Streamly.Internal.Data.Unboxed
( MutableByteArray(..)
, Unbox
, sizeOf
, peekWith
, pokeWith
, newUnpinnedBytes
)
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
newtype IORef a = IORef MutableByteArray
{-# INLINE newIORef #-}
newIORef :: forall a. Unbox a => a -> IO (IORef a)
newIORef :: forall a. Unbox a => a -> IO (IORef a)
newIORef a
x = do
MutableByteArray
var <- Int -> IO MutableByteArray
newUnpinnedBytes (forall a. Unbox a => Proxy a -> Int
sizeOf (forall {k} (t :: k). Proxy t
Proxy :: Proxy a))
forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
var Int
0 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutableByteArray -> IORef a
IORef MutableByteArray
var
{-# INLINE writeIORef #-}
writeIORef :: Unbox a => IORef a -> a -> IO ()
writeIORef :: forall a. Unbox a => IORef a -> a -> IO ()
writeIORef (IORef MutableByteArray
var) = forall a. Unbox a => MutableByteArray -> Int -> a -> IO ()
pokeWith MutableByteArray
var Int
0
{-# INLINE readIORef #-}
readIORef :: Unbox a => IORef a -> IO a
readIORef :: forall a. Unbox a => IORef a -> IO a
readIORef (IORef MutableByteArray
var) = forall a. Unbox a => MutableByteArray -> Int -> IO a
peekWith MutableByteArray
var Int
0
{-# INLINE modifyIORef' #-}
modifyIORef' :: Unbox a => IORef a -> (a -> a) -> IO ()
modifyIORef' :: forall a. Unbox a => IORef a -> (a -> a) -> IO ()
modifyIORef' IORef a
var a -> a
g = do
a
x <- forall a. Unbox a => IORef a -> IO a
readIORef IORef a
var
forall a. Unbox a => IORef a -> a -> IO ()
writeIORef IORef a
var (a -> a
g a
x)
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: (MonadIO m, Unbox a) => IORef a -> D.Stream m a
toStreamD :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
IORef a -> Stream m a
toStreamD IORef a
var = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {p}. MonadIO m => p -> () -> m (Step () a)
step ()
where
{-# INLINE_LATE step #-}
step :: p -> () -> m (Step () a)
step p
_ () = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. Unbox a => IORef a -> IO a
readIORef IORef a
var) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x ()