[Haskell-cafe] Success and one last issue with Data.Binary

Thomas DuBuisson thomas.dubuisson at gmail.com
Tue Jun 2 13:24:51 EDT 2009

> I think getRemainingLazyByteString expects at least one byte
No, it works with an empty bytestring.  Or, my tests do with binary

The specific error means you are requiring more data than providing.
First check the length of the bytestring you pass in to the to level
decode (or 'get') routine and walk though that to figure out how much
it should be consuming.  I notice you have a guard on the
'getSpecific' function, hopefully you're sure the case you gave us is
the branch being taken.

I think the issue isn't with the code provided.  I cleaned up the code
(which did change behavior due to the guard and data declarations that
weren't in the mailling) and it works fine all the way down to the
expected minimum of 13 bytes.

> import Data.ByteString.Lazy
> import Data.Binary
> import Data.Binary.Get
> data RV =
> Rversion {     size 	:: Word32,
>                mtype 	:: Word8,
>                tag 	:: Word16,
>                msize 	:: Word32,
>                ssize 	:: Word16,
>                version :: ByteString}
>	deriving (Eq, Ord, Show)

> instance Binary RV where
>  get = do s <- getWord32le
>          mtype <- getWord8
>          getSpecific s mtype
>   where
>    getSpecific s mt = do t <- getWord16le
>                          ms <- getWord32le
>                          ss <- getWord16le
>                          v <- getRemainingLazyByteString
>                          return $ Rversion {size=s,
>                                             mtype=mt,
>                                             tag=t,
>                                             msize=ms,
>                                             ssize=ss,
>                                             version=v }
>  put _ = undefined

More information about the Haskell-Cafe mailing list