{-# OPTIONS_HADDOCK hide #-}

-- NOTE: For now, this module is classified "Internal" because the definitions
-- are re-exported from the runtime prelude. Should we add definitions intended
-- for qualified import, we need to add a public module.

-- | Pointer utilities
module HsBindgen.Runtime.Internal.Ptr (
    plusPtrElem
  ) where

import Foreign.Ptr
import Foreign.Storable

-- | Advances the given address by the given offset in number of elements of
-- type @a@.
--
-- Examples:
--
-- > plusPtr (_ :: Ptr Word32) 2 -- moves the pointer by 2 bytes
-- > plusPtrElem (_ :: Ptr Word32) 2 -- moves the pointer by 2*4=8 bytes
--
-- NOTE: if @a@ is an instance of 'Data.Primitive.Types.Prim', then you can
-- alternatively use 'Data.Primitive.Ptr.advancePtr'.
plusPtrElem :: forall a. Storable a => Ptr a -> Int -> Ptr a
plusPtrElem :: forall a. Storable a => Ptr a -> Int -> Ptr a
plusPtrElem Ptr a
ptr Int
i = Ptr a
ptr Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a))