blaze-builder-conduit 0.0.1 → 0.2.0
raw patch · 3 files changed
+60/−9 lines, 3 filesdep ~conduitPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: conduit
API changes (from Hackage documentation)
+ Data.Conduit.Blaze: builderToByteStringFlush :: ResourceUnsafeIO m => Conduit (Flush Builder) m (Flush ByteString)
+ Data.Conduit.Blaze: builderToByteStringWithFlush :: ResourceUnsafeIO m => BufferAllocStrategy -> Conduit (Flush Builder) m (Flush ByteString)
Files
- Data/Conduit/Blaze.hs +50/−6
- blaze-builder-conduit.cabal +2/−2
- test/main.hs +8/−1
Data/Conduit/Blaze.hs view
@@ -36,6 +36,9 @@ , unsafeBuilderToByteString , builderToByteStringWith + -- ** Flush+ , builderToByteStringFlush+ , builderToByteStringWithFlush ) where import Data.Conduit hiding (SinkResult (Done))@@ -54,6 +57,13 @@ builderToByteString = builderToByteStringWith (allNewBuffersStrategy defaultBufferSize) +-- |+--+-- Since 0.0.2+builderToByteStringFlush :: ResourceUnsafeIO m => Conduit (Flush Builder) m (Flush S.ByteString)+builderToByteStringFlush =+ builderToByteStringWithFlush (allNewBuffersStrategy defaultBufferSize)+ -- | Incrementally execute builders on the given buffer and pass on the filled -- chunks as bytestrings. Note that, if the given buffer is too small for the -- execution of a build step, a larger one will be allocated.@@ -76,18 +86,52 @@ -> Conduit Builder m S.ByteString builderToByteStringWith (ioBuf0, nextBuf) = conduitState ioBuf0- push+ (push nextBuf) close where- finalStep !(BufRange pf _) = return $ Done pf ()- close ioBuf = lift $ unsafeFromIO $ do buf <- ioBuf return $ maybe [] return $ unsafeFreezeNonEmptyBuffer buf - push ioBuf x = lift $ unsafeFromIO $ do- (ioBuf', front) <- go (unBuilder x (buildStep finalStep)) ioBuf id- return (ioBuf', Producing $ front [])+-- |+--+-- Since 0.0.2+builderToByteStringWithFlush+ :: ResourceUnsafeIO m+ => BufferAllocStrategy+ -> Conduit (Flush Builder) m (Flush S.ByteString)+builderToByteStringWithFlush (ioBuf0, nextBuf) = conduitState+ ioBuf0+ push'+ close+ where+ close ioBuf = lift $ unsafeFromIO $ do+ buf <- ioBuf+ return $ maybe [] (return . Chunk) $ unsafeFreezeNonEmptyBuffer buf++ push' :: ResourceUnsafeIO m+ => IO Buffer+ -> Flush Builder+ -> ResourceT m (ConduitStateResult (IO Buffer) input (Flush S.ByteString))+ push' ioBuf Flush = do+ StateProducing ioBuf' chunks <- push nextBuf ioBuf flush+ let myFold bs rest+ | S.null bs = rest+ | otherwise = Chunk bs : rest+ chunks' = foldr myFold [Flush] chunks+ return $ StateProducing ioBuf' chunks'+ push' ioBuf (Chunk builder) = (fmap . fmap) Chunk (push nextBuf ioBuf builder)++push :: ResourceUnsafeIO m+ => (Int -> Buffer -> IO (IO Buffer))+ -> IO Buffer+ -> Builder+ -> ResourceT m (ConduitStateResult (IO Buffer) input S.ByteString)+push nextBuf ioBuf0 x = lift $ unsafeFromIO $ do+ (ioBuf', front) <- go (unBuilder x (buildStep finalStep)) ioBuf0 id+ return $ StateProducing ioBuf' $ front []+ where+ finalStep !(BufRange pf _) = return $ Done pf () go bStep ioBuf front = do !buf <- ioBuf
blaze-builder-conduit.cabal view
@@ -1,5 +1,5 @@ Name: blaze-builder-conduit-Version: 0.0.1+Version: 0.2.0 Synopsis: Convert streams of builders to streams of bytestrings. Description: Convert streams of builders to streams of bytestrings. License: BSD3@@ -20,7 +20,7 @@ , bytestring >= 0.9 , text >= 0.11 , blaze-builder >= 0.2.1.4 && < 0.4- , conduit+ , conduit >= 0.2 ghc-options: -Wall test-suite test
test/main.hs view
@@ -8,7 +8,7 @@ import qualified Data.Conduit as C import qualified Data.Conduit.List as CL import Data.ByteString.Char8 ()-import Data.Conduit.Blaze (builderToByteString)+import Data.Conduit.Blaze (builderToByteString, builderToByteStringFlush) import Data.Conduit (runResourceT) import Control.Monad.ST (runST) import Data.Monoid@@ -42,3 +42,10 @@ let src = mconcat $ map (CL.sourceList . return) builders outBss <- src C.$= builderToByteString C.$$ CL.consume :: C.ResourceT IO [S.ByteString] liftIO $ lbs @=? L.fromChunks outBss++ prop "flushing" $ \bss' -> runST $ runResourceT $ do+ let bss = concatMap (\bs -> [C.Chunk $ S.pack bs, C.Flush]) $ filter (not . null) bss'+ let src = CL.sourceList $ map (fmap fromByteString) bss+ outBss <- src C.$= builderToByteStringFlush C.$$ CL.consume+ if bss == outBss then return () else error (show (bss, outBss))+ return $ bss == outBss