Source

text / Data / Text / Foreign.hs

Diff from to

File Data/Text/Foreign.hs

     -- * Safe conversion functions
     , fromPtr
     , useAsPtr
+    , asForeignPtr
     -- * Unsafe conversion code
     , lengthWord16
     , unsafeCopyToPtr
 import Data.Word (Word16)
 import Foreign.Marshal.Alloc (allocaBytes)
 import Foreign.Ptr (Ptr, castPtr, plusPtr)
+import Foreign.ForeignPtr (ForeignPtr, mallocForeignPtrArray, withForeignPtr)
 import Foreign.Storable (peek, poke)
 
 -- $interop
     allocaBytes (len * 2) $ \buf -> do
       unsafeCopyToPtr t buf
       action (castPtr buf) (fromIntegral len)
+
+-- | /O(n)/ Make a mutable copy of a 'Text'.
+asForeignPtr :: Text -> IO (ForeignPtr Word16, I16)
+asForeignPtr t@(Text _arr _off len) = do
+  fp <- mallocForeignPtrArray len
+  withForeignPtr fp $ unsafeCopyToPtr t
+  return (fp, I16 len)