[Haskell-cafe] Backtracking when building a bytestring

Yuras Shumovich shumovichy at gmail.com
Fri Jun 24 10:41:26 UTC 2016


I think it is possible (and easy) to implement with
Data.ByteString.Builder. Just use the low-level interface: http://hacka
ge.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString-
Builder-Extra.html#v:runBuilder

BufferWriter takes a buffer to write to and returns a continuation to
be called in case the buffer is too small. The idea is to allocate a
buffer, but reserve necessary space for length and checksum. Then run
the builder, get the resulting ByteString (or work directly with `Ptr
Word8`), calculate length and check sum and write then into the
reserved space.

Just be careful when using the low level API, and read the docs
carefully.

Thanks,
Yuras.

On Fri, 2016-06-24 at 10:23 +0100, David Turner wrote:
> Hi,
> 
> 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.
> 
> Cheers,
> 
> David
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list