module Data.StorableVector.ST.Private where
import qualified Data.StorableVector.Base as V
import Data.StorableVector.Memory (mallocForeignPtrArray, )
import Control.Monad.ST.Strict (ST, )
import Foreign.Ptr (Ptr, )
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, )
import Foreign.Storable (Storable, )
import qualified System.Unsafe as Unsafe
import Prelude hiding (read, length, )
data Vector s a =
SV {-# UNPACK #-} !(ForeignPtr a)
{-# UNPACK #-} !Int
create :: (Storable a) => Int -> (Ptr a -> IO ()) -> IO (Vector s a)
create :: forall a s.
Storable a =>
Int -> (Ptr a -> IO ()) -> IO (Vector s a)
create Int
l Ptr a -> IO ()
f = do
ForeignPtr a
fp <- forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
l
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fp Ptr a -> IO ()
f
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall s a. ForeignPtr a -> Int -> Vector s a
SV ForeignPtr a
fp Int
l
{-# INLINE unsafeCreate #-}
unsafeCreate :: (Storable a) => Int -> (Ptr a -> IO ()) -> ST s (Vector s a)
unsafeCreate :: forall a s.
Storable a =>
Int -> (Ptr a -> IO ()) -> ST s (Vector s a)
unsafeCreate Int
l Ptr a -> IO ()
f = forall a s. IO a -> ST s a
Unsafe.ioToST forall a b. (a -> b) -> a -> b
$ forall a s.
Storable a =>
Int -> (Ptr a -> IO ()) -> IO (Vector s a)
create Int
l Ptr a -> IO ()
f
{-# INLINE unsafeToVector #-}
unsafeToVector :: Vector s a -> ST s (V.Vector a)
unsafeToVector :: forall s a. Vector s a -> ST s (Vector a)
unsafeToVector (SV ForeignPtr a
x Int
l) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. ForeignPtr a -> Int -> Int -> Vector a
V.SV ForeignPtr a
x Int
0 Int
l)