简体   繁体   English

在可存储和拆箱之间转换

[英]Converting between Storable and Unbox

Due to the answer to this question , I'd like to be able to construct instances of Foreign.Storable.Storable from instances of Data.Vector.Unboxed.Unbox (and vice versa). 由于回答这个问题 ,我希望能够构建的实例Foreign.Storable.Storable从实例Data.Vector.Unboxed.Unbox (反之亦然)。 Is this possible? 这可能吗?

The closest thing I'm aware of is vector-th-unbox , but this only creates an unboxable instance. 我知道的最接近的东西是vector-th-unbox ,但这只会创建一个不可装箱的实例。

Check the vector package. 检查vector包。

Use Data.Vector.convert :: (Vector va, Vector wa) => va -> wa . 使用Data.Vector.convert :: (Vector va, Vector wa) => va -> wa

I can imagine a terribly hacky way to go from unbox to storable, but it is genuinely hacky and inefficient! 我可以想像出从取消装箱到存储的一种极其骇人听闻的方法,但这确实是骇人听闻且效率低下的! In fact, having worked through it, I don't actually want to write it out. 实际上,经过研究,我实际上不想写出来。 The notion is that all you can do with an Unbox is work with it in an unboxed vector. 这个概念是您可以对Unbox进行的所有操作都可以在Unboxed向量中进行。 Therefore you can initialize an unboxed vector with a singleton element. 因此,您可以使用singleton元素初始化未装箱的矢量。 Given said element, you can create a vector holding it, then pry apart your Vector by evil, and use the bytes directly for your Storable instance. 给定所说的元素,您可以创建一个保存它的向量,然后用邪恶撬开向量,并将这些字节直接用于可存储实例。 You can equally evilly go the other way. 您同样可以邪恶地走另一条路。

But I don't know why you'd like to do this. 但是我不知道你为什么要这么做。 Most data types that are Storable by default are also Unbox and vice versa. 默认情况下,大多数可存储的数据类型也是Unbox,反之亦然。 And for those that they aren't, deriving Storable is easy enough -- either manually, or with a tool. 而对于那些并非如此的人来说,派生Storable非常容易-手动或使用工具。

正如vivian已经回答的那样,您可以使用Data.Vector.Generic模块中可用的convert函数: http : Data.Vector.Generic : Data.Vector.Generic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM