Execute a
Put and return the computed result and the bytes
written during the computation as a
LazyByteString.
This function is strict in the computed result and lazy in the writing
of the bytes. For example, given
infinitePut = sequence_ (repeat (putBuilder (word8 1))) >> return 0
evaluating the expression
fst $ putToLazyByteString infinitePut
does not terminate, while evaluating the expression
L.head $ snd $ putToLazyByteString infinitePut
does terminate and yields the value
1 :: Word8.
An illustrative example for these strictness properties is the
implementation of Base64 decoding
(
http://en.wikipedia.org/wiki/Base64).
type DecodingState = ...
decodeBase64 :: StrictByteString -> DecodingState -> Put (Maybe DecodingState)
decodeBase64 = ...
The above function takes a
StrictByteString supposed to
represent Base64 encoded data and the current decoding state. It
writes the decoded bytes as the side-effect of the
Put and
returns the new decoding state, if the decoding of all data in the
StrictByteString was successful. The checking if the
StrictByteString represents Base64 encoded data and the actual
decoding are fused. This makes the common case, where all data
represents Base64 encoded data, more efficient. It also implies that
all data must be decoded before the final decoding state can be
returned.
Puts are intended for implementing such fused
checking and decoding/encoding, which is reflected in their strictness
properties.