{-# LANGUAGE ImportQualifiedPost #-}
module Sel.Internal.Scoped.Foreign where
import Control.Monad.Trans.Class (lift)
import Data.ByteString (StrictByteString)
import Data.ByteString.Unsafe qualified as ByteString
import Foreign (ForeignPtr, Ptr, Storable)
import Foreign qualified
import Foreign.C (CString, CStringLen)
import Sel.Internal.Scoped
foreignPtr :: ForeignPtr a -> Scoped IO (Ptr a)
foreignPtr :: forall a. ForeignPtr a -> Scoped IO (Ptr a)
foreignPtr ForeignPtr a
fptr = (forall b. (Ptr a -> IO b) -> IO b) -> Scoped IO (Ptr a)
forall {k} (m :: k -> *) a.
(forall (b :: k). (a -> m b) -> m b) -> Scoped m a
Scoped ((forall b. (Ptr a -> IO b) -> IO b) -> Scoped IO (Ptr a))
-> (forall b. (Ptr a -> IO b) -> IO b) -> Scoped IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> (Ptr a -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
Foreign.withForeignPtr ForeignPtr a
fptr
unsafeCStringLen :: StrictByteString -> Scoped IO CStringLen
unsafeCStringLen :: StrictByteString -> Scoped IO CStringLen
unsafeCStringLen StrictByteString
bs = (forall b. (CStringLen -> IO b) -> IO b) -> Scoped IO CStringLen
forall {k} (m :: k -> *) a.
(forall (b :: k). (a -> m b) -> m b) -> Scoped m a
Scoped ((forall b. (CStringLen -> IO b) -> IO b) -> Scoped IO CStringLen)
-> (forall b. (CStringLen -> IO b) -> IO b) -> Scoped IO CStringLen
forall a b. (a -> b) -> a -> b
$ StrictByteString -> (CStringLen -> IO b) -> IO b
forall a. StrictByteString -> (CStringLen -> IO a) -> IO a
ByteString.unsafeUseAsCStringLen StrictByteString
bs
unsafeCString :: StrictByteString -> Scoped IO CString
unsafeCString :: StrictByteString -> Scoped IO CString
unsafeCString StrictByteString
bs = (forall b. (CString -> IO b) -> IO b) -> Scoped IO CString
forall {k} (m :: k -> *) a.
(forall (b :: k). (a -> m b) -> m b) -> Scoped m a
Scoped ((forall b. (CString -> IO b) -> IO b) -> Scoped IO CString)
-> (forall b. (CString -> IO b) -> IO b) -> Scoped IO CString
forall a b. (a -> b) -> a -> b
$ StrictByteString -> (CString -> IO b) -> IO b
forall a. StrictByteString -> (CString -> IO a) -> IO a
ByteString.unsafeUseAsCString StrictByteString
bs
mallocBytes :: Int -> Scoped IO (Ptr a)
mallocBytes :: forall a. Int -> Scoped IO (Ptr a)
mallocBytes = IO (Ptr a) -> Scoped IO (Ptr a)
forall (m :: * -> *) a. Monad m => m a -> Scoped m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO (Ptr a) -> Scoped IO (Ptr a))
-> (Int -> IO (Ptr a)) -> Int -> Scoped IO (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IO (Ptr a)
forall a. Int -> IO (Ptr a)
Foreign.mallocBytes
mallocForeignPtrBytes :: Int -> Scoped IO (ForeignPtr a)
mallocForeignPtrBytes :: forall a. Int -> Scoped IO (ForeignPtr a)
mallocForeignPtrBytes Int
len = IO (ForeignPtr a) -> Scoped IO (ForeignPtr a)
forall (m :: * -> *) a. Monad m => m a -> Scoped m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO (ForeignPtr a) -> Scoped IO (ForeignPtr a))
-> IO (ForeignPtr a) -> Scoped IO (ForeignPtr a)
forall a b. (a -> b) -> a -> b
$ Int -> IO (ForeignPtr a)
forall a. Int -> IO (ForeignPtr a)
Foreign.mallocForeignPtrBytes Int
len
copyArray :: Storable a => Ptr a -> Ptr a -> Int -> Scoped IO ()
copyArray :: forall a. Storable a => Ptr a -> Ptr a -> Int -> Scoped IO ()
copyArray Ptr a
target Ptr a
source Int
len = IO () -> Scoped IO ()
forall (m :: * -> *) a. Monad m => m a -> Scoped m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO () -> Scoped IO ()) -> IO () -> Scoped IO ()
forall a b. (a -> b) -> a -> b
$ Ptr a -> Ptr a -> Int -> IO ()
forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
Foreign.copyArray Ptr a
target Ptr a
source Int
len