summaryrefslogtreecommitdiff
path: root/haskell/Data/BA.hs
blob: 60b1136fa4ee5313dc8fbc1b4c1826b4d5914a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{-# LANGUAGE MagicHash           #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.BA where

import GHC.Exts
import Control.Monad.Primitive
import Data.Primitive.Types
import Data.Primitive.ByteArray

-- | WARNING: Unsafe to use this on packed C structs.
writeAtByte :: (Prim a, PrimMonad m) => MutableByteArray (PrimState m) -> Int -> a -> m ()
writeAtByte buf offset a = writeByteArray buf (div offset $ I# (sizeOf# a)) a
{-# INLINE writeAtByte #-}

readAtByte :: forall a m. (Prim a, PrimMonad m) => MutableByteArray (PrimState m) -> Int -> m a
readAtByte buf offset = readByteArray buf (div offset $ I# (sizeOf# (undefined :: a)))
{-# INLINE readAtByte #-}