Haskell ByteString / Data.Binary.Get 问题

发布于 2024-08-24 07:30:39 字数 531 浏览 5 评论 0原文

尝试使用 Data.Binary.Get 和 ByteString 但不明白发生了什么。我的代码如下:

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

对于返回元组的第三项,即有效负载,我收到以下错误:

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

有人请向我解释 Data.Binary.Get 和 ByteStrings 之间的交互以及我如何能够做我想要的事情。谢谢。

Attempting to use Data.Binary.Get and ByteString and not understanding what's 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 against the third item of the return tuple, ie 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'm intending. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2024-08-31 07:30:39

它表示您希望元组的第二个元素是 L.ByteString (我假设 L 来自 Data.ByteString.Lazy),但是 getByteString< /code> 例程从 Data.ByteString 返回严格的 ByteString。您可能想使用getLazyByteString

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

随心而道 2024-08-31 07:30:39

有两种 ByteString 数据类型:一种位于 Data.ByteString.Lazy 中,一种位于 Data.ByteString 中。

考虑到 L 限定了您的 ByteString,我认为您想要的是惰性类型,但是 getByteString 为您提供了严格的 ByteString

惰性ByteString在内部由严格ByteString列表表示。

幸运的是,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)

并且一切都应该与世界正确。

There are two ByteString data types: one is in Data.ByteString.Lazy and one is in Data.ByteString.

Given the L qualifying your ByteString, I presume you want the lazy variety, but getByteString is giving you a strict ByteString.

Lazy ByteStrings are internally represented by a list of strict ByteStrings.

Fortunately Data.ByteString.Lazy gives you a mechanism for turning a list of strict ByteStrings into a lazy ByteString.

If you define

import qualified Data.ByteString as S


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

you can change your code fragment to

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

and all should be right with the world.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文