Skip to content

Instantly share code, notes, and snippets.

@chessai
Created January 18, 2019 22:30
Show Gist options
  • Save chessai/d44e7f167ec6ef5309e431cda83f4b6c to your computer and use it in GitHub Desktop.
Save chessai/d44e7f167ec6ef5309e431cda83f4b6c to your computer and use it in GitHub Desktop.
convert a (pinned) bytearray to a bytestring without copying
byteArrayToByteString :: ByteArray -> ByteString
byteArrayToByteString (ByteArray b#) =
let contents# = byteArrayContents# b#
fp = ForeignPtr contents# (PlainPtr (byteArrayToMutableByteArray b#))
len = lenAddr contents#
in PS fp 0 len
byteArrayToMutableByteArray :: ByteArray# -> MutableByteArray# RealWorld
byteArrayToMutableByteArray = unsafeCoerce#
lenAddr :: Addr# -> Int
lenAddr addr# = fromIntegral . accursedUnutterablePerformIO $ c_strlen (Ptr addr#)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment