diff options
author | Benjamin Barenblat <bbaren@google.com> | 2018-08-31 17:14:38 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2018-08-31 17:14:38 -0400 |
commit | b1639dda870f22a78136b26295f98585e503fa98 (patch) | |
tree | ff69c9cbf0e4bdcc0e98e005227bf955d30b1682 /src/BTLS/Buffer.hs | |
parent | 80f2fb2e3be2c4074fbfb0adbc47746d2d88813b (diff) |
Marshal `ByteString` input arguments to `fun`s
Diffstat (limited to 'src/BTLS/Buffer.hs')
-rw-r--r-- | src/BTLS/Buffer.hs | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/BTLS/Buffer.hs b/src/BTLS/Buffer.hs index d7b3f14..186054f 100644 --- a/src/BTLS/Buffer.hs +++ b/src/BTLS/Buffer.hs @@ -13,29 +13,25 @@ -- the License. module BTLS.Buffer - ( unsafeUseAsCUStringLen + ( unsafeUseAsCBuffer , packCUStringLen ) where import Data.ByteString (ByteString) import qualified Data.ByteString as ByteString import qualified Data.ByteString.Unsafe as ByteString -import Foreign (Ptr) +import Foreign (Ptr, castPtr) import Foreign.C.Types import Unsafe.Coerce (unsafeCoerce) -unsafeUseAsCUStringLen :: - Integral n => ByteString -> ((Ptr CUChar, n) -> IO a) -> IO a -unsafeUseAsCUStringLen bs f = +unsafeUseAsCBuffer :: ByteString -> ((Ptr a, CULong) -> IO b) -> IO b +unsafeUseAsCBuffer bs f = ByteString.unsafeUseAsCStringLen bs $ \(pStr, len) -> - f (asCUCharBuf pStr, fromIntegral len) + f (castPtr pStr, fromIntegral len) packCUStringLen :: Integral n => (Ptr CUChar, n) -> IO ByteString packCUStringLen (pStr, len) = ByteString.packCStringLen (asCCharBuf pStr, fromIntegral len) -asCUCharBuf :: Ptr CChar -> Ptr CUChar -asCUCharBuf = unsafeCoerce - asCCharBuf :: Ptr CUChar -> Ptr CChar asCCharBuf = unsafeCoerce |