Safe Haskell | None |
---|---|
Language | Haskell98 |
Convert Haskell values to and from a compact binary representation.
For testing purposes, use packToList
which takes a format, a record, and returns a list of bytes.
> import Data.Repa.Convert.Format > let Just bytes = packToList (FixString ASCII 10 :*: Word16be :*: Float64be) ("foo" :*: 66 :*: 93.42) > bytes [102,111,111,0,0,0,0,0,0,0,0,66,64,87,90,225,71,174,20,123]
We can then unpack the raw bytes back to Haskell values with unpackFromList
.
> unpackFromList (FixString ASCII 10 :*: Word16be :*: Float64be) bytes Just ("foo" :*: (66 :*: 93.42))
In production code use pack
and unpack
to work directly with a buffer in foreign memory.
- class Format f where
- class Format format => Packable format where
- packToList :: Packable format => format -> Value format -> Maybe [Word8]
- unpackFromList :: Packable format => format -> [Word8] -> Maybe (Value format)
- forFormat :: format -> Value format -> Value format
- listFormat :: format -> [Value format] -> [Value format]
- data a :*: b = !a :*: !b
- data FixList f = FixList f Int
- data VarList f = VarList f
- data FixString t = FixString t Int
- data VarString t = VarString t
- data ASCII = ASCII
- data Word8be = Word8be
- data Int8be = Int8be
- data Word16be = Word16be
- data Int16be = Int16be
- data Word32be = Word32be
- data Int32be = Int32be
- data Float32be = Float32be
- data Word64be = Word64be
- data Int64be = Int64be
- data Float64be = Float64be
Data formats
Relates a storage format to the Haskell type of the value that is stored in that format.
fixedSize :: f -> Maybe Int Source
For fixed size storage formats, yield their size (length) in bytes.
Yields Nothing
if this is not a fixed size format.
packedSize :: f -> Value f -> Maybe Int Source
Yield the size of a value in the given format.
Yields Nothing
when a collection of values is to be packed into a
fixed length format, but the size of the collection does not match
the format.
Format Float64be | |
Format Float32be | |
Format Word64be | |
Format Int64be | |
Format Word32be | |
Format Int32be | |
Format Word16be | |
Format Int16be | |
Format Word8be | |
Format Int8be | |
Format (VarString ASCII) | |
Format (FixString ASCII) | |
Format f => Format (VarList f) | |
Format f => Format (FixList f) | |
(Format a, Format b) => Format ((:*:) a b) |
Packing records
class Format format => Packable format where Source
Class of storage formats that can have values packed and unpacked from foreign bufferes.
The methods are written using continuations to make it easier for GHC to optimise its core code when packing/unpacking many fields.
:: Ptr Word8 | Target Buffer. |
-> format | Storage format. |
-> Value format | Value to pack. |
-> (Int -> IO (Maybe a)) | Continue, given the number of bytes written. |
-> IO (Maybe a) |
Pack a value into a buffer using the given format.
If the format contains fixed width fields and the corresponding
value has too many elements, then this function returns False
,
otherwise True
.
:: Ptr Word8 | Source buffer. |
-> format | Format of buffer. |
-> ((Value format, Int) -> IO (Maybe a)) | Continue, given the unpacked value and the number of bytes read. |
-> IO (Maybe a) |
Unpack a value from a buffer using the given format.
This is the inverse of pack
above.
packToList :: Packable format => format -> Value format -> Maybe [Word8] Source
Pack a value into a list of Word8
.
unpackFromList :: Packable format => format -> [Word8] -> Maybe (Value format) Source
Unpack a value from a list of Word8
.
Constraints
forFormat :: format -> Value format -> Value format Source
Constrain the type of a value to match the given format.
The value itself is not used.
listFormat :: format -> [Value format] -> [Value format] Source
Constrain the type of some values to match the given format.
The value itself is not used.
Products
Strict product type, written infix.
!a :*: !b infixr 9 |
(Unbox a, Unbox b) => Vector Vector ((:*:) a b) | |
(Unbox a, Unbox b) => MVector MVector ((:*:) a b) | |
(Eq a, Eq b) => Eq ((:*:) a b) | |
(Show a, Show b) => Show ((:*:) a b) | |
(Unbox a, Unbox b) => Unbox ((:*:) a b) | |
(Packable fa, Packable fb) => Packable ((:*:) fa fb) | |
(Format a, Format b) => Format ((:*:) a b) | |
data MVector s ((:*:) a b) = MV_Prod !Int !(MVector s a) !(MVector s b) | |
data Vector ((:*:) a b) = V_Prod !Int !(Vector a) !(Vector b) | |
type Value ((:*:) a b) = (:*:) (Value a) (Value b) |
Lists
Fixed length list.
Variable length list.
VarList f |
Strings
Fixed length string.
- When packing, if the provided string is shorter than the fixed length then the extra bytes are zero-filled.
Variable length string.
String is encoded as 8-bit ASCII characters.
Atomic values
8-bit
Big-endian 8-bit unsigned word.
Big-endian 8-bit signed integer.
16-bit
Big-endian 32-bit unsigned word.
32-bit
Big-endian 32-bit unsigned word.
Big-endian 32-bit signed integer.
Big-endian 32-bit IEEE 754 float.
64-bit
Big-endian 64-bit unsigned word.
Big-endian 64-bit signed integer.