2

"Peeking", to read a pointer ptr to a new ByteString, is straightforward:

BS.packCStringLen (ptr, size)

But the poke operation (writing a ByteString into a given memory location) isn't that obvious. We have

useAsCStringLen :: ByteString -> (CStringLen -> IO a) -> IO a

so we could do (with the help of copyBytes.

BS.useAsCStringLen bs $ \(src, len) -> copyBytes ptr src len

or for better performance unsafeUseAsCStringLen.

Is there perhaps a ready-made function that does the above?

Petr
  • 62,528
  • 13
  • 153
  • 317
  • Wouldn't that break purity? Even if it's in IO, we can't usually change e.g. `x :: Int` to a different value... – chi Jun 24 '16 at 18:31
  • @chi We're copying into `ptr`, leaving ByteString `src` intact, so if `ptr` is correctly handled, there won't be purity violations. But now I see the title might be a bit misleading, I'll update it. – Petr Jun 24 '16 at 18:34
  • Ah, now I see. You want to modify what the `ptr` points to, not the ByteString. I misread. – chi Jun 24 '16 at 18:54

0 Answers0