Created
January 18, 2019 22:30
-
-
Save chessai/d44e7f167ec6ef5309e431cda83f4b6c to your computer and use it in GitHub Desktop.
convert a (pinned) bytearray to a bytestring without copying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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