Copyright | (c) Dong Han 2017-2019 |
---|---|
License | BSD |
Maintainer | [email protected] |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Z.Data.Array.UnalignedAccess
Description
This module implements unaligned element access with ghc primitives (> 8.6).
Synopsis
- newtype UnalignedSize a = UnalignedSize {}
- class UnalignedAccess a where
- unalignedSize :: UnalignedSize a
- indexWord8ArrayAs# :: ByteArray# -> Int# -> a
- readWord8ArrayAs# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
- writeWord8ArrayAs# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s
- peekMBA :: MutableByteArray# RealWorld -> Int -> IO a
- pokeMBA :: MutableByteArray# RealWorld -> Int -> a -> IO ()
- indexBA :: ByteArray# -> Int -> a
- writeWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutableByteArray (PrimState m) -> Int -> a -> m ()
- readWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutableByteArray (PrimState m) -> Int -> m a
- indexWord8ArrayAs :: UnalignedAccess a => ByteArray -> Int -> a
- writePrimWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutablePrimArray (PrimState m) Word8 -> Int -> a -> m ()
- readPrimWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutablePrimArray (PrimState m) Word8 -> Int -> m a
- indexPrimWord8ArrayAs :: UnalignedAccess a => PrimArray Word8 -> Int -> a
- newtype LE a = LE {
- getLE :: a
- newtype BE a = BE {
- getBE :: a
Documentation
newtype UnalignedSize a Source #
Constructors
UnalignedSize | |
Fields |
Instances
Eq (UnalignedSize a) Source # | |
Defined in Z.Data.Array.UnalignedAccess Methods (==) :: UnalignedSize a -> UnalignedSize a -> Bool # (/=) :: UnalignedSize a -> UnalignedSize a -> Bool # | |
Show (UnalignedSize a) Source # | |
Defined in Z.Data.Array.UnalignedAccess Methods showsPrec :: Int -> UnalignedSize a -> ShowS # show :: UnalignedSize a -> String # showList :: [UnalignedSize a] -> ShowS # |
class UnalignedAccess a where Source #
Primitive types which can be unaligned accessed
It can also be used as a lightweight method to peek/poke value from/to C structs
when you pass MutableByteArray#
to FFI as struct pointer, e.g.
-- | note the .hsc syntax peekSocketAddrMBA :: HasCallStack => MBA## SocketAddr -> IO SocketAddr peekSocketAddrMBA p = do family <- peekMBA p (#offset struct sockaddr, sa_family) case family :: CSaFamily of (#const AF_INET) -> do addr <- peekMBA p (#offset struct sockaddr_in, sin_addr) port <- peekMBA p (#offset struct sockaddr_in, sin_port) return (SocketAddrInet (PortNumber port) addr) ....
Minimal complete definition
unalignedSize, indexWord8ArrayAs#, writeWord8ArrayAs#, readWord8ArrayAs# | unalignedSize, indexBA, peekMBA, pokeMBA
Methods
unalignedSize :: UnalignedSize a Source #
byte size
indexWord8ArrayAs# :: ByteArray# -> Int# -> a Source #
index element off byte array with offset in bytes(maybe unaligned)
readWord8ArrayAs# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #) Source #
read element from byte array with offset in bytes(maybe unaligned)
writeWord8ArrayAs# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s Source #
write element to byte array with offset in bytes(maybe unaligned)
peekMBA :: MutableByteArray# RealWorld -> Int -> IO a Source #
IO version of writeWord8ArrayAs#
but more convenient to write manually.
pokeMBA :: MutableByteArray# RealWorld -> Int -> a -> IO () Source #
IO version of readWord8ArrayAs#
but more convenient to write manually.
indexBA :: ByteArray# -> Int -> a Source #
index element off byte array with offset in bytes(maybe unaligned)
Instances
writeWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutableByteArray (PrimState m) -> Int -> a -> m () Source #
Lifted version of writeWord8ArrayAs#
readWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutableByteArray (PrimState m) -> Int -> m a Source #
Lifted version of readWord8ArrayAs#
indexWord8ArrayAs :: UnalignedAccess a => ByteArray -> Int -> a Source #
Lifted version of indexWord8ArrayAs#
writePrimWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutablePrimArray (PrimState m) Word8 -> Int -> a -> m () Source #
Lifted version of writeWord8ArrayAs#
readPrimWord8ArrayAs :: (PrimMonad m, UnalignedAccess a) => MutablePrimArray (PrimState m) Word8 -> Int -> m a Source #
Lifted version of readWord8ArrayAs#
indexPrimWord8ArrayAs :: UnalignedAccess a => PrimArray Word8 -> Int -> a Source #
Lifted version of indexWord8ArrayAs#
little endianess wrapper
Instances
big endianess wrapper