{-# LANGUAGE CPP #-}
module Streamly.External.ByteString.Lazy
( chunkReader
, reader
, toChunks
, fromChunks
, fromChunksIO
, read
, readChunks
)
where
import Data.Word (Word8)
import Streamly.Data.Array (Array)
import System.IO.Unsafe (unsafeInterleaveIO)
import Streamly.Data.Stream (Stream)
import Data.ByteString.Lazy.Internal (ByteString(..), chunk)
#if MIN_VERSION_streamly_core(0,2,0)
import Streamly.Internal.Data.Unfold (Unfold(..))
import Streamly.Internal.Data.Stream (Step(..))
#else
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
#endif
import qualified Streamly.External.ByteString as Strict
import qualified Streamly.Data.Array as Array
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.Data.Stream as Stream
import Prelude hiding (read)
{-# INLINE chunkReader #-}
chunkReader :: Monad m => Unfold m ByteString (Array Word8)
chunkReader :: forall (m :: * -> *). Monad m => Unfold m ByteString (Array Word8)
chunkReader = (ByteString -> m (Step ByteString (Array Word8)))
-> (ByteString -> m ByteString)
-> Unfold m ByteString (Array Word8)
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold ByteString -> m (Step ByteString (Array Word8))
forall {m :: * -> *}.
Monad m =>
ByteString -> m (Step ByteString (Array Word8))
step ByteString -> m ByteString
forall {a}. a -> m a
seed
where
seed :: a -> m a
seed = a -> m a
forall {a}. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
step :: ByteString -> m (Step ByteString (Array Word8))
step (Chunk ByteString
bs ByteString
bl) = Step ByteString (Array Word8) -> m (Step ByteString (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step ByteString (Array Word8)
-> m (Step ByteString (Array Word8)))
-> Step ByteString (Array Word8)
-> m (Step ByteString (Array Word8))
forall a b. (a -> b) -> a -> b
$ Array Word8 -> ByteString -> Step ByteString (Array Word8)
forall s a. a -> s -> Step s a
Yield (ByteString -> Array Word8
Strict.toArray ByteString
bs) ByteString
bl
step ByteString
Empty = Step ByteString (Array Word8) -> m (Step ByteString (Array Word8))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step ByteString (Array Word8)
forall s a. Step s a
Stop
{-# INLINE reader #-}
reader :: Monad m => Unfold m ByteString Word8
reader :: forall (m :: * -> *). Monad m => Unfold m ByteString Word8
reader = Unfold m (Array Word8) Word8
-> Unfold m ByteString (Array Word8) -> Unfold m ByteString Word8
forall (m :: * -> *) b c a.
Monad m =>
Unfold m b c -> Unfold m a b -> Unfold m a c
Unfold.many Unfold m (Array Word8) Word8
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
Array.reader Unfold m ByteString (Array Word8)
forall (m :: * -> *). Monad m => Unfold m ByteString (Array Word8)
readChunks
{-# INLINE toChunks #-}
toChunks :: Monad m => ByteString -> Stream m (Array Word8)
toChunks :: forall (m :: * -> *).
Monad m =>
ByteString -> Stream m (Array Word8)
toChunks = Unfold m ByteString (Array Word8)
-> ByteString -> Stream m (Array Word8)
forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> a -> Stream m b
Stream.unfold Unfold m ByteString (Array Word8)
forall (m :: * -> *). Monad m => Unfold m ByteString (Array Word8)
readChunks
{-# INLINE fromChunks #-}
fromChunks :: Monad m => Stream m (Array Word8) -> m ByteString
fromChunks :: forall (m :: * -> *).
Monad m =>
Stream m (Array Word8) -> m ByteString
fromChunks = (ByteString -> ByteString -> ByteString)
-> ByteString -> Stream m ByteString -> m ByteString
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
Stream.foldr ByteString -> ByteString -> ByteString
chunk ByteString
Empty (Stream m ByteString -> m ByteString)
-> (Stream m (Array Word8) -> Stream m ByteString)
-> Stream m (Array Word8)
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array Word8 -> ByteString)
-> Stream m (Array Word8) -> Stream m ByteString
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array Word8 -> ByteString
Strict.fromArray
{-# INLINE fromChunksIO #-}
fromChunksIO :: Stream IO (Array Word8) -> IO ByteString
fromChunksIO :: Stream IO (Array Word8) -> IO ByteString
fromChunksIO =
(ByteString -> IO ByteString -> IO ByteString)
-> IO ByteString -> Stream IO ByteString -> IO ByteString
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
Stream.foldrM (\ByteString
x IO ByteString
b -> ByteString -> ByteString -> ByteString
chunk ByteString
x (ByteString -> ByteString) -> IO ByteString -> IO ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO ByteString -> IO ByteString
forall a. IO a -> IO a
unsafeInterleaveIO IO ByteString
b) (ByteString -> IO ByteString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
Empty)
(Stream IO ByteString -> IO ByteString)
-> (Stream IO (Array Word8) -> Stream IO ByteString)
-> Stream IO (Array Word8)
-> IO ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array Word8 -> ByteString)
-> Stream IO (Array Word8) -> Stream IO ByteString
forall a b. (a -> b) -> Stream IO a -> Stream IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array Word8 -> ByteString
Strict.fromArray
{-# DEPRECATED readChunks "Please use chunkReader instead." #-}
{-# INLINE readChunks #-}
readChunks :: Monad m => Unfold m ByteString (Array Word8)
readChunks :: forall (m :: * -> *). Monad m => Unfold m ByteString (Array Word8)
readChunks = Unfold m ByteString (Array Word8)
forall (m :: * -> *). Monad m => Unfold m ByteString (Array Word8)
chunkReader
{-# DEPRECATED read "Please use reader instead." #-}
{-# INLINE read #-}
read :: Monad m => Unfold m ByteString Word8
read :: forall (m :: * -> *). Monad m => Unfold m ByteString Word8
read = Unfold m ByteString Word8
forall (m :: * -> *). Monad m => Unfold m ByteString Word8
reader