Converting things to and from binary

Tomasz Zielonka t.zielonka@students.mimuw.edu.pl
Mon, 19 May 2003 23:00:13 +0200


On Mon, May 19, 2003 at 07:21:00PM +0100, Glynn Clements wrote:
> 
> 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".
> 
> 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

Hmmm, I thought that Foreign.Storable.Storable is meant for different
purposes, namely for accessing C data in memory and for marshalling
between Haskell and C worlds. Are you sure that these functionalities
must overlap?

Extending Storable would require to extend its every existing instance.
It might be better to introduce some subclass, but I'm not sure that
every type supporting marshalling to and from binary format can
support operations from Storable.

It's quite easy to write value of type [[Int]] to disk, but how to
define alignment and sizeOf methods for this type?

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links