|
Packit |
4b2029 |
-- | Convert a stream of blaze-builder @Builder@s into a stream of @ByteString@s.
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- Adapted from blaze-builder-enumerator, written by myself and Simon Meier.
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- Note that the functions here can work in any monad built on top of @IO@ or
|
|
Packit |
4b2029 |
-- @ST@.
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- Since 1.1.7.0, the functions here call their counterparts in
|
|
Packit |
4b2029 |
-- "Data.Conduit.ByteString.Builder", which work with both
|
|
Packit |
4b2029 |
-- 'Data.ByteString.Builder.Builder' and blaze-builder 0.3's
|
|
Packit |
4b2029 |
-- 'Blaze.ByteString.Builder.Builder'.
|
|
Packit |
4b2029 |
module Data.Conduit.Blaze
|
|
Packit |
4b2029 |
(
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- * Conduits from builders to bytestrings
|
|
Packit |
4b2029 |
builderToByteString
|
|
Packit |
4b2029 |
, unsafeBuilderToByteString
|
|
Packit |
4b2029 |
, builderToByteStringWith
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- ** Flush
|
|
Packit |
4b2029 |
, builderToByteStringFlush
|
|
Packit |
4b2029 |
, builderToByteStringWithFlush
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- * Buffers
|
|
Packit |
4b2029 |
, B.Buffer
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- ** Status information
|
|
Packit |
4b2029 |
, B.freeSize
|
|
Packit |
4b2029 |
, B.sliceSize
|
|
Packit |
4b2029 |
, B.bufferSize
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- ** Creation and modification
|
|
Packit |
4b2029 |
, B.allocBuffer
|
|
Packit |
4b2029 |
, B.reuseBuffer
|
|
Packit |
4b2029 |
, B.nextSlice
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- ** Conversion to bytestings
|
|
Packit |
4b2029 |
, B.unsafeFreezeBuffer
|
|
Packit |
4b2029 |
, B.unsafeFreezeNonEmptyBuffer
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- * Buffer allocation strategies
|
|
Packit |
4b2029 |
, B.BufferAllocStrategy
|
|
Packit |
4b2029 |
, B.allNewBuffersStrategy
|
|
Packit |
4b2029 |
, B.reuseBufferStrategy
|
|
Packit |
4b2029 |
) where
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
import Data.Conduit
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
import qualified Data.ByteString as S
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
import Blaze.ByteString.Builder (Builder)
|
|
Packit |
4b2029 |
import Control.Monad.Primitive (PrimMonad)
|
|
Packit |
4b2029 |
import Control.Monad.Base (MonadBase)
|
|
Packit |
4b2029 |
import Data.Streaming.Blaze
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
import qualified Data.Conduit.ByteString.Builder as B
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- | Incrementally execute builders and pass on the filled chunks as
|
|
Packit |
4b2029 |
-- bytestrings.
|
|
Packit |
4b2029 |
builderToByteString :: (MonadBase base m, PrimMonad base) => Conduit Builder m S.ByteString
|
|
Packit |
4b2029 |
builderToByteString = B.builderToByteString
|
|
Packit |
4b2029 |
{-# INLINE builderToByteString #-}
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- |
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- Since 0.0.2
|
|
Packit |
4b2029 |
builderToByteStringFlush :: (MonadBase base m, PrimMonad base) => Conduit (Flush Builder) m (Flush S.ByteString)
|
|
Packit |
4b2029 |
builderToByteStringFlush = B.builderToByteStringFlush
|
|
Packit |
4b2029 |
{-# INLINE builderToByteStringFlush #-}
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- | Incrementally execute builders on the given buffer and pass on the filled
|
|
Packit |
4b2029 |
-- chunks as bytestrings. Note that, if the given buffer is too small for the
|
|
Packit |
4b2029 |
-- execution of a build step, a larger one will be allocated.
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- WARNING: This conduit yields bytestrings that are NOT
|
|
Packit |
4b2029 |
-- referentially transparent. Their content will be overwritten as soon
|
|
Packit |
4b2029 |
-- as control is returned from the inner sink!
|
|
Packit |
4b2029 |
unsafeBuilderToByteString :: (MonadBase base m, PrimMonad base)
|
|
Packit |
4b2029 |
=> IO Buffer -- action yielding the inital buffer.
|
|
Packit |
4b2029 |
-> Conduit Builder m S.ByteString
|
|
Packit |
4b2029 |
unsafeBuilderToByteString = B.unsafeBuilderToByteString
|
|
Packit |
4b2029 |
{-# INLINE unsafeBuilderToByteString #-}
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- | A conduit that incrementally executes builders and passes on the
|
|
Packit |
4b2029 |
-- filled chunks as bytestrings to an inner sink.
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- INV: All bytestrings passed to the inner sink are non-empty.
|
|
Packit |
4b2029 |
builderToByteStringWith :: (MonadBase base m, PrimMonad base)
|
|
Packit |
4b2029 |
=> BufferAllocStrategy
|
|
Packit |
4b2029 |
-> Conduit Builder m S.ByteString
|
|
Packit |
4b2029 |
builderToByteStringWith = B.builderToByteStringWith
|
|
Packit |
4b2029 |
{-# INLINE builderToByteStringWith #-}
|
|
Packit |
4b2029 |
|
|
Packit |
4b2029 |
-- |
|
|
Packit |
4b2029 |
--
|
|
Packit |
4b2029 |
-- Since 0.0.2
|
|
Packit |
4b2029 |
builderToByteStringWithFlush
|
|
Packit |
4b2029 |
:: (MonadBase base m, PrimMonad base)
|
|
Packit |
4b2029 |
=> BufferAllocStrategy
|
|
Packit |
4b2029 |
-> Conduit (Flush Builder) m (Flush S.ByteString)
|
|
Packit |
4b2029 |
builderToByteStringWithFlush = B.builderToByteStringWithFlush
|
|
Packit |
4b2029 |
{-# INLINE builderToByteStringWithFlush #-}
|