Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ binary-x.x.x.x
TODO: fix since annotations

- Add `Data.Binary.Get.getShortByteString`
- Add `Data.Binary.Put.runPutMBuilder`
- Don't reexport `Data.Word` from `Data.Binary`
- Add `Binary (Proxy a)` instance
- Add binary instance for `GHC.Generics.Generically`. `Binary T` can
Expand Down
10 changes: 10 additions & 0 deletions src/Data/Binary/Put.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Data.Binary.Put (
, PutM(..)
, runPut
, runPutM
, runPutMBuilder
, putBuilder
, execPut

Expand Down Expand Up @@ -194,6 +195,15 @@ runPutM :: PutM a -> (a, L.ByteString)
runPutM (Put (PairS f s)) = (f, toLazyByteString s)
{-# INLINE runPutM #-}

-- | Run the 'Put' monad and get its result together with the 'Builder' it
-- constructed. Unlike 'runPutM', the 'Builder' is not executed, so it can
-- still be combined with other 'Builder's.
--
-- @since x.x.x.x
runPutMBuilder :: PutM a -> (a, Builder)
runPutMBuilder (Put (PairS f s)) = (f, s)
{-# INLINE runPutMBuilder #-}

------------------------------------------------------------------------

-- | Pop the ByteString we have constructed so far, if any, yielding a
Expand Down
23 changes: 23 additions & 0 deletions tests/QC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,28 @@ prop_putM_monad_and_flush w8 w16 w32 =
, lbs === lbsExec
]

prop_runPutMBuilder :: Word8 -> Word16 -> Word32 -> Property
prop_runPutMBuilder w8 w16 w32 =
let pm :: PutM (Word8, Word16)
pm = do
putWord8 w8
flush
putWord16be w16
putWord32le w32
return (w8, w16)
(res, builder) = runPutMBuilder pm
lbs = Builder.toLazyByteString builder
infPut :: Put
infPut = putWord8 w8 >> infPut
in conjoin
[ res === (w8, w16)
, (res, lbs) === runPutM pm
, lbs === runPut (putWord8 w8 >> putWord16be w16 >> putWord32le w32)
, Builder.toLazyByteString (builder <> builder) === L.append lbs lbs
-- an infinite PutM still streams through runPutMBuilder
, L.take 16 (Builder.toLazyByteString (snd (runPutMBuilder infPut))) === L.replicate 16 w8
]

prop_put_infinite_streaming :: Word8 -> Property
prop_put_infinite_streaming w =
forAll (choose (0, 64)) $ \n ->
Expand Down Expand Up @@ -1532,6 +1554,7 @@ tests =

, testGroup "Put"
[ testProperty "PutM Functor/Applicative/Monad/flush/execPut" (p prop_putM_monad_and_flush)
, testProperty "runPutMBuilder" (p prop_runPutMBuilder)
, testProperty "Put infinite streaming" (p prop_put_infinite_streaming)
, testProperty "tryReadN negative n falls back to slow" (p prop_tryReadN_negative)
, testProperty "big Integer invalid sign fails before reading payload" (p prop_big_integer_invalid_sign_fails_fast)
Expand Down