derive-storable: Derive Storable instances with GHC.Generics.

[ foreign, library, mit ] [ Propose Tags ]

Derive Storable instances with GHC.Generics. The derived Storable instances have the same alignment as C structs.


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
sumtypes

Enable support for non-recursive sum types.

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.1.0, 0.1.1.1, 0.1.2.0, 0.2.0.0, 0.3.0.0, 0.3.1.0
Change log ChangeLog.md
Dependencies base (>=4.8 && <5) [details]
License MIT
Author Mateusz Kloczko
Maintainer mateusz.p.kloczko@gmail.com
Category Foreign
Home page https://www.github.com/mkloczko/derive-storable/
Source repo head: git clone https://github.com/mkloczko/derive-storable -b master
Uploaded by mkloczko at 2022-04-09T09:38:04Z
Distributions LTSHaskell:0.3.1.0, NixOS:0.3.1.0, Stackage:0.3.1.0
Reverse Dependencies 9 direct, 11 indirect [details]
Downloads 8760 total (55 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for derive-storable-0.3.1.0

[back to package description]

Introduction

Build Status

The derive-storable package allows you to automatically generate Storable instances for your datatypes. It uses GHC.Generics, which allows the coders to derive certain instances automatically. To derive a (G)Storable instance, the data-type has to:

  • have only one constructor There is now a sumtypes option for data-types with multiple constructors. See Sum types section for more.
  • all fields of the constructor need to be GStorable.
  • implement a Generic instance (derive (Generic))

Sum types

To enable support for sum types, add a -f sumtypes option to cabal new-build or cabal new-configure. The library discerns between sum and non-sum types. Non-sum types have the same memory layout as C structs, while sum types correspond to tagged unions:

struct datatype {
    unsigned char tag;
    union { 
        constructor1 a;
        constructor2 b;
        ...
    } val;
};

Note - while it is possible to have an instance for a self/mutually recursive data-type, using methods for the data-type will result in infinite loop. So there is no support for recursion in data-types.

Note on performance

There are some problems with performance of derived Storable instances. For now there exists a solution in form of GHC Core plugin - derive-storable-plugin.

Usage

Here's an example:

{-# LANGUAGE DeriveGeneric #-}

import Foreign.Storable
import Foreign.Storable.Generic
import Foreign.Ptr
import Foreign.Marshal.Alloc

import Generics.Deriving

data Position = Position {
   x :: Double, 
   y :: Double
} deriving (Show,Read, Generic)

instance GStorable Position

updatePosition :: Ptr Position -> Position -> IO ()
updatePosition ptr pos = poke ptr pos


main = do
    let val = Position 0.0 10.0
    ptr <- malloc :: IO (Ptr Position)      
    putStrLn "Created a ptr with value of"
    putStrLn =<< show <$> peek ptr
    updatePosition ptr val
    putStrLn "And now the value of ptr is:"   
    putStrLn =<< show <$> peek ptr