[Haskell-cafe] Backtracking when building a bytestring

David Turner dct25-561bs at mythic-beasts.com
Fri Jun 24 09:23:02 UTC 2016


I'm working on a network protocol that involves sending frames of data
prefixed by their length and a checksum. The only realistic way to work out
the length of a frame is to actually write the bytes out, although the
length and checksum take up a fixed number of bytes.

If I were working in C I'd be filling a buffer, leaving space for the
length/checksum, and then go back and fill them in at the end. So this is
_almost_ what ByteString.Builder does except for the backtracking bit.

Does anyone know if there's an implementation of a thing that's a bit like
ByteString.Builder but also allows for this kind of backtracking? Ideally I
want to be able to batch up a number of frames into a single buffer, and
deal gracefully with overflowing the buffer by allocating some more, much
as Builder does.

I can't think of a terribly good way of doing this using the existing
Builder implementation as it stands, although it looks quite easy to modify
it to add this functionality so I might just do that locally if needs be.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160624/bdef81d2/attachment.html>

More information about the Haskell-Cafe mailing list