module HsBindgen.Runtime.IncompleteArray (
IncompleteArray
, toVector
, fromVector
, toPtr
, toFirstElemPtr
, peekArray
, pokeArray
, withPtr
, repeat
, fromList
, toList
) where
import Prelude hiding (repeat)
import Data.Coerce (Coercible, coerce)
import Data.Vector.Storable qualified as VS
import Foreign.ForeignPtr (mallocForeignPtrArray, withForeignPtr)
import Foreign.Marshal.Utils (copyBytes)
import Foreign.Ptr (Ptr, castPtr, plusPtr)
import Foreign.Storable (Storable (..))
import GHC.Records (HasField (..))
newtype IncompleteArray a = IA (VS.Vector a)
deriving stock (IncompleteArray a -> IncompleteArray a -> Bool
(IncompleteArray a -> IncompleteArray a -> Bool)
-> (IncompleteArray a -> IncompleteArray a -> Bool)
-> Eq (IncompleteArray a)
forall a.
(Storable a, Eq a) =>
IncompleteArray a -> IncompleteArray a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a.
(Storable a, Eq a) =>
IncompleteArray a -> IncompleteArray a -> Bool
== :: IncompleteArray a -> IncompleteArray a -> Bool
$c/= :: forall a.
(Storable a, Eq a) =>
IncompleteArray a -> IncompleteArray a -> Bool
/= :: IncompleteArray a -> IncompleteArray a -> Bool
Eq, Int -> IncompleteArray a -> ShowS
[IncompleteArray a] -> ShowS
IncompleteArray a -> String
(Int -> IncompleteArray a -> ShowS)
-> (IncompleteArray a -> String)
-> ([IncompleteArray a] -> ShowS)
-> Show (IncompleteArray a)
forall a. (Show a, Storable a) => Int -> IncompleteArray a -> ShowS
forall a. (Show a, Storable a) => [IncompleteArray a] -> ShowS
forall a. (Show a, Storable a) => IncompleteArray a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. (Show a, Storable a) => Int -> IncompleteArray a -> ShowS
showsPrec :: Int -> IncompleteArray a -> ShowS
$cshow :: forall a. (Show a, Storable a) => IncompleteArray a -> String
show :: IncompleteArray a -> String
$cshowList :: forall a. (Show a, Storable a) => [IncompleteArray a] -> ShowS
showList :: [IncompleteArray a] -> ShowS
Show)
type role IncompleteArray nominal
toVector ::
Coercible arrayLike (IncompleteArray a)
=> arrayLike
-> VS.Vector a
toVector :: forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
arrayLike -> Vector a
toVector (arrayLike -> Vector a
forall a b. Coercible a b => a -> b
coerce -> Vector a
xs) = Vector a
xs
fromVector ::
Coercible arrayLike (IncompleteArray a)
=> VS.Vector a
-> arrayLike
fromVector :: forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
Vector a -> arrayLike
fromVector = Vector a -> arrayLike
forall a b. Coercible a b => a -> b
coerce
instance HasField "toFirstElemPtr" (Ptr (IncompleteArray a)) (Ptr a) where
getField :: Ptr (IncompleteArray a) -> Ptr a
getField = Ptr (IncompleteArray a) -> Ptr a
forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
Ptr arrayLike -> Ptr a
toFirstElemPtr
toPtr ::
forall arrayLike a. Coercible arrayLike (IncompleteArray a)
=> Ptr a
-> Ptr arrayLike
toPtr :: forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
Ptr a -> Ptr arrayLike
toPtr = Ptr a -> Ptr arrayLike
forall a b. Ptr a -> Ptr b
castPtr
where
_unused :: arrayLike -> IncompleteArray a
_unused = forall a b. Coercible a b => a -> b
forall a b. Coercible a b => a -> b
coerce @arrayLike @(IncompleteArray a)
toFirstElemPtr ::
forall arrayLike a. Coercible arrayLike (IncompleteArray a)
=> Ptr arrayLike
-> Ptr a
toFirstElemPtr :: forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
Ptr arrayLike -> Ptr a
toFirstElemPtr Ptr arrayLike
ptr = Ptr arrayLike -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr Ptr arrayLike
ptr
where
_unused :: arrayLike -> IncompleteArray a
_unused = forall a b. Coercible a b => a -> b
forall a b. Coercible a b => a -> b
coerce @arrayLike @(IncompleteArray a)
peekArray ::
forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a)
=> Int
-> Ptr arrayLike
-> IO arrayLike
peekArray :: forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Int -> Ptr arrayLike -> IO arrayLike
peekArray = Int -> Int -> Ptr arrayLike -> IO arrayLike
forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Int -> Int -> Ptr arrayLike -> IO arrayLike
peekArrayOff Int
0
peekArrayOff ::
forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a)
=> Int
-> Int
-> Ptr arrayLike
-> IO arrayLike
peekArrayOff :: forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Int -> Int -> Ptr arrayLike -> IO arrayLike
peekArrayOff Int
off Int
size Ptr arrayLike
ptr = do
ForeignPtr a
fptr <- forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray @a Int
size
ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr a
ptr' :: Ptr a) -> do
Ptr a -> Ptr a -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr a
ptr' (Ptr arrayLike -> Ptr Any
forall a b. Ptr a -> Ptr b
castPtr Ptr arrayLike
ptr Ptr Any -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offBytes) (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sizeOfA)
Vector a
vs <- MVector (PrimState IO) a -> IO (Vector a)
forall a (m :: * -> *).
(Storable a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
VS.freeze (Int -> ForeignPtr a -> MVector RealWorld a
forall s a. Int -> ForeignPtr a -> MVector s a
VS.MVector Int
size ForeignPtr a
fptr)
arrayLike -> IO arrayLike
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (arrayLike -> IO arrayLike) -> arrayLike -> IO arrayLike
forall a b. (a -> b) -> a -> b
$ IncompleteArray a -> arrayLike
forall a b. Coercible a b => a -> b
coerce (Vector a -> IncompleteArray a
forall a. Vector a -> IncompleteArray a
IA Vector a
vs)
where
sizeOfA :: Int
sizeOfA = a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)
offBytes :: Int
offBytes = Int
sizeOfA Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
off
pokeArray ::
forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a)
=> Ptr arrayLike
-> arrayLike
-> IO ()
pokeArray :: forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Ptr arrayLike -> arrayLike -> IO ()
pokeArray = Int -> Ptr arrayLike -> arrayLike -> IO ()
forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Int -> Ptr arrayLike -> arrayLike -> IO ()
pokeArrayOff Int
0
pokeArrayOff ::
forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a)
=> Int
-> Ptr arrayLike
-> arrayLike
-> IO ()
pokeArrayOff :: forall a arrayLike.
(Coercible arrayLike (IncompleteArray a), Storable a) =>
Int -> Ptr arrayLike -> arrayLike -> IO ()
pokeArrayOff Int
off Ptr arrayLike
ptr (arrayLike -> IncompleteArray a
forall a b. Coercible a b => a -> b
coerce -> IA Vector a
vs) = do
VS.MVector Int
size ForeignPtr a
fptr <- Vector a -> IO (MVector (PrimState IO) a)
forall a (m :: * -> *).
(Storable a, PrimMonad m) =>
Vector a -> m (MVector (PrimState m) a)
VS.unsafeThaw Vector a
vs
ForeignPtr a -> (Ptr a -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr a
ptr' :: Ptr a) ->
Ptr Any -> Ptr Any -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes (Ptr arrayLike -> Ptr Any
forall a b. Ptr a -> Ptr b
castPtr Ptr arrayLike
ptr) (Ptr a
ptr' Ptr a -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offBytes) (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sizeOfA)
where
sizeOfA :: Int
sizeOfA = a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)
offBytes :: Int
offBytes = Int
sizeOfA Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
off
withPtr ::
(Coercible b (IncompleteArray a), Storable a)
=> b -> (Ptr b -> IO r) -> IO r
withPtr :: forall b a r.
(Coercible b (IncompleteArray a), Storable a) =>
b -> (Ptr b -> IO r) -> IO r
withPtr (b -> IncompleteArray a
forall a b. Coercible a b => a -> b
coerce -> IA Vector a
v) Ptr b -> IO r
k = do
VS.MVector Int
_ ForeignPtr a
fptr <- Vector a -> IO (MVector (PrimState IO) a)
forall a (m :: * -> *).
(Storable a, PrimMonad m) =>
Vector a -> m (MVector (PrimState m) a)
VS.thaw Vector a
v
ForeignPtr a -> (Ptr a -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> IO r) -> IO r) -> (Ptr a -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \(Ptr a
ptr :: Ptr a) -> Ptr b -> IO r
k (Ptr a -> Ptr b
forall arrayLike a.
Coercible arrayLike (IncompleteArray a) =>
Ptr a -> Ptr arrayLike
toPtr Ptr a
ptr)
repeat :: Storable a => Int -> a -> IncompleteArray a
repeat :: forall a. Storable a => Int -> a -> IncompleteArray a
repeat Int
n a
x = Vector a -> IncompleteArray a
forall a. Vector a -> IncompleteArray a
IA (Int -> a -> Vector a
forall a. Storable a => Int -> a -> Vector a
VS.replicate Int
n a
x)
fromList :: Storable a => [a] -> IncompleteArray a
fromList :: forall a. Storable a => [a] -> IncompleteArray a
fromList [a]
xs = Vector a -> IncompleteArray a
forall a. Vector a -> IncompleteArray a
IA ([a] -> Vector a
forall a. Storable a => [a] -> Vector a
VS.fromList [a]
xs)
toList :: Storable a => IncompleteArray a -> [a]
toList :: forall a. Storable a => IncompleteArray a -> [a]
toList (IA Vector a
v) = Vector a -> [a]
forall a. Storable a => Vector a -> [a]
VS.toList Vector a
v