Open treeowl opened 6 years ago
sSizeOf# _
| I# s <- FS.sizeOf (undefined :: a)
= s
sAlignment# _
| I# al <- FS.alignment (undefined :: a)
= al
sReadOffAddr# addr# i# = internal $ do
PrimStorable x <- unsafeIOToST $ FS.peekElemOff (Ptr addr#) (I# i#)
pure x
sWriteOffAddr# addr# i# a = internal_ $ unsafeIOToST $
FS.pokeElemOff (Ptr addr#) (I# i#) (PrimStorable a)
sIndexOffAddr# addr# i# = unsafeDupablePerformIO $ do
PrimStorable x <- FS.peekElemOff (Ptr addr#) (I# i#)
pure x
We have
PrimStorable
to deriveStorable
instances fromPrim
instances. It would be very nice to be able to offer aStorablePrim
to go the other way. Unfortunately, I don't think it's quite possible to do this.The tricky parts are the
ByteArray#
andMutableByteArray#
operations. We can almost usebyteArrayContents# :: ByteArray# -> Addr#
, but that only works reliably if theByteArray#
is pinned. The trouble is that the garbage collector could run between the time we get the address of the start of the array and the time the appropriateStorable
method uses it. I'm pretty sure the only entirely reliable way to implement these operations would be to copy the entire array, which is just awful.On the other hand, we can offer adapter functions for all the other
Prim
methods, so I think we should add them.