iteratee ByteStringLike conversions

Conrad Parker conrad at metadecks.org
Fri Nov 11 04:29:42 GMT 2011


Hi,

I recently converted zoom-cache to use (I.Nullable, LL.ListLike s
Word8) constraints for byte streams. I then added support for
compressed data blocks, using enumInflate from iteratee-compress:

enumInflate :: MonadIO m => Format -> DecompressParams -> Enumerator
ByteString m a

In order to use that unmodified, I wanted a function of type (Iteratee
ByteString m a -> Iteratee s m a), and ended up writing a simple
typeclass for ByteStringLike streams:


class (I.Nullable s, LL.ListLike s Word8) => ByteStringLike s where
    likeFromByteString :: ByteString -> s
    likeToByteString   :: s -> ByteString

instance ByteStringLike [Word8] where
    likeFromByteString = B.unpack
    likeToByteString   = B.pack

instance ByteStringLike ByteString where
    likeFromByteString = id
    likeToByteString   = id


allowing the desired conversion function:


iterToByteStringLike :: (I.Nullable s, LL.ListLike s Word8,
ByteStringLike s, Monad m)
                     => Iteratee ByteString m a -> Iteratee s m a
iterToByteStringLike = I.joinI . I.mapChunks likeToByteString


This all seems a bit circuitous.

1) Does a similar typeclass already exist?

2) Is there a simpler way to use enumInflate from an arbitrary byte stream?

3) It would be nice to just generalize the type of enumInflate, but it
binds to the zlib C library and expects byte buffers, hence the direct
ByteString interface; it would have to do something similar
internally. Would these to/from iteratee conversion functions be
generally useful for the Iteratee library?

Code for Data.Iteratee.ByteStringLike is here:
https://github.com/kfish/zoom-cache/blob/zlib/Data/Iteratee/ByteStringLike.hs

cheers,

Conrad.



More information about the Iteratee mailing list