{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
#include "inline.hs"
module Streamly.Internal.FileSystem.Handle
(
read
, readWithBufferOf
, toBytes
, toBytesWithBufferOf
, getBytes
, readChunks
, readChunksWithBufferOf
, toChunksWithBufferOf
, toChunks
, getChunks
, write
, write2
, writeWithBufferOf
, fromBytes
, fromBytesWithBufferOf
, writeArray
, writeChunks
, writeChunksWithBufferOf
, fromChunksWithBufferOf
, fromChunks
, putChunks
, putStrings
, putBytes
, putLines
)
where
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import Foreign.Ptr (minusPtr, plusPtr)
import Foreign.Storable (Storable(..))
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
import Prelude hiding (read)
import Streamly (MonadAsync)
import Streamly.Data.Fold (Fold)
import Streamly.Internal.Data.Fold.Types (Fold2(..))
import Streamly.Internal.Data.Unfold.Types (Unfold(..))
import Streamly.Internal.Memory.Array.Types
(Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
lpackArraysChunksOf)
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
import qualified Streamly.Data.Fold as FL
import qualified Streamly.Internal.Data.Fold.Types as FL
import qualified Streamly.Internal.Data.Unfold as UF
import qualified Streamly.Internal.Memory.Array as IA
import qualified Streamly.Internal.Memory.ArrayStream as AS
import qualified Streamly.Internal.Prelude as S
import qualified Streamly.Memory.Array as A
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
{-# INLINABLE readArrayUpto #-}
readArrayUpto :: Int -> Handle -> IO (Array Word8)
readArrayUpto :: Int -> Handle -> IO (Array Word8)
readArrayUpto Int
size Handle
h = do
ForeignPtr Word8
ptr <- Int -> IO (ForeignPtr Word8)
forall a. Int -> IO (ForeignPtr a)
mallocPlainForeignPtrBytes Int
size
ForeignPtr Word8
-> (Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
ptr ((Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8))
-> (Ptr Word8 -> IO (Array Word8)) -> IO (Array Word8)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
Int
n <- Handle -> Ptr Word8 -> Int -> IO Int
forall a. Handle -> Ptr a -> Int -> IO Int
hGetBufSome Handle
h Ptr Word8
p Int
size
let v :: Array Word8
v = Array :: forall a. ForeignPtr a -> Ptr a -> Ptr a -> Array a
Array
{ aStart :: ForeignPtr Word8
aStart = ForeignPtr Word8
ptr
, aEnd :: Ptr Word8
aEnd = Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n
, aBound :: Ptr Word8
aBound = Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size
}
Array Word8 -> IO (Array Word8)
forall a. Storable a => Array a -> IO (Array a)
shrinkToFit Array Word8
v
{-# INLINABLE _toChunksWithBufferOf #-}
_toChunksWithBufferOf :: (IsStream t, MonadIO m)
=> Int -> Handle -> t m (Array Word8)
_toChunksWithBufferOf :: Int -> Handle -> t m (Array Word8)
_toChunksWithBufferOf Int
size Handle
h = t m (Array Word8)
go
where
go :: t m (Array Word8)
go = (forall r.
State Stream m (Array Word8)
-> (Array Word8 -> t m (Array Word8) -> m r)
-> (Array Word8 -> m r)
-> m r
-> m r)
-> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
(forall r.
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> m r)
-> t m a
mkStream ((forall r.
State Stream m (Array Word8)
-> (Array Word8 -> t m (Array Word8) -> m r)
-> (Array Word8 -> m r)
-> m r
-> m r)
-> t m (Array Word8))
-> (forall r.
State Stream m (Array Word8)
-> (Array Word8 -> t m (Array Word8) -> m r)
-> (Array Word8 -> m r)
-> m r
-> m r)
-> t m (Array Word8)
forall a b. (a -> b) -> a -> b
$ \State Stream m (Array Word8)
_ Array Word8 -> t m (Array Word8) -> m r
yld Array Word8 -> m r
_ m r
stp -> do
Array Word8
arr <- IO (Array Word8) -> m (Array Word8)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8) -> m (Array Word8))
-> IO (Array Word8) -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Handle -> IO (Array Word8)
readArrayUpto Int
size Handle
h
if Array Word8 -> Int
forall a. Storable a => Array a -> Int
A.length Array Word8
arr Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then m r
stp
else Array Word8 -> t m (Array Word8) -> m r
yld Array Word8
arr t m (Array Word8)
go
{-# INLINE_NORMAL toChunksWithBufferOf #-}
toChunksWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
toChunksWithBufferOf :: Int -> Handle -> t m (Array Word8)
toChunksWithBufferOf Int
size Handle
h = Stream m (Array Word8) -> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD ((State Stream m (Array Word8) -> () -> m (Step () (Array Word8)))
-> () -> Stream m (Array Word8)
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m (Array Word8) -> () -> m (Step () (Array Word8))
forall (m :: * -> *) p p.
MonadIO m =>
p -> p -> m (Step () (Array Word8))
step ())
where
{-# INLINE_LATE step #-}
step :: p -> p -> m (Step () (Array Word8))
step p
_ p
_ = do
Array Word8
arr <- IO (Array Word8) -> m (Array Word8)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8) -> m (Array Word8))
-> IO (Array Word8) -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Handle -> IO (Array Word8)
readArrayUpto Int
size Handle
h
Step () (Array Word8) -> m (Step () (Array Word8))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step () (Array Word8) -> m (Step () (Array Word8)))
-> Step () (Array Word8) -> m (Step () (Array Word8))
forall a b. (a -> b) -> a -> b
$
case Array Word8 -> Int
forall a. Storable a => Array a -> Int
A.length Array Word8
arr of
Int
0 -> Step () (Array Word8)
forall s a. Step s a
D.Stop
Int
_ -> Array Word8 -> () -> Step () (Array Word8)
forall s a. a -> s -> Step s a
D.Yield Array Word8
arr ()
{-# INLINE_NORMAL readChunksWithBufferOf #-}
readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
readChunksWithBufferOf :: Unfold m (Int, Handle) (Array Word8)
readChunksWithBufferOf = ((Int, Handle) -> m (Step (Int, Handle) (Array Word8)))
-> ((Int, Handle) -> m (Int, Handle))
-> Unfold m (Int, Handle) (Array Word8)
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (Int, Handle) -> m (Step (Int, Handle) (Array Word8))
forall (m :: * -> *).
MonadIO m =>
(Int, Handle) -> m (Step (Int, Handle) (Array Word8))
step (Int, Handle) -> m (Int, Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return
where
{-# INLINE_LATE step #-}
step :: (Int, Handle) -> m (Step (Int, Handle) (Array Word8))
step (Int
size, Handle
h) = do
Array Word8
arr <- IO (Array Word8) -> m (Array Word8)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Array Word8) -> m (Array Word8))
-> IO (Array Word8) -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Handle -> IO (Array Word8)
readArrayUpto Int
size Handle
h
Step (Int, Handle) (Array Word8)
-> m (Step (Int, Handle) (Array Word8))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Int, Handle) (Array Word8)
-> m (Step (Int, Handle) (Array Word8)))
-> Step (Int, Handle) (Array Word8)
-> m (Step (Int, Handle) (Array Word8))
forall a b. (a -> b) -> a -> b
$
case Array Word8 -> Int
forall a. Storable a => Array a -> Int
A.length Array Word8
arr of
Int
0 -> Step (Int, Handle) (Array Word8)
forall s a. Step s a
D.Stop
Int
_ -> Array Word8 -> (Int, Handle) -> Step (Int, Handle) (Array Word8)
forall s a. a -> s -> Step s a
D.Yield Array Word8
arr (Int
size, Handle
h)
{-# INLINE toChunks #-}
toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
toChunks :: Handle -> t m (Array Word8)
toChunks = Int -> Handle -> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Int -> Handle -> t m (Array Word8)
toChunksWithBufferOf Int
defaultChunkSize
{-# INLINE getChunks #-}
getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
getChunks :: t m (Array Word8)
getChunks = Handle -> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Handle -> t m (Array Word8)
toChunks Handle
stdin
{-# INLINE getBytes #-}
getBytes :: (IsStream t, MonadIO m) => t m Word8
getBytes :: t m Word8
getBytes = Handle -> t m Word8
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Handle -> t m Word8
toBytes Handle
stdin
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Unfold m Handle (Array Word8)
readChunks :: Unfold m Handle (Array Word8)
readChunks = Unfold m (Int, Handle) (Array Word8)
-> Int -> Unfold m Handle (Array Word8)
forall (m :: * -> *) a b c. Unfold m (a, b) c -> a -> Unfold m b c
UF.supplyFirst Unfold m (Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Handle) (Array Word8)
readChunksWithBufferOf Int
defaultChunkSize
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
readWithBufferOf :: Unfold m (Int, Handle) Word8
readWithBufferOf = Unfold m (Int, Handle) (Array Word8)
-> Unfold m (Array Word8) Word8 -> Unfold m (Int, Handle) Word8
forall (m :: * -> *) a b c.
Monad m =>
Unfold m a b -> Unfold m b c -> Unfold m a c
UF.concat Unfold m (Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Handle) (Array Word8)
readChunksWithBufferOf Unfold m (Array Word8) Word8
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
A.read
{-# INLINE toBytesWithBufferOf #-}
toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
toBytesWithBufferOf :: Int -> Handle -> t m Word8
toBytesWithBufferOf Int
chunkSize Handle
h = t m (Array Word8) -> t m Word8
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
t m (Array a) -> t m a
AS.concat (t m (Array Word8) -> t m Word8) -> t m (Array Word8) -> t m Word8
forall a b. (a -> b) -> a -> b
$ Int -> Handle -> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Int -> Handle -> t m (Array Word8)
toChunksWithBufferOf Int
chunkSize Handle
h
{-# INLINE read #-}
read :: MonadIO m => Unfold m Handle Word8
read :: Unfold m Handle Word8
read = Unfold m (Int, Handle) Word8 -> Int -> Unfold m Handle Word8
forall (m :: * -> *) a b c. Unfold m (a, b) c -> a -> Unfold m b c
UF.supplyFirst Unfold m (Int, Handle) Word8
forall (m :: * -> *). MonadIO m => Unfold m (Int, Handle) Word8
readWithBufferOf Int
defaultChunkSize
{-# INLINE toBytes #-}
toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
toBytes :: Handle -> t m Word8
toBytes = t m (Array Word8) -> t m Word8
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
t m (Array a) -> t m a
AS.concat (t m (Array Word8) -> t m Word8)
-> (Handle -> t m (Array Word8)) -> Handle -> t m Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> t m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Handle -> t m (Array Word8)
toChunks
{-# INLINABLE writeArray #-}
writeArray :: Storable a => Handle -> Array a -> IO ()
writeArray :: Handle -> Array a -> IO ()
writeArray Handle
_ Array a
arr | Array a -> Int
forall a. Storable a => Array a -> Int
A.length Array a
arr Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
writeArray Handle
h Array{Ptr a
ForeignPtr a
aBound :: Ptr a
aEnd :: Ptr a
aStart :: ForeignPtr a
aBound :: forall a. Array a -> Ptr a
aEnd :: forall a. Array a -> Ptr a
aStart :: forall a. Array a -> ForeignPtr a
..} = ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
aStart ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Handle -> Ptr a -> Int -> IO ()
forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
h Ptr a
p Int
aLen
where
aLen :: Int
aLen =
let p :: Ptr a
p = ForeignPtr a -> Ptr a
forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr a
aStart
in Ptr a
aEnd Ptr a -> Ptr a -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr a
p
{-# INLINE fromChunks #-}
fromChunks :: (MonadIO m, Storable a)
=> Handle -> SerialT m (Array a) -> m ()
fromChunks :: Handle -> SerialT m (Array a) -> m ()
fromChunks Handle
h = (Array a -> m ()) -> SerialT m (Array a) -> m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SerialT m a -> m ()
S.mapM_ (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Array a -> IO ()) -> Array a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Array a -> IO ()
forall a. Storable a => Handle -> Array a -> IO ()
writeArray Handle
h)
{-# INLINE putChunks #-}
putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
putChunks :: SerialT m (Array a) -> m ()
putChunks = Handle -> SerialT m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> SerialT m (Array a) -> m ()
fromChunks Handle
stdout
{-# INLINE putStrings #-}
putStrings :: MonadAsync m
=> (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putStrings :: (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putStrings SerialT m Char -> SerialT m Word8
encode = SerialT m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m ()
putChunks (SerialT m (Array Word8) -> m ())
-> (SerialT m String -> SerialT m (Array Word8))
-> SerialT m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> m (Array Word8))
-> SerialT m String -> SerialT m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadAsync m) =>
(a -> m b) -> t m a -> t m b
S.mapM (SerialT m Word8 -> m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m a -> m (Array a)
IA.fromStream (SerialT m Word8 -> m (Array Word8))
-> (String -> SerialT m Word8) -> String -> m (Array Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m Char -> SerialT m Word8
encode (SerialT m Char -> SerialT m Word8)
-> (String -> SerialT m Char) -> String -> SerialT m Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SerialT m Char
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
[a] -> t m a
S.fromList)
{-# INLINE putLines #-}
putLines :: MonadAsync m
=> (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putLines :: (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putLines SerialT m Char -> SerialT m Word8
encode = SerialT m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m (Array a) -> m ()
putChunks (SerialT m (Array Word8) -> m ())
-> (SerialT m String -> SerialT m (Array Word8))
-> SerialT m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> m (Array Word8))
-> SerialT m String -> SerialT m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadAsync m) =>
(a -> m b) -> t m a -> t m b
S.mapM
(\String
xs -> SerialT m Word8 -> m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
SerialT m a -> m (Array a)
IA.fromStream (SerialT m Word8 -> m (Array Word8))
-> SerialT m Word8 -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ SerialT m Char -> SerialT m Word8
encode (String -> SerialT m Char
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
[a] -> t m a
S.fromList (String
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n")))
{-# INLINE putBytes #-}
putBytes :: MonadIO m => SerialT m Word8 -> m ()
putBytes :: SerialT m Word8 -> m ()
putBytes = Handle -> SerialT m Word8 -> m ()
forall (m :: * -> *).
MonadIO m =>
Handle -> SerialT m Word8 -> m ()
fromBytes Handle
stdout
{-# INLINE fromChunksWithBufferOf #-}
fromChunksWithBufferOf :: (MonadIO m, Storable a)
=> Int -> Handle -> SerialT m (Array a) -> m ()
fromChunksWithBufferOf :: Int -> Handle -> SerialT m (Array a) -> m ()
fromChunksWithBufferOf Int
n Handle
h SerialT m (Array a)
xs = Handle -> SerialT m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> SerialT m (Array a) -> m ()
fromChunks Handle
h (SerialT m (Array a) -> m ()) -> SerialT m (Array a) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> SerialT m (Array a) -> SerialT m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> SerialT m (Array a) -> SerialT m (Array a)
AS.compact Int
n SerialT m (Array a)
xs
{-# INLINE fromBytesWithBufferOf #-}
fromBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
fromBytesWithBufferOf :: Int -> Handle -> SerialT m Word8 -> m ()
fromBytesWithBufferOf Int
n Handle
h SerialT m Word8
m = Handle -> SerialT m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> SerialT m (Array a) -> m ()
fromChunks Handle
h (SerialT m (Array Word8) -> m ())
-> SerialT m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> SerialT m Word8 -> SerialT m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadIO m, Storable a) =>
Int -> t m a -> t m (Array a)
S.arraysOf Int
n SerialT m Word8
m
{-# INLINE fromBytes #-}
fromBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
fromBytes :: Handle -> SerialT m Word8 -> m ()
fromBytes = Int -> Handle -> SerialT m Word8 -> m ()
forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> SerialT m Word8 -> m ()
fromBytesWithBufferOf Int
defaultChunkSize
{-# INLINE writeChunks #-}
writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
writeChunks :: Handle -> Fold m (Array a) ()
writeChunks Handle
h = (Array a -> m ()) -> Fold m (Array a) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Fold m a ()
FL.drainBy (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Array a -> IO ()) -> Array a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Array a -> IO ()
forall a. Storable a => Handle -> Array a -> IO ()
writeArray Handle
h)
{-# INLINE writeChunks2 #-}
writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
writeChunks2 :: Fold2 m Handle (Array a) ()
writeChunks2 = (Handle -> Array a -> m Handle)
-> (Handle -> m Handle)
-> (Handle -> m ())
-> Fold2 m Handle (Array a) ()
forall (m :: * -> *) c a b s.
(s -> a -> m s) -> (c -> m s) -> (s -> m b) -> Fold2 m c a b
Fold2 (\Handle
h Array a
arr -> IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> m Handle) -> IO Handle -> m Handle
forall a b. (a -> b) -> a -> b
$ Handle -> Array a -> IO ()
forall a. Storable a => Handle -> Array a -> IO ()
writeArray Handle
h Array a
arr IO () -> IO Handle -> IO Handle
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO Handle
forall (m :: * -> *) a. Monad m => a -> m a
return Handle
h) Handle -> m Handle
forall (m :: * -> *) a. Monad m => a -> m a
return (\Handle
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINE writeChunksWithBufferOf #-}
writeChunksWithBufferOf :: (MonadIO m, Storable a)
=> Int -> Handle -> Fold m (Array a) ()
writeChunksWithBufferOf :: Int -> Handle -> Fold m (Array a) ()
writeChunksWithBufferOf Int
n Handle
h = Int -> Fold m (Array a) () -> Fold m (Array a) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf Int
n (Handle -> Fold m (Array a) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> Fold m (Array a) ()
writeChunks Handle
h)
{-# INLINE writeWithBufferOf #-}
writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
writeWithBufferOf :: Int -> Handle -> Fold m Word8 ()
writeWithBufferOf Int
n Handle
h = Int
-> Fold m Word8 (Array Word8)
-> Fold m (Array Word8) ()
-> Fold m Word8 ()
forall (m :: * -> *) a b c.
Monad m =>
Int -> Fold m a b -> Fold m b c -> Fold m a c
FL.lchunksOf Int
n (Int -> Fold m Word8 (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeNUnsafe Int
n) (Handle -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> Fold m (Array a) ()
writeChunks Handle
h)
{-# INLINE writeWithBufferOf2 #-}
writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
writeWithBufferOf2 :: Int -> Fold2 m Handle Word8 ()
writeWithBufferOf2 Int
n = Int
-> Fold m Word8 (Array Word8)
-> Fold2 m Handle (Array Word8) ()
-> Fold2 m Handle Word8 ()
forall (m :: * -> *) a b x c.
Monad m =>
Int -> Fold m a b -> Fold2 m x b c -> Fold2 m x a c
FL.lchunksOf2 Int
n (Int -> Fold m Word8 (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeNUnsafe Int
n) Fold2 m Handle (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold2 m Handle (Array a) ()
writeChunks2
{-# INLINE write #-}
write :: MonadIO m => Handle -> Fold m Word8 ()
write :: Handle -> Fold m Word8 ()
write = Int -> Handle -> Fold m Word8 ()
forall (m :: * -> *). MonadIO m => Int -> Handle -> Fold m Word8 ()
writeWithBufferOf Int
defaultChunkSize
{-# INLINE write2 #-}
write2 :: MonadIO m => Fold2 m Handle Word8 ()
write2 :: Fold2 m Handle Word8 ()
write2 = Int -> Fold2 m Handle Word8 ()
forall (m :: * -> *). MonadIO m => Int -> Fold2 m Handle Word8 ()
writeWithBufferOf2 Int
defaultChunkSize