{-# LANGUAGE CPP #-}
module Database.PostgreSQL.LibPQ.Marshal where

import Foreign (Ptr, Storable, allocaArray, nullPtr, pokeArray)

unsafeWithArray :: Storable a => Int -> [a] -> (Ptr a -> IO b) -> IO b
unsafeWithArray :: forall a b. Storable a => Int -> [a] -> (Ptr a -> IO b) -> IO b
unsafeWithArray Int
len [a]
vals Ptr a -> IO b
f =
#if 0
  if len /= length vals then error "unsafeWithArray: len mismatch" else
#endif
  forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
len forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
      forall a. Storable a => Ptr a -> [a] -> IO ()
pokeArray Ptr a
ptr [a]
vals
      Ptr a -> IO b
f Ptr a
ptr

-- | Like maybe with but takes an int. Usable with 'withArrayLen'.
-- In 'Nothing' case uses 0 and 'nullPtr'.
maybeWithInt :: (     a -> (Int -> Ptr b -> IO c) -> IO c)
          -> (Maybe a -> (Int -> Ptr b -> IO c) -> IO c)
maybeWithInt :: forall a b c.
(a -> (Int -> Ptr b -> IO c) -> IO c)
-> Maybe a -> (Int -> Ptr b -> IO c) -> IO c
maybeWithInt = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (\Int -> Ptr b -> IO c
f -> Int -> Ptr b -> IO c
f Int
0 forall a. Ptr a
nullPtr)