Haskell ByteString / Data.Binary. Get a Question

Trying to use Data.Binary.Get and ByteString and not understand what is happening. My code is below:

getSegmentParams :: Get (Int, L.ByteString)
getSegmentParams = do 
    seglen <- liftM fromIntegral getWord16be
    params <- getByteString (seglen - 2)
    return (seglen, params)

I get the following error regarding the third element of the returned tuple, i.e. the payload:

Couldn't match expected type `L.ByteString'
       against inferred type `bytestring-0.9.1.4:Data.ByteString.Internal.ByteString'

Someone please explain to me the interaction between Data.Binary.Get and ByteStrings and how I can do what I intend. Thank.

+3
source share
2 answers

It says that you expect the second element of the tuple to be L.ByteString(I assume L is from Data.ByteString.Lazy), but the procedure getByteStringreturns a strict ByteString from Data.ByteString. You probably want to use getLazyByteString.

+5
source

ByteString: Data.ByteString.Lazy, - Data.ByteString.

, L ByteString, , , getByteString ByteString.

Lazy ByteString ByteString s.

, Data.ByteString.Lazy ByteString ByteString.

import qualified Data.ByteString as S


strictToLazy :: S.ByteString -> L.ByteString
strictToLazy = L.fromChunks . return 

getSegmentParams :: Get (Int, L.ByteString)
getSegmentParams = do 
    seglen <- liftM fromIntegral getWord16be
    params <- getByteString (seglen - 2)
    return (seglen, strictToLazy params)

.

+1

Source: https://habr.com/ru/post/1736219/


All Articles