binary icon indicating copy to clipboard operation
binary copied to clipboard

A size counter for Get

Open copumpkin opened this issue 11 years ago • 1 comments

I've often wanted a combinator called sized :: Get a -> Get (Int, a) that will behave the same as its input, except will also tell you how many bytes it consumed.

copumpkin avatar Mar 16 '14 02:03 copumpkin

Now that I think about it, having something similar for Put would also be nice. The use case is a fairly common one in existing binary formats: you have a discriminated/tagged union that's extensible, and implementations aren't required to understand all the tags. The typical way this is accomplished is by having a leading tag, then the size, then the constructor-specific data. I'm currently accomplishing this by pre-writing to aByteString, figuring out its size, and then writing the size and the ByteString, but that feels kind of hacky. It'd also be a natural counterpart to the sized combinator I created this ticket for. Anyone have thoughts on how to do this nicely? It also seems related to #12.

Edit: thinking about it some more, this could rekindle the distinction between Builder (which I think will eventually just be re-exported from ByteString, right?) and Put. Put would become more of a specialized State monad that knows how much has been written and can deal with alignment and write sizes cleanly. Builder would then be the major building block for that.

copumpkin avatar Mar 16 '14 16:03 copumpkin