blanu / Dust

A Polymorphic Engine for Filtering-Resistant Transport Protocols
286 stars 31 forks source link

Switch binary package #3

Closed singpolyma closed 11 years ago

singpolyma commented 11 years ago

This package currently uses binary-strict, an older package that simply cloned binary because the error handling in binary was abysmal at the time. The binary package has improved considerably, and I would recommend moving to it.

The cereal package is also another (more actively maintained) option, but I believe things will be moving back to binary in general, now that the package is so much better.

blanu commented 11 years ago

I use binary-strict for the Data.Binary.Strict.BitGet and BitPut monads for parsing bit-aligned data. The binary and cereal packages seems to only support byte-aligned data. Parsing of bit-aligned data is required for Huffman encoding.

blanu commented 11 years ago

Strangely, there is a Data.Binary.BitPut, but no Data.Binary.BitGet?

singpolyma commented 11 years ago

Looking at the code, it seems the whole package dependency is only used for implementing bitpack and bitunpack, which are more succinctly (and without any dependency) written as:

bitpack :: [Bool] -> B.ByteString
bitpack = B.pack . map packByte . takeWhile (not . null) . unfoldr (Just . splitAt 8)
    where
    packByte = foldl' (\i b -> (i `shiftL` 1) .|. (fromIntegral $ fromEnum b)) 0

bitunpack :: S.ByteString -> Either String [Bool]
bitunpack = Right . concatMap (\byte -> map (testBit byte) [7,6..0]) . S.unpack

I can prepare a pull request for these versions (and dropping the binary-strict) dependency, if this meets with your approval.

You will also note that bitunpack never fails, so the type signature could be changed to reflect that.

singpolyma commented 11 years ago

The bitpack dependency also happens to be used in Dust.Model.Packet, but that can be easily replaced with calls to cereal (which is already a dependency anyway). I would include such a change in any pull request.

blanu commented 11 years ago

Sounds good!

blanu commented 11 years ago

Done!