Skip to content

Instantly share code, notes, and snippets.

@jchia
Last active May 30, 2018 01:28
Show Gist options
  • Save jchia/40ee37ed760cca87babed902950051ed to your computer and use it in GitHub Desktop.
Save jchia/40ee37ed760cca87babed902950051ed to your computer and use it in GitHub Desktop.
Copy from UArray to ByteString
import import Data.Array.Base
import Data.ByteString
import GHC.Prim
type Chunk = UArray Int Word8
-- Is there anything preventing byteArray from being relocated by the garbage collector after it's read but before it's used packCStringLen, so that packCStringLen uses an old, invalid, address?
chunkToBs :: Chunk -> ByteString
chunkToBs (UArray _ _ n byteArray) = unsafePerformIO $
packCStringLen (Ptr (byteArrayContents# byteArray), n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment