raw data access
john at repetae.net
Mon May 1 19:46:07 EDT 2006
On Tue, May 02, 2006 at 12:57:17AM +0200, Johan Henriksson wrote:
> toBinary :: a -> [Int] -- pack data as a string of bytes
> fromBinary :: [Int] -> a -- unpack
> binarySize :: a -> Maybe Int
> -- number of bytes for this type or Nothing if not fixed
> the packing would be compiler dependent since it is not of interest
> to read the content, just to get an easy way of marshalling arbitrary types.
Depending on what you mean, we might already have this, or it might be
impossible in general :)
if you just want to create a reference to an arbitrary haskell type that
you can pass to foreign code, and then recover the original haskell
value from, then the Foreign.StablePtr does just this. it lets you cast
haskell values to plain pointers and back again.
if you want to store the structure in memory, then the Storable class
provides this. I have thought it would be useful to allow
deriving(Storable) with the obvious meaning and adding a 'StorableRef'
that can create a reference to an arbitray storable object. I believe
bulat is working on unboxed arrays that can work like this.
of course, the limitation is that your data types need to be in class
since you don't seem to care about compiler independence and if you are
willing to give up architecture independence, you can probably use
storable to serialize structures to disk, but do so at your own risk.
pretty much any solution will require a typeclass to guide and restrict
it, otherwise how would the compiler handle function types? it can't
serialize the body of functions in general, or handle cyclic structures
or pointers from the serialized version to live haskell structures and
how they interect with the GC?
You also might want to look at the various Binary libraries out there.
perhaps one fits your needs.
John Meacham - ⑆repetae.net⑆john⑈
More information about the Haskell-prime