module Data.Repa.Convert.Internal.Packer
( Packer (..)
, unsafeRunPacker)
where
import Data.Word
import Data.IORef
import GHC.Exts
import qualified Foreign.Ptr as F
data Packer
= Packer
{
fromPacker
:: Addr#
-> IO ()
-> (Addr# -> IO ())
-> IO ()
}
instance Semigroup Packer where
(<>) = mappend
instance Monoid Packer where
mempty
= Packer $ \buf _fail k -> k buf
{-# INLINE mempty #-}
mappend (Packer fa) (Packer fb)
= Packer $ \buf0 fails k -> fa buf0 fails (\buf1 -> fb buf1 fails k)
{-# INLINE mappend #-}
unsafeRunPacker
:: Packer
-> F.Ptr Word8
-> IO (Maybe (F.Ptr Word8))
unsafeRunPacker (Packer make) (Ptr addr)
= do ref <- newIORef Nothing
make addr
(return ())
(\addr' -> writeIORef ref (Just (Ptr addr')))
readIORef ref
{-# INLINE unsafeRunPacker #-}