Converting things to and from binary
Ketil Z. Malde
20 May 2003 09:07:24 +0200
Glynn Clements <firstname.lastname@example.org> writes:
> George Russell wrote:
>> The general problem to solve is "How to we convert things to and
>> from a binary format, so they can be efficiently written to and
>> from disk". I have solved this (incompletely) twice myself,
> Possibly the best way to handle this would be to extend the Storable
> class, e.g.
> class Storable a where
> toByteArray :: a -> Array Int Word8
> fromByteArray :: Array Int Word8 -> a
I wonder if this could be used to address a question (or perhaps
"frustration" is a better term), namely that of efficient packing of
data structures in memory.
Would it be possible to separate binary I/O in two layers, the binary
layer converting to/from (U?)Arrays of Word8, and another, the I/O
layer, reading and writing such arrays?
Something almost entirely, but not quite, unlike:
class Binary a where
toBin :: a -> Array Int Word8
toBinList :: [a] -> Array Int Word8
fromBin :: Array Int Word8 -> a -- perhaps with offset?
fromBinList :: Array Int Word8 -> [a]
hPutArray :: Handle -> Array Int Word8 -> IO ()
hGetArray :: ...
writeBinaryFile :: FilePath -> Array Int Word8 -> IO ()
If I haven't seen further, it is by standing in the footprints of giants