{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE CPP #-}
module Raaz.Core.Transfer
(
ReadM, ReadIO, bytesToRead, unsafeRead
, readBytes, readInto
, WriteM, WriteIO, bytesToWrite, unsafeWrite
, write, writeStorable, writeVector, writeStorableVector
, writeFrom, writeBytes
, padWrite, prependWrite, glueWrites
, writeByteString, skipWrite
) where
import Control.Monad.IO.Class
import Data.ByteString (ByteString)
import Data.String
import Data.ByteString.Internal (unsafeCreate)
import Data.Monoid
import qualified Data.Vector.Generic as G
import Data.Word (Word8)
import Foreign.Ptr (castPtr, Ptr)
import Foreign.Storable ( Storable, poke )
import Raaz.Core.MonoidalAction
import Raaz.Core.Types.Copying
import Raaz.Core.Types.Endian
import Raaz.Core.Types.Pointer
import Raaz.Core.Util.ByteString as BU
import Raaz.Core.Encode
newtype TransferM m = TransferM { TransferM m -> m ()
unTransferM :: m () }
#if MIN_VERSION_base(4,11,0)
instance Monad m => Semigroup (TransferM m) where
<> :: TransferM m -> TransferM m -> TransferM m
(<>) TransferM m
wa TransferM m
wb = m () -> TransferM m
forall (m :: * -> *). m () -> TransferM m
TransferM (m () -> TransferM m) -> m () -> TransferM m
forall a b. (a -> b) -> a -> b
$ TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM TransferM m
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM TransferM m
wb
#endif
instance Monad m => Monoid (TransferM m) where
mempty :: TransferM m
mempty = m () -> TransferM m
forall (m :: * -> *). m () -> TransferM m
TransferM (m () -> TransferM m) -> m () -> TransferM m
forall a b. (a -> b) -> a -> b
$ () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE mempty #-}
mappend :: TransferM m -> TransferM m -> TransferM m
mappend TransferM m
wa TransferM m
wb = m () -> TransferM m
forall (m :: * -> *). m () -> TransferM m
TransferM (m () -> TransferM m) -> m () -> TransferM m
forall a b. (a -> b) -> a -> b
$ TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM TransferM m
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM TransferM m
wb
{-# INLINE mappend #-}
mconcat :: [TransferM m] -> TransferM m
mconcat = m () -> TransferM m
forall (m :: * -> *). m () -> TransferM m
TransferM (m () -> TransferM m)
-> ([TransferM m] -> m ()) -> [TransferM m] -> TransferM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TransferM m -> m ()) -> [TransferM m] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM
{-# INLINE mconcat #-}
type TransferAction m = Pointer -> TransferM m
instance LAction (BYTES Int) (TransferAction m) where
BYTES Int
offset <.> :: BYTES Int -> TransferAction m -> TransferAction m
<.> TransferAction m
action = TransferAction m
action TransferAction m -> (Pointer -> Pointer) -> TransferAction m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BYTES Int
offsetBYTES Int -> Pointer -> Pointer
forall m space. LAction m space => m -> space -> space
<.>)
{-# INLINE (<.>) #-}
instance Monad m => Distributive (BYTES Int) (TransferAction m)
type Transfer m = SemiR (TransferAction m) (BYTES Int)
makeTransfer :: LengthUnit u => u -> (Pointer -> m ()) -> Transfer m
{-# INLINE makeTransfer #-}
makeTransfer :: u -> (Pointer -> m ()) -> Transfer m
makeTransfer u
sz Pointer -> m ()
action = (Pointer -> TransferM m) -> BYTES Int -> Transfer m
forall space m. space -> m -> SemiR space m
SemiR (m () -> TransferM m
forall (m :: * -> *). m () -> TransferM m
TransferM (m () -> TransferM m)
-> (Pointer -> m ()) -> Pointer -> TransferM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pointer -> m ()
action) (BYTES Int -> Transfer m) -> BYTES Int -> Transfer m
forall a b. (a -> b) -> a -> b
$ u -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes u
sz
newtype WriteM m = WriteM { WriteM m -> Transfer m
unWriteM :: Transfer m }
#if MIN_VERSION_base(4,11,0)
deriving (b -> WriteM m -> WriteM m
NonEmpty (WriteM m) -> WriteM m
WriteM m -> WriteM m -> WriteM m
(WriteM m -> WriteM m -> WriteM m)
-> (NonEmpty (WriteM m) -> WriteM m)
-> (forall b. Integral b => b -> WriteM m -> WriteM m)
-> Semigroup (WriteM m)
forall b. Integral b => b -> WriteM m -> WriteM m
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (m :: * -> *). Monad m => NonEmpty (WriteM m) -> WriteM m
forall (m :: * -> *). Monad m => WriteM m -> WriteM m -> WriteM m
forall (m :: * -> *) b.
(Monad m, Integral b) =>
b -> WriteM m -> WriteM m
stimes :: b -> WriteM m -> WriteM m
$cstimes :: forall (m :: * -> *) b.
(Monad m, Integral b) =>
b -> WriteM m -> WriteM m
sconcat :: NonEmpty (WriteM m) -> WriteM m
$csconcat :: forall (m :: * -> *). Monad m => NonEmpty (WriteM m) -> WriteM m
<> :: WriteM m -> WriteM m -> WriteM m
$c<> :: forall (m :: * -> *). Monad m => WriteM m -> WriteM m -> WriteM m
Semigroup, Semigroup (WriteM m)
WriteM m
Semigroup (WriteM m)
-> WriteM m
-> (WriteM m -> WriteM m -> WriteM m)
-> ([WriteM m] -> WriteM m)
-> Monoid (WriteM m)
[WriteM m] -> WriteM m
WriteM m -> WriteM m -> WriteM m
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (m :: * -> *). Monad m => Semigroup (WriteM m)
forall (m :: * -> *). Monad m => WriteM m
forall (m :: * -> *). Monad m => [WriteM m] -> WriteM m
forall (m :: * -> *). Monad m => WriteM m -> WriteM m -> WriteM m
mconcat :: [WriteM m] -> WriteM m
$cmconcat :: forall (m :: * -> *). Monad m => [WriteM m] -> WriteM m
mappend :: WriteM m -> WriteM m -> WriteM m
$cmappend :: forall (m :: * -> *). Monad m => WriteM m -> WriteM m -> WriteM m
mempty :: WriteM m
$cmempty :: forall (m :: * -> *). Monad m => WriteM m
$cp1Monoid :: forall (m :: * -> *). Monad m => Semigroup (WriteM m)
Monoid)
#else
deriving Monoid
#endif
type WriteIO = WriteM IO
bytesToWrite :: WriteM m -> BYTES Int
bytesToWrite :: WriteM m -> BYTES Int
bytesToWrite = SemiR (TransferAction m) (BYTES Int) -> BYTES Int
forall space m. SemiR space m -> m
semiRMonoid (SemiR (TransferAction m) (BYTES Int) -> BYTES Int)
-> (WriteM m -> SemiR (TransferAction m) (BYTES Int))
-> WriteM m
-> BYTES Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriteM m -> SemiR (TransferAction m) (BYTES Int)
forall (m :: * -> *). WriteM m -> Transfer m
unWriteM
unsafeWrite :: WriteM m
-> Pointer
-> m ()
unsafeWrite :: WriteM m -> Pointer -> m ()
unsafeWrite WriteM m
wr = TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM (TransferM m -> m ())
-> (Pointer -> TransferM m) -> Pointer -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SemiR (Pointer -> TransferM m) (BYTES Int)
-> Pointer -> TransferM m
forall space m. SemiR space m -> space
semiRSpace (WriteM m -> SemiR (Pointer -> TransferM m) (BYTES Int)
forall (m :: * -> *). WriteM m -> Transfer m
unWriteM WriteM m
wr)
makeWrite :: LengthUnit u => u -> (Pointer -> m ()) -> WriteM m
makeWrite :: u -> (Pointer -> m ()) -> WriteM m
makeWrite u
sz = Transfer m -> WriteM m
forall (m :: * -> *). Transfer m -> WriteM m
WriteM (Transfer m -> WriteM m)
-> ((Pointer -> m ()) -> Transfer m)
-> (Pointer -> m ())
-> WriteM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. u -> (Pointer -> m ()) -> Transfer m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> Transfer m
makeTransfer u
sz
writeStorable :: (MonadIO m, Storable a) => a -> WriteM m
writeStorable :: a -> WriteM m
writeStorable a
a = Transfer m -> WriteM m
forall (m :: * -> *). Transfer m -> WriteM m
WriteM (Transfer m -> WriteM m) -> Transfer m -> WriteM m
forall a b. (a -> b) -> a -> b
$ BYTES Int -> (Pointer -> m ()) -> Transfer m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> Transfer m
makeTransfer (a -> BYTES Int
forall a. Storable a => a -> BYTES Int
sizeOf a
a) Pointer -> m ()
forall a. Ptr a -> m ()
pokeIt
where pokeIt :: Ptr a -> m ()
pokeIt = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Ptr a -> IO ()) -> Ptr a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr a -> a -> IO ()) -> a -> Ptr a -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke a
a (Ptr a -> IO ()) -> (Ptr a -> Ptr a) -> Ptr a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr a -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr
write :: (MonadIO m, EndianStore a) => a -> WriteM m
write :: a -> WriteM m
write a
a = BYTES Int -> (Pointer -> m ()) -> WriteM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> WriteM m
makeWrite (a -> BYTES Int
forall a. Storable a => a -> BYTES Int
sizeOf a
a) ((Pointer -> m ()) -> WriteM m) -> (Pointer -> m ()) -> WriteM m
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Pointer -> IO ()) -> Pointer -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pointer -> a -> IO ()) -> a -> Pointer -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Ptr a -> a -> IO ()
forall w. EndianStore w => Ptr w -> w -> IO ()
store (Ptr a -> a -> IO ())
-> (Pointer -> Ptr a) -> Pointer -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pointer -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr) a
a
writeFrom :: (MonadIO m, EndianStore a) => Int -> Src (Ptr a) -> WriteM m
writeFrom :: Int -> Src (Ptr a) -> WriteM m
writeFrom Int
n Src (Ptr a)
src = BYTES Int -> (Pointer -> m ()) -> WriteM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> WriteM m
makeWrite (a -> Src (Ptr a) -> BYTES Int
forall a. Storable a => a -> Src (Ptr a) -> BYTES Int
sz a
forall a. HasCallStack => a
undefined Src (Ptr a)
src)
((Pointer -> m ()) -> WriteM m) -> (Pointer -> m ()) -> WriteM m
forall a b. (a -> b) -> a -> b
$ \ Pointer
ptr -> IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Dest Pointer -> Src (Ptr a) -> Int -> IO ()
forall w.
EndianStore w =>
Dest Pointer -> Src (Ptr w) -> Int -> IO ()
copyToBytes (Pointer -> Dest Pointer
forall a. a -> Dest a
destination Pointer
ptr) Src (Ptr a)
src Int
n
where sz :: Storable a => a -> Src (Ptr a) -> BYTES Int
sz :: a -> Src (Ptr a) -> BYTES Int
sz a
a Src (Ptr a)
_ = Int -> BYTES Int
forall a. Enum a => Int -> a
toEnum Int
n BYTES Int -> BYTES Int -> BYTES Int
forall a. Num a => a -> a -> a
* a -> BYTES Int
forall a. Storable a => a -> BYTES Int
sizeOf a
a
writeStorableVector :: (Storable a, G.Vector v a, MonadIO m) => v a -> WriteM m
{-# INLINE writeStorableVector #-}
writeStorableVector :: v a -> WriteM m
writeStorableVector = (WriteM m -> a -> WriteM m) -> WriteM m -> v a -> WriteM m
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' WriteM m -> a -> WriteM m
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
WriteM m -> a -> WriteM m
foldFunc WriteM m
forall a. Monoid a => a
mempty
where foldFunc :: WriteM m -> a -> WriteM m
foldFunc WriteM m
w a
a = WriteM m
w WriteM m -> WriteM m -> WriteM m
forall a. Semigroup a => a -> a -> a
<> a -> WriteM m
forall (m :: * -> *) a. (MonadIO m, Storable a) => a -> WriteM m
writeStorable a
a
writeVector :: (EndianStore a, G.Vector v a, MonadIO m) => v a -> WriteM m
{-# INLINE writeVector #-}
writeVector :: v a -> WriteM m
writeVector = (WriteM m -> a -> WriteM m) -> WriteM m -> v a -> WriteM m
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
G.foldl' WriteM m -> a -> WriteM m
forall (m :: * -> *) a.
(MonadIO m, EndianStore a) =>
WriteM m -> a -> WriteM m
foldFunc WriteM m
forall a. Monoid a => a
mempty
where foldFunc :: WriteM m -> a -> WriteM m
foldFunc WriteM m
w a
a = WriteM m
w WriteM m -> WriteM m -> WriteM m
forall a. Semigroup a => a -> a -> a
<> a -> WriteM m
forall (m :: * -> *) a. (MonadIO m, EndianStore a) => a -> WriteM m
write a
a
writeBytes :: (LengthUnit n, MonadIO m) => Word8 -> n -> WriteM m
writeBytes :: Word8 -> n -> WriteM m
writeBytes Word8
w8 n
n = n -> (Pointer -> m ()) -> WriteM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> WriteM m
makeWrite n
n Pointer -> m ()
forall (m :: * -> *). MonadIO m => Pointer -> m ()
memsetIt
where memsetIt :: Pointer -> m ()
memsetIt Pointer
cptr = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Pointer -> Word8 -> n -> IO ()
forall (m :: * -> *) l.
(MonadIO m, LengthUnit l) =>
Pointer -> Word8 -> l -> m ()
memset Pointer
cptr Word8
w8 n
n
glueWrites :: ( LengthUnit n, MonadIO m)
=> Word8
-> n
-> WriteM m
-> WriteM m
-> WriteM m
glueWrites :: Word8 -> n -> WriteM m -> WriteM m -> WriteM m
glueWrites Word8
w8 n
n WriteM m
hdr WriteM m
ftr = WriteM m
hdr WriteM m -> WriteM m -> WriteM m
forall a. Semigroup a => a -> a -> a
<> Word8 -> BYTES Int -> WriteM m
forall n (m :: * -> *).
(LengthUnit n, MonadIO m) =>
Word8 -> n -> WriteM m
writeBytes Word8
w8 BYTES Int
lglue WriteM m -> WriteM m -> WriteM m
forall a. Semigroup a => a -> a -> a
<> WriteM m
ftr
where lhead :: BYTES Int
lhead = WriteM m -> BYTES Int
forall (m :: * -> *). WriteM m -> BYTES Int
bytesToWrite WriteM m
hdr
lfoot :: BYTES Int
lfoot = WriteM m -> BYTES Int
forall (m :: * -> *). WriteM m -> BYTES Int
bytesToWrite WriteM m
ftr
lexceed :: BYTES Int
lexceed = (BYTES Int
lhead BYTES Int -> BYTES Int -> BYTES Int
forall a. Num a => a -> a -> a
+ BYTES Int
lfoot) BYTES Int -> BYTES Int -> BYTES Int
forall a. Integral a => a -> a -> a
`rem` BYTES Int
nBytes
lglue :: BYTES Int
lglue = BYTES Int
nBytes BYTES Int -> BYTES Int -> BYTES Int
forall a. Num a => a -> a -> a
- BYTES Int
lexceed
nBytes :: BYTES Int
nBytes = n -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes n
n
prependWrite :: ( LengthUnit n, MonadIO m)
=> Word8
-> n
-> WriteM m
-> WriteM m
prependWrite :: Word8 -> n -> WriteM m -> WriteM m
prependWrite Word8
w8 n
n = Word8 -> n -> WriteM m -> WriteM m -> WriteM m
forall n (m :: * -> *).
(LengthUnit n, MonadIO m) =>
Word8 -> n -> WriteM m -> WriteM m -> WriteM m
glueWrites Word8
w8 n
n WriteM m
forall a. Monoid a => a
mempty
padWrite :: ( LengthUnit n, MonadIO m)
=> Word8
-> n
-> WriteM m
-> WriteM m
padWrite :: Word8 -> n -> WriteM m -> WriteM m
padWrite Word8
w8 n
n = (WriteM m -> WriteM m -> WriteM m)
-> WriteM m -> WriteM m -> WriteM m
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Word8 -> n -> WriteM m -> WriteM m -> WriteM m
forall n (m :: * -> *).
(LengthUnit n, MonadIO m) =>
Word8 -> n -> WriteM m -> WriteM m -> WriteM m
glueWrites Word8
w8 n
n) WriteM m
forall a. Monoid a => a
mempty
writeByteString :: MonadIO m => ByteString -> WriteM m
writeByteString :: ByteString -> WriteM m
writeByteString ByteString
bs = BYTES Int -> (Pointer -> m ()) -> WriteM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> WriteM m
makeWrite (ByteString -> BYTES Int
BU.length ByteString
bs) ((Pointer -> m ()) -> WriteM m) -> (Pointer -> m ()) -> WriteM m
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Pointer -> IO ()) -> Pointer -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Pointer -> IO ()
BU.unsafeCopyToPointer ByteString
bs
skipWrite :: (LengthUnit u, Monad m) => u -> WriteM m
skipWrite :: u -> WriteM m
skipWrite = (u -> (Pointer -> m ()) -> WriteM m)
-> (Pointer -> m ()) -> u -> WriteM m
forall a b c. (a -> b -> c) -> b -> a -> c
flip u -> (Pointer -> m ()) -> WriteM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> WriteM m
makeWrite ((Pointer -> m ()) -> u -> WriteM m)
-> (Pointer -> m ()) -> u -> WriteM m
forall a b. (a -> b) -> a -> b
$ m () -> Pointer -> m ()
forall a b. a -> b -> a
const (m () -> Pointer -> m ()) -> m () -> Pointer -> m ()
forall a b. (a -> b) -> a -> b
$ () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance MonadIO m => IsString (WriteM m) where
fromString :: String -> WriteM m
fromString = ByteString -> WriteM m
forall (m :: * -> *). MonadIO m => ByteString -> WriteM m
writeByteString (ByteString -> WriteM m)
-> (String -> ByteString) -> String -> WriteM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
forall a. IsString a => String -> a
fromString
instance Encodable (WriteM IO) where
{-# INLINE toByteString #-}
toByteString :: WriteM IO -> ByteString
toByteString WriteM IO
w = Int -> (Ptr Word8 -> IO ()) -> ByteString
unsafeCreate Int
n ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ WriteM IO -> Pointer -> IO ()
forall (m :: * -> *). WriteM m -> Pointer -> m ()
unsafeWrite WriteM IO
w (Pointer -> IO ()) -> (Ptr Word8 -> Pointer) -> Ptr Word8 -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Word8 -> Pointer
forall a b. Ptr a -> Ptr b
castPtr
where BYTES Int
n = WriteM IO -> BYTES Int
forall (m :: * -> *). WriteM m -> BYTES Int
bytesToWrite WriteM IO
w
{-# INLINE unsafeFromByteString #-}
unsafeFromByteString :: ByteString -> WriteM IO
unsafeFromByteString = ByteString -> WriteM IO
forall (m :: * -> *). MonadIO m => ByteString -> WriteM m
writeByteString
{-# INLINE fromByteString #-}
fromByteString :: ByteString -> Maybe (WriteM IO)
fromByteString = WriteM IO -> Maybe (WriteM IO)
forall a. a -> Maybe a
Just (WriteM IO -> Maybe (WriteM IO))
-> (ByteString -> WriteM IO) -> ByteString -> Maybe (WriteM IO)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> WriteM IO
forall (m :: * -> *). MonadIO m => ByteString -> WriteM m
writeByteString
newtype ReadM m = ReadM { ReadM m -> Transfer m
unReadM :: Transfer m}
#if MIN_VERSION_base(4,11,0)
deriving (b -> ReadM m -> ReadM m
NonEmpty (ReadM m) -> ReadM m
ReadM m -> ReadM m -> ReadM m
(ReadM m -> ReadM m -> ReadM m)
-> (NonEmpty (ReadM m) -> ReadM m)
-> (forall b. Integral b => b -> ReadM m -> ReadM m)
-> Semigroup (ReadM m)
forall b. Integral b => b -> ReadM m -> ReadM m
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (m :: * -> *). Monad m => NonEmpty (ReadM m) -> ReadM m
forall (m :: * -> *). Monad m => ReadM m -> ReadM m -> ReadM m
forall (m :: * -> *) b.
(Monad m, Integral b) =>
b -> ReadM m -> ReadM m
stimes :: b -> ReadM m -> ReadM m
$cstimes :: forall (m :: * -> *) b.
(Monad m, Integral b) =>
b -> ReadM m -> ReadM m
sconcat :: NonEmpty (ReadM m) -> ReadM m
$csconcat :: forall (m :: * -> *). Monad m => NonEmpty (ReadM m) -> ReadM m
<> :: ReadM m -> ReadM m -> ReadM m
$c<> :: forall (m :: * -> *). Monad m => ReadM m -> ReadM m -> ReadM m
Semigroup, Semigroup (ReadM m)
ReadM m
Semigroup (ReadM m)
-> ReadM m
-> (ReadM m -> ReadM m -> ReadM m)
-> ([ReadM m] -> ReadM m)
-> Monoid (ReadM m)
[ReadM m] -> ReadM m
ReadM m -> ReadM m -> ReadM m
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (m :: * -> *). Monad m => Semigroup (ReadM m)
forall (m :: * -> *). Monad m => ReadM m
forall (m :: * -> *). Monad m => [ReadM m] -> ReadM m
forall (m :: * -> *). Monad m => ReadM m -> ReadM m -> ReadM m
mconcat :: [ReadM m] -> ReadM m
$cmconcat :: forall (m :: * -> *). Monad m => [ReadM m] -> ReadM m
mappend :: ReadM m -> ReadM m -> ReadM m
$cmappend :: forall (m :: * -> *). Monad m => ReadM m -> ReadM m -> ReadM m
mempty :: ReadM m
$cmempty :: forall (m :: * -> *). Monad m => ReadM m
$cp1Monoid :: forall (m :: * -> *). Monad m => Semigroup (ReadM m)
Monoid)
#else
deriving Monoid
#endif
type ReadIO = ReadM IO
makeRead :: LengthUnit u => u -> (Pointer -> m ()) -> ReadM m
makeRead :: u -> (Pointer -> m ()) -> ReadM m
makeRead u
sz = Transfer m -> ReadM m
forall (m :: * -> *). Transfer m -> ReadM m
ReadM (Transfer m -> ReadM m)
-> ((Pointer -> m ()) -> Transfer m)
-> (Pointer -> m ())
-> ReadM m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. u -> (Pointer -> m ()) -> Transfer m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> Transfer m
makeTransfer u
sz
bytesToRead :: ReadM m -> BYTES Int
bytesToRead :: ReadM m -> BYTES Int
bytesToRead = SemiR (TransferAction m) (BYTES Int) -> BYTES Int
forall space m. SemiR space m -> m
semiRMonoid (SemiR (TransferAction m) (BYTES Int) -> BYTES Int)
-> (ReadM m -> SemiR (TransferAction m) (BYTES Int))
-> ReadM m
-> BYTES Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReadM m -> SemiR (TransferAction m) (BYTES Int)
forall (m :: * -> *). ReadM m -> Transfer m
unReadM
unsafeRead :: ReadM m
-> Pointer
-> m ()
unsafeRead :: ReadM m -> Pointer -> m ()
unsafeRead ReadM m
rd = TransferM m -> m ()
forall (m :: * -> *). TransferM m -> m ()
unTransferM (TransferM m -> m ())
-> (Pointer -> TransferM m) -> Pointer -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SemiR (Pointer -> TransferM m) (BYTES Int)
-> Pointer -> TransferM m
forall space m. SemiR space m -> space
semiRSpace (ReadM m -> SemiR (Pointer -> TransferM m) (BYTES Int)
forall (m :: * -> *). ReadM m -> Transfer m
unReadM ReadM m
rd)
readBytes :: ( LengthUnit sz, MonadIO m)
=> sz
-> Dest Pointer
-> ReadM m
readBytes :: sz -> Dest Pointer -> ReadM m
readBytes sz
sz Dest Pointer
dest = sz -> (Pointer -> m ()) -> ReadM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> ReadM m
makeRead sz
sz
((Pointer -> m ()) -> ReadM m) -> (Pointer -> m ()) -> ReadM m
forall a b. (a -> b) -> a -> b
$ \ Pointer
ptr -> IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Dest Pointer -> Src Pointer -> sz -> IO ()
forall (m :: * -> *) l.
(MonadIO m, LengthUnit l) =>
Dest Pointer -> Src Pointer -> l -> m ()
memcpy Dest Pointer
dest (Pointer -> Src Pointer
forall a. a -> Src a
source Pointer
ptr) sz
sz
readInto :: (EndianStore a, MonadIO m)
=> Int
-> Dest (Ptr a)
-> ReadM m
readInto :: Int -> Dest (Ptr a) -> ReadM m
readInto Int
n Dest (Ptr a)
dest = BYTES Int -> (Pointer -> m ()) -> ReadM m
forall u (m :: * -> *).
LengthUnit u =>
u -> (Pointer -> m ()) -> ReadM m
makeRead (a -> Dest (Ptr a) -> BYTES Int
forall a. Storable a => a -> Dest (Ptr a) -> BYTES Int
sz a
forall a. HasCallStack => a
undefined Dest (Ptr a)
dest)
((Pointer -> m ()) -> ReadM m) -> (Pointer -> m ()) -> ReadM m
forall a b. (a -> b) -> a -> b
$ \ Pointer
ptr -> IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Dest (Ptr a) -> Src Pointer -> Int -> IO ()
forall w.
EndianStore w =>
Dest (Ptr w) -> Src Pointer -> Int -> IO ()
copyFromBytes Dest (Ptr a)
dest (Pointer -> Src Pointer
forall a. a -> Src a
source Pointer
ptr) Int
n
where sz :: Storable a => a -> Dest (Ptr a) -> BYTES Int
sz :: a -> Dest (Ptr a) -> BYTES Int
sz a
a Dest (Ptr a)
_ = Int -> BYTES Int
forall a. Enum a => Int -> a
toEnum Int
n BYTES Int -> BYTES Int -> BYTES Int
forall a. Num a => a -> a -> a
* a -> BYTES Int
forall a. Storable a => a -> BYTES Int
sizeOf a
a