module Data.Conduit.ByteString.Builder
(
builderToByteString
, unsafeBuilderToByteString
, builderToByteStringWith
, builderToByteStringFlush
, builderToByteStringWithFlush
, Buffer
, freeSize
, sliceSize
, bufferSize
, allocBuffer
, reuseBuffer
, nextSlice
, unsafeFreezeBuffer
, unsafeFreezeNonEmptyBuffer
, BufferAllocStrategy
, allNewBuffersStrategy
, reuseBufferStrategy
) where
import Data.Conduit
import Control.Monad (unless, liftM)
import Control.Monad.Trans.Class (lift, MonadTrans)
import qualified Data.ByteString as S
import Control.Monad.Primitive (PrimMonad, unsafePrimToPrim)
import Control.Monad.Base (MonadBase, liftBase)
import Data.Streaming.ByteString.Builder.Class
unsafeLiftIO :: (MonadBase base m, PrimMonad base) => IO a -> m a
unsafeLiftIO = liftBase . unsafePrimToPrim
builderToByteString :: (MonadBase base m, PrimMonad base, StreamingBuilder b)
=> Conduit b m S.ByteString
builderToByteString =
builderToByteStringWith defaultStrategy
builderToByteStringFlush :: (MonadBase base m, PrimMonad base, StreamingBuilder b)
=> Conduit (Flush b) m (Flush S.ByteString)
builderToByteStringFlush =
builderToByteStringWithFlush defaultStrategy
unsafeBuilderToByteString :: (MonadBase base m, PrimMonad base, StreamingBuilder b)
=> IO Buffer
-> Conduit b m S.ByteString
unsafeBuilderToByteString = builderToByteStringWith . reuseBufferStrategy
builderToByteStringWith :: (MonadBase base m, PrimMonad base, StreamingBuilder b)
=> BufferAllocStrategy
-> Conduit b m S.ByteString
builderToByteStringWith =
helper (liftM (fmap Chunk) await) yield'
where
yield' Flush = return ()
yield' (Chunk bs) = yield bs
builderToByteStringWithFlush
:: (MonadBase base m, PrimMonad base, StreamingBuilder b)
=> BufferAllocStrategy
-> Conduit (Flush b) m (Flush S.ByteString)
builderToByteStringWithFlush = helper await yield
helper :: (MonadBase base m, PrimMonad base, Monad (t m), MonadTrans t, StreamingBuilder b)
=> t m (Maybe (Flush b))
-> (Flush S.ByteString -> t m ())
-> BufferAllocStrategy
-> t m ()
helper await' yield' strat = do
(recv, finish) <- lift $ unsafeLiftIO $ newBuilderRecv strat
let loop = await' >>= maybe finish' cont
finish' = do
mbs <- lift $ unsafeLiftIO finish
maybe (return ()) (yield' . Chunk) mbs
cont fbuilder = do
let builder =
case fbuilder of
Flush -> builderFlush
Chunk b -> b
popper <- lift $ unsafeLiftIO $ recv builder
let cont' = do
bs <- lift $ unsafeLiftIO popper
unless (S.null bs) $ do
yield' (Chunk bs)
cont'
cont'
case fbuilder of
Flush -> yield' Flush
Chunk _ -> return ()
loop
loop