Converting things to and from binary

Glynn Clements glynn.clements@virgin.net
Mon, 19 May 2003 19:21:00 +0100


George Russell wrote:

> Sorry, I know there was an ongoing discussion on this topic somewhere, but I
> can't find it, so I'll have to hope this list is the most appropriate.
> 
> 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, I expect Glasgow Haskell has solved
> it too, there really ought to be a standard solution.
> 
>  From the deficiencies of my incomplete solutions I conclude:
> (1) you don't want to force everything to go via single Haskell
> characters.  This is horrendously inefficient when what you want is
> to blast in and out large quantities of binary data, and of course
> that's precisely where you probably want efficiency.
> (2) you don't want a binary converter only to be able to write to
> Handles.  I've found myself that it's useful to be able to
> convert to (for example) vectors of bytes.

Possibly the best way to handle this would be to extend the Storable
class, e.g.

class Storable a where
	...
	hPutObj :: Handle -> a -> IO ()
	hGetObj :: Handle -> IO a
	toByteList :: a -> [Word8]
	fromByteList :: [Word8] -> a
	toByteArray :: a -> Array Int Word8
	fromByteArray :: Array Int Word8 -> a

At a minimum, it would be useful to at least have standard low-level
read/write functions, e.g.

	hWrite :: Handle -> Ptr a -> IO ()
	hRead :: Handle -> Ptr a -> IO ()

The rest could then be constructed using the existing Storable
methods.

That still leaves the effort of [un]marshalling boxed values, but that
may be unavoidable.

-- 
Glynn Clements <glynn.clements@virgin.net>