module Data.Vector.Storable.Internal (
ptrToOffset, offsetToPtr
) where
import Control.Monad.Primitive ( unsafeInlineIO )
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Marshal.Array ( advancePtr )
import GHC.Base ( quotInt )
distance :: forall a. Storable a => Ptr a -> Ptr a -> Int
distance p q = (p `minusPtr` q) `quotInt` sizeOf (undefined :: a)
ptrToOffset :: Storable a => ForeignPtr a -> Ptr a -> Int
ptrToOffset fp q = unsafeInlineIO
$ withForeignPtr fp $ \p -> return (distance q p)
offsetToPtr :: Storable a => ForeignPtr a -> Int -> Ptr a
offsetToPtr fp i = unsafeInlineIO
$ withForeignPtr fp $ \p -> return (advancePtr p i)