4
votes

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). Is this possible?

The closest thing I'm aware of is vector-th-unbox, but this only creates an unboxable instance.

3

3 Answers

4
votes

Check the vector package.

Use Data.Vector.convert :: (Vector v a, Vector w a) => v a -> w a.

1
votes

As vivian has already answered you can use the convert function that is available in the Data.Vector.Generic module: http://hackage.haskell.org/package/vector/docs/Data-Vector-Generic.html#v:convert

0
votes

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. Therefore you can initialize an unboxed vector with a singleton element. 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. And for those that they aren't, deriving Storable is easy enough -- either manually, or with a tool.