Elegant definition of Storable instances for records
|LTS Haskell 20.25:||0.0.7|
|Stackage Nightly 2023-06-10:||0.0.7|
|Latest on Hackage:||0.0.7|
Module documentation for 0.0.7
With this package you can build a Storable instance of a record type from Storable instances of its elements in an elegant way. It does not do any magic, just a bit arithmetic to compute the right offsets, that would be otherwise done manually or by a preprocessor like C2HS. I cannot promise that the generated memory layout is compatible with that of a corresponding C struct. However, the module generates the smallest layout that is possible with respect to the alignment of the record elements. If you encounter, that a record does not have a compatible layout, we should fix that. But also without C compatibility this package is useful e.g. in connection with StorableVector.
We provide Storable instance support for several cases:
If you wrap a type in a
newtype, then you can lift its
Storableinstance to that
newtypewith the module
Foreign.Storable.Newtype. This way you do not need the
GeneralizedNewtypeDerivingfeature of GHC.
If you have a type that is an instance of
Traversable, you can use that feature for implementation of
Storablemethods. The module
Foreign.Storable.Traversableallows manipulation of the portion of your type, that is accessible by
Traversablemethods. For instance with the type
data T a = Cons Int [a]and an according
Traversableimplementation, you can load and store the elements of the contained list. This may be part of a
Storableimplementation of the whole type.
If you have a record containing elements of various types, then you need module
Note however that the Storable instances defined with this package are quite slow in (up to) GHC-6.12.1. I'm afraid this is due to incomplete inlining, but we have still to investigate the problem.
For examples see packages