Skip to content

Add runPutMBuilder - #249

Open
NopeCharon wants to merge 2 commits into
haskell:masterfrom
NopeCharon:runPutMBuilder
Open

NopeCharon wants to merge 2 commits into
haskell:masterfrom
NopeCharon:runPutMBuilder

Conversation

@NopeCharon

@NopeCharon NopeCharon commented Dec 5, 2026 •

Copy link
Copy Markdown

Closes #151.

Adds

runPutMBuilder :: PutM a -> (a, Builder)

to Data.Binary.Put. It returns the result of a PutM computation together with the Builder it constructed, without running the Builder (unlike runPutM), so the computation can be embedded in a larger Builder-based writer while still using its result. Previously there was no single function for this, since PairS isn't exported.

I went with cereal's name, as mentioned in the issue. I'm happy to rename it if you prefer something else.

Tests: a QuickCheck property checking that the result and bytes match runPutM, that the returned Builder composes, and that an infinite PutM still streams through runPutMBuilder.

There was no way to run a PutM computation and get both its result and
the Builder it constructed in a single call: runPutM executes the
Builder into a lazy ByteString, execPut discards the result, and the
PairS type that holds both is not exported. This makes it awkward to
embed PutM computations into larger Builder-based writers while still
using their results.

Add

    runPutMBuilder :: PutM a -> (a, Builder)

which returns the result together with the unexecuted Builder, so it
can be combined with other Builders. The name matches the equivalent
function in cereal, as suggested in the issue.

Add a QuickCheck property checking that the result and the bytes agree
with runPutM, that the Builder composes, and that the Builder of an
infinite PutM is returned without being run.

Closes haskell#151
Comment thread tests/QC.hs Outdated
Comment on lines +945 to +946
-- the Builder is returned without being run
, L.take 16 (Builder.toLazyByteString (snd (runPutMBuilder infPut))) === L.replicate 16 w8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this check that the Builder is returned without being run?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, really. It only shows that an infinite PutM still streams through runPutMBuilder, which running the Builder lazily would pass too, so I reworded the comment in da58d7b to say just that.

The comment claimed the assertion shows the Builder is returned without
being run, but taking a prefix of an infinite PutM only shows that
runPutMBuilder does not force the Builder, so the output still streams.
Running the Builder lazily and re-wrapping the bytes would pass as well.
Reword the comment to describe what is actually checked.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I can't just get the answer and builder out of Put in a single call.

2 participants